c++ proposed exercises (chapter 7: the c++ programing language, fourth edition)

7
 Proposed Exercises Chapter 7 In this document you can nd exercises related to the content of C++ Programming Language (Fourth Edition). Solut ion is prov ided to some probl ems. How ev er, if you fo llow the book care fully , you will be able to solv e the proposed problems easily. Introduction material is very dense and that’s why no exercise material is elaborated. Don’t get panic If you have difculti es trying to solve these exercis es. The material provided in the Introduction section (Part I) and chapter seven, allow you to PROPOSE some solution. Some recommendations are: 1. T ry to mimic the Programming Technique  found in the chapter. 2. For now , don’t worry for efciency , make the program work. 3. Declaration implies denition (init ialization). 4. T est your code in main(). 5. Smile, you are beco ming part of the C++ community . A. Pointers and Reference. 1. Declares a pointer to double and print the address and v alue. 2. Declares a pointer to double that point to the addres s of pointer implemented in (1). Print both address and values. 3. Identify visually if an y error is invok ed: 1  #include  <iostream> 2 3  int  main () { 4  const int  data = 3.15; 5  const int  data1 {3 .15 }; 6  const int   data2 =  new in t  (3.15) ; 7  const int   data3{ new int  {3.15}}; Now, use your IDE and compare results. 4. I forget to free memory in the prev ious ex ercises. Please do it for me. 5. (*) Create a function "Add" with the f ollowing charac teristics: (a) T wo double values input. One is a pointer to double, the other is an lvalue. (b) Outp ut is a point er to double. (c) Declares a pointer to val ue equal to Add(x,y). Print the address and value of the pointer. (d) Check vis ually for any error in the fo llowing code:

Upload: mauricio-bedoya

Post on 05-Nov-2015

10 views

Category:

Documents


0 download

DESCRIPTION

.

