c++ exercises chapter 2 (solution)

11
 Chapter 2 Solutions A. Getting Familiar with the IDE and Compiler 1. Iden tify ho w to add:  . h;  .cpp file. Step’s to elaborate C++ program in Xcode (6.3.2): Open Xcod e. Create a new Xcode project (As an alternativ e: File/New /Proy ect) Select Command Line T ool Den e the projec t name.

Upload: mauricio-bedoya

Post on 05-Nov-2015

19 views

Category:

Documents


0 download

DESCRIPTION

.

TRANSCRIPT

  • Chapter 2 Solutions

    A. Getting Familiar with the IDE and Compiler

    1. Identify how to add: .h; .cpp file. Steps to elaborate C++ program in Xcode (6.3.2):

    Open Xcode.

    Create a new Xcode project (As an alternative: File/New/Proyect)

    Select Command Line Tool

    Define the project name.

  • Select the location to save the project and click create.

    The output of thre previous step is an environment that allow us to program in c++.

    Hello world example is implemented by default in the main.cpp file. To add a new .cpp(.h) file (or existingfiles) select main.cpp and click file/new/file. By default is selected Source. Next select the file extensionneed it and thats all.

    2. Differentiate between .h and .hppSearch in google.

    3. The code of MyHeader.cpp using lstlisting latex package is:

    1 i n t data1 { 2 3 } ;2 i n t data2 { 1 2 } ;3 i n t data3 {data1+data2 } ;4

    5 i n t main ( i n t argc , const char * argv [ ] ) {6

    7 }

  • Once you run the program, nothing happens. To identify the output view section and variable view section,click in view/Debug Area/Show Debug Area. In the next image, I will stop debugging in the line (3) data3definition. Left to the definition of each variable, there is a white space, just click there and run the code.

    The compilation will stop in that line and in the output variable view section you can identify the variablesdefined in that specific scope.

    4. The code is:

    1 # inc lude 2

    3 i n t data1 { 2 3 } ;4 i n t data2 { 1 2 } ;5 i n t data3 {data1+data2 } ;6

    7 i n t main ( i n t argc , const char * argv [ ] ) {8 std : : cout

  • 1 # inc lude 2

    3 i n t data1 { 2 3 } ;4 i n t data2 { 1 2 } ;5 i n t data3 {data1+data2 } ;6

    7 using namespace std ;8

    9 i n t main ( i n t argc , const char * argv [ ] ) {10 cout

  • 7. In this case we have two possibilities: Define the integers to add as global or in the main scope.

    1 # inc lude 2

    3 using std : : cout ;4 using std : : endl ;5

    6 i n t Addition ( const i n t & data1 , const i n t & data2 )7 {8 r e t u r n (data1 + data2 ) ;9 }

    10

    11 i n t a { 12 } , b { 4 } ;12

    13 i n t main ( i n t argc , const char * argv [ ] ) {14 i n t Add {Addition (a ,b ) } ;15 }

    1 # inc lude 2

    3 using std : : cout ;4 using std : : endl ;5

    6 i n t Addition ( const i n t & data1 , const i n t & data2 )7 {8 r e t u r n (data1 + data2 ) ;9 }

    10

    11 i n t main ( i n t argc , const char * argv [ ] ) {12 i n t a { 12 } , b { 4 } ;13 i n t Add {Addition (a ,b ) } ;14 }

    Get familiar with the scope definition. Previous two implementations arent equivalent.

    8. A function is a type, in the case of Addition an integer type with two int inputs. Then the following code willfail to compile.

    1 i n t Addition ( const i n t & data1 , const i n t & data2 )2 {3 r e t u r n (data1 + data2 ) ;4 }5

    6 i n t main ( i n t argc , const char * argv [ ] ) {7 i n t a { 12 } , b { 4 } ;8

    9 i n t Addition MyFunction (12 ,4 ) ; / / E r ro r here10

    11 }

    However, this will compile.

  • 1 # inc lude 2

    3 i n t Addition ( const i n t & data1 , const i n t & data2 )4 {5 r e t u r n (data1 + data2 ) ;6 }7

    8 i n t main ( i n t argc , const char * argv [ ] ) {9 i n t a { 12 } , b { 4 } ;

    10

    11 typedef i n t Addition ( const i n t & data1 , const i n t & data2 ) ; / / typedef o f f u n c t i o n12

    13 Addition Add1 ;14 i n t A {Add1 (12 ,4 ) } ;15 }

    9. Printing Addition

    1 # inc lude 2 using std : : cout ;3 using std : : endl ;4

    5 i n t Addition ( const i n t & data1 , const i n t & data2 )6 {7 r e t u r n (data1 + data2 ) ;8 }9

    10 i n t main ( i n t argc , const char * argv [ ] ) {11 i n t a { 12 } , b { 4 } ;12

    13 cout

  • 19

    20 cout

  • 12. Struct members are defined by default public. Class members are defined by default private. This is atricky interview question, so dont forget it.

    13. The code is:

    1 # inc lude 2 using std : : cout ;3 using std : : endl ;4

    5 s t r u c t My_Struct {6 i n t a_ , b_ ;7 My_Struct ( const i n t & a , const i n t & b ) :a_ (a ) ,b_ (b ) { }8 ~My_Struct ( ) { }9 i n t Addition ( ) {

    10 r e t u r n (a_ + b_ ) ;11 }12 } ;13

    14 i n t main ( i n t argc , const char * argv [ ] ) {15 i n t a { 12 } , b { 4 } ;16 My_Struct Add (a ,b ) ;17

    18 cout

  • 15. Implement only the class (you practice with the struct)

    1 # i f n d e f __Chapter2__MyClass__2 # def ine __Chapter2__MyClass__3

    4 # inc lude < s t d i o . h>5

    6 c lass MyClass {7 p r i v a t e :8 i n t data1_ , data2_ ;9

    10 p u b l i c :11 MyClass ( const i n t & data1 , const i n t & data2 ) ;12 ~MyClass ( ) ;13 i n t Addition ( ) const ;14 } ;15

    16 # end i f / * def ined ( __Chapter2__MyClass__ ) * /

    MyC lass.h

    1 # inc lude " MyClass . h "2

    3 MyClass : : MyClass ( const i n t & data1 , const i n t & data2 ) :data1_ (data1 ) ,data2_ (data2 ) { }4 MyClass : : ~MyClass ( ) { }5 i n t MyClass : : Addition ( ) const {6 r e t u r n (data1_ + data2_ ) ;7 }

    MyC lass.cpp

    1 # inc lude 2 # inc lude " MyClass . h "3

    4 using std : : cout ;5 using std : : endl ;6

    7 i n t main ( i n t argc , const char * argv [ ] ) {8 i n t a { 12 } , b { 4 } ;9 MyClass Add (a ,b ) ;

    10

    11 cout

  • C. Pointers

    16. Two possibilities

    1 # inc lude 2

    3 using std : : cout ;4 using std : : endl ;5

    6 i n t main ( i n t argc , const char * argv [ ] ) {7

    8 / / Option 19 i n t data1 { 1 2 } ;

    10 i n t *p_data1 {new i n t {data1 } } ;11

    12 cout

  • 18. In this case, reader will find the struct definition and implementation in one .cpp file. Use exercise solution15 as an example and separate the definition and implementation.

    1 using std : : cout ;2 using std : : endl ;3

    4 s t r u c t MyClass {5 p r i v a t e :6 i n t data1_ , data2_ ;7

    8 p u b l i c :9 MyClass ( const i n t & data1 , const i n t & data2 ) :data1_ (data1 ) ,data2_ (data2 ) { }

    10 ~MyClass ( ) { }11 i n t Addition ( ) const {12 r e t u r n (data1_ + data2_ ) ;13 }14 } ;15

    16 i n t main ( i n t argc , const char * argv [ ] ) {17 i n t *a {new i n t { 1 2 } } ;18 i n t *b {new i n t { 4 } } ;19 MyClass *Add (new MyClass ( *a , *b ) ) ;20

    21 cout