TRANSCRIPT

  • Proposed Exercises Chapter 7

    In this document you can find exercises related to the content of C++ Programming Language (Fourth Edition).Solution is provided to some problems. However, if you follow the book carefully, you will be able to solve theproposed problems easily.

    Introduction material is very dense and thats why no exercise material is elaborated.Dont get panic If you have difficulties trying to solve these exercises. The material provided in the Introduction

    section (Part I) and chapter seven, allow you to PROPOSE some solution. Some recommendations are:

    1. Try to mimic the Programming Technique found in the chapter.

    2. For now, dont worry for efficiency, make the program work.

    3. Declaration implies definition (initialization).

    4. Test your code in main().

    5. Smile, you are becoming part of the C++ community.

    A. Pointers and Reference.

    1. Declares a pointer to double and print the address and value.

    2. Declares a pointer to double that point to the address of pointer implemented in (1). Print both address andvalues.

    3. Identify visually if any error is invoked:

    1 #include 23 i n t main ( ) {4 const in t data = 3 .15 ;5 const in t data1 { 3 . 1 5 } ;6 const in t data2 = new in t ( 3 . 1 5 ) ;7 const in t data3 {new in t { 3 . 1 5 } } ;

    Now, use your IDE and compare results.

    4. I forget to free memory in the previous exercises. Please do it for me.

    5. (*) Create a function "Add" with the following characteristics:

    (a) Two double values input. One is a pointer to double, the other is an lvalue.

    (b) Output is a pointer to double.

    (c) Declares a pointer to value equal to Add(x,y). Print the address and value of the pointer.

    (d) Check visually for any error in the following code:

  • 1 #include 2 using namespace std ;34 double Add (double value1 , double value2 ) {5 double r e s u l t = value1 + value2 ;6 return &r e s u l t ;7 }89 i n t main ( ) {

    10 double data1 {new double { 1 2 . 5 } } ;11 double data2 { 1 0 . 5 } ;12 void Resul t {Add ( data1 , data2 ) } ;13 / / Memory i s Pr in ted14 cout

  • 11. Test visually for any errors in the following code:

    1 #include 2 using namespace std ;34 i n t main ( ) {5 / / Se lec t the co r r ec t one ( Both can be ok )6 / One /7 /1 /8 i n t data [ 1 2 ] = { 1 . 1 , 2 . 2 , 3 . 3 , 4 . 4 , 5 . 5 } ;9 /2 /

    10 double data1 [ 1 2 ] = { 1 . 1 , 2 . 2 , 3 . 3 , 4 . 4 , 5 . 5 } ;1112 / Two /13 /1 /14 char data2 [ ] = { " Hola " , " He l lo " , " Halo " } ;15 /2 /16 s t r i n g data2 [ ] = { " Hola " , " He l lo " , " Halo " } ;1718 / Three /19 const in t s ize = 5;20 /1 /21 i n t data3 [ s ize ] ;22 for ( i n t i = 0 ; i != 5 ; ++ i )23 {24 data3 [ i ] = i ;25 }26 for ( i n t i = 0 ; i != s ize ; i ++)27 {28 cout

  • C. Pointers and const

    A trick to use and differentiate pointers expressions, is to read the declaration from right to left.

    16. Write the definition of the following declarations (example: int* data: data is a pointer to integer):

    const int * var1 :: var2 is a pointer to double: var3 is a constant pointer to constant float: var4 is an array of constant pointers to double: var5 is a constant array of pointers to constant string

    Figure 1: Constants & Pointer Declaration

    17. Declare (no initialization) a pointer to integer. Any warning or error message?

    18. Declare (no initialization) a pointer to a constant double. Any warning or error message?

    19. Declare (no initialization) a constant pointer to a constant string. Any warning or error message?

    20. Declare a constant pointer to a constant double with default value equal to nullptr.

    21. (*)Declare tow pointers to constant integers with initial value 1 and 7. Implement a function called print thatadd (1+7) with the following characteristics:

    (a) Two pointer to integer as input.

    (b) One pointer to integer as output.

    Use std::cout to prompt the result (Hint: Use const_cast ()).

    22. Check the following code:

    1 #include 23 using namespace std ;45 void p r i n t ( const in t& data ) { cout

  • D. lvalue & rvalue

    23. In page 75 you can find: "So an rvalue is - to a first approximation - a value that you cant assign to, suchas an integer returned by a function call, and an rvalue reference is a reference to something that nobodyelse can assign to." In my IDE (Xcode 6.3 - libc++ Standard Library) I can identify rvalue easily due to thefollowing error message:

    Expression is not assignable. Non-const lvalue reference to type int...... Cant take the address of an rvalue of ...

    How do you identify this in your IDE. ?

    24. In page 191, you find the following declaration:

    1 #include 23 using namespace std ;45 i n t main ( ) {6 double& dr { 1 } ; / / E r ro r : l va l ue needed .7 const double& cdr { 3 } ; / / OK8 }

    Now close the book and explain the difference of these two lines.

    25. Try this:

    1 #include 23 using namespace std ;45 i n t& func t i on3 ( ) { return 14 ; }6 const in t& func t i on4 ( ) { return 15 ; }78 i n t main ( ) {9 i n t var1 = func t i on4 ( ) ;

    10 cout

  • 26. (*) Check the following code (class object was introduced in chapter 3 - dont get panic). The purpose is toshow how move (rvalue) and copy (lvalue) differs.

    1 #include 2 #include 34 using namespace std ;56 class Obj {7 public :8 Obj ( const in t& data =0) : data_ ( data ) { cout

  • (b)1 i n t main ( ) {2 Obj A1(12) ;3 Obj A2(A1) ;4 }

    (c)1 i n t main ( ) {2 Obj A1( Obj (13) ) ;3 }

    (d)1 i n t main ( ) {2 vector V;3 V. push_back ( Obj ( 1 ) ) ;4 V . push_back ( Obj ( 2 ) ) ;5 }

    (e)1 i n t main ( ) {2 vector V;3 V. reserve ( 5 ) ;4 V . push_back ( Obj ( 1 ) ) ;5 V . push_back ( Obj ( 2 ) ) ;6 }

    Explain the different behaviour of point d and e.