Vous êtes sur la page 1sur 11

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 existing
files) select main.cpp and click file/new/file. By default is selected Source. Next select the file extension
need it and thats all.

2. Differentiate between .h and .hpp


Search in google.
3. The code of MyHeader.cpp using lstlisting latex package is:
1
2
3

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

4
5

i n t main ( i n t argc , c o n s t 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) data3
definition. 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 variables
defined in that specific scope.

4. The code is:


1

# i n c l u d e < iostream >

2
3
4
5

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

6
7
8
9

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


std : : cout << " data3 v a l u e i s : " << data3 << std : : endl ;
}

In this case Im using the scope resolution operator to search in the namespace std the function cout and
endl. Alternatives are:
1

# i n c l u d e < iostream >

2
3
4
5

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

6
7
8

u s i n g std : : cout ;
u s i n g std : : endl ;

9
10
11
12

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


cout << " data3 v a l u e i s : " << data3 << endl ;
}

The previous implementation use (using) only cout and endl members defined in namespace std. The last
example will include the whole content of namespace std.

# i n c l u d e < iostream >

2
3
4
5

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

6
7

u s i n g namespace std ;

8
9
10
11

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


cout << " data3 v a l u e i s : " << data3 << endl ;
}

5. To personalize header and library search path, preprocessor, linkers and more; select the name of the
project, there you can find the Build Setting (Headers, preprocessor, warning messages, etc); Build Phases
(Link to Dynamic or static Libraries), and more. For example, if you want to use Boost Library, you need to
specify the header search path and library search path.

B. My First Function
6. The same implementation in different ways.
1

# i n c l u d e < iostream >

2
3
4
5
6

i n t Addition ( c o n s t i n t & data1 , c o n s t i n t & data2 )


{
r e t u r n ( data1 + data2 ) ;
}

7
8

i n t Add { Addition ( 1 2 , 4 ) } ;

9
10

i n t main ( i n t argc , c o n s t char * argv [ ] ) {

11
12

# i n c l u d e < iostream >

2
3
4
5
6

i n t Addition ( c o n s t i n t & data1 , c o n s t i n t & data2 )


{
r e t u r n ( data1 + data2 ) ;
}

7
8
9

i n t a{12} , b { 4 } ;
i n t Add { Addition ( a , b ) } ;

10
11

i n t main ( i n t argc , c o n s t char * argv [ ] ) {

12
13

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

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6
7
8
9

i n t Addition ( c o n s t i n t & data1 , c o n s t i n t & data2 )


{
r e t u r n ( data1 + data2 ) ;
}

10
11

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

12
13
14
15

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t Add { Addition ( a , b ) } ;
}

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6
7
8
9

i n t Addition ( c o n s t i n t & data1 , c o n s t i n t & data2 )


{
r e t u r n ( data1 + data2 ) ;
}

10
11
12
13
14

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t a{12} , b { 4 } ;
i n t Add { Addition ( a , b ) } ;
}

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 will
fail to compile.
1
2
3
4

i n t Addition ( c o n s t i n t & data1 , c o n s t i n t & data2 )


{
r e t u r n ( data1 + data2 ) ;
}

5
6
7

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


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

i n t Addition MyFunction ( 1 2 , 4 ) ;

9
10
11

However, this will compile.

/ / E r r o r here

# i n c l u d e < iostream >

2
3
4
5
6

i n t Addition ( c o n s t i n t & data1 , c o n s t i n t & data2 )


{
r e t u r n ( data1 + data2 ) ;
}

7
8
9

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


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

10

t y p e d e f i n t Addition ( c o n s t i n t & data1 , c o n s t i n t & data2 ) ; / / t y p e d e f o f f u n c t i o n

11
12

Addition Add1 ;
i n t A { Add1 ( 1 2 , 4 ) } ;

13
14
15

9. Printing Addition

1
2
3

# i n c l u d e < iostream >


u s i n g std : : cout ;
u s i n g std : : endl ;

4
5
6
7
8

i n t Addition ( c o n s t i n t & data1 , c o n s t i n t & data2 )


{
r e t u r n ( data1 + data2 ) ;
}

9
10
11

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


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

12

cout << " A d d i t i o n o f " << a << " and " << b << " i s : " << Addition ( a , b ) ;

13
14

Try to make a call of Add1.


10. This exercise can be implemented in different ways. I will show 2 of them:
Option 1
1
2
3

# i n c l u d e < iostream >


u s i n g std : : cout ;
u s i n g std : : endl ;

4
5
6
7

s t r u c t data {
int a, b;
};

8
9
10
11
12

i n t Addition ( c o n s t data& my_data )


{
r e t u r n ( my_data . a + my_data . b ) ;
}

13
14
15
16
17
18

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t a{12} , b { 4 } ;
data My_data ;
My_data . a = a ;
My_data . b = b ;

19

cout << " A d d i t i o n o f " << a << " and " << b << " i s : " << Addition ( My_data ) ;

20
21

In the previous implementation identify that a is defined in two different scopes.


Option 2
1
2
3

# i n c l u d e < iostream >


u s i n g std : : cout ;
u s i n g std : : endl ;

4
5
6
7
8
9

s t r u c t data {
int a, b;
data ( c o n s t i n t & data1 , c o n s t i n t & data2 ) : a ( data1 ) , b ( data2 ) { }
~data ( ) { }
};

10
11
12
13
14

i n t Addition ( c o n s t data& my_data )


{
r e t u r n ( my_data . a + my_data . b ) ;
}

15
16
17
18

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t a{12} , b { 4 } ;
data My_data ( a , b ) ;

19

cout << " A d d i t i o n o f " << a << " and " << b << " i s : " << Addition ( My_data ) ;

20
21

11. In the case of a class:


1
2
3

# i n c l u d e < iostream >


u s i n g std : : cout ;
u s i n g std : : endl ;

4
5
6
7
8
9
10
11
12
13

c l a s s data {
private :
int a, b;
public :
data ( c o n s t i n t & data1 , c o n s t i n t & data2 ) : a ( data1 ) , b ( data2 ) { }
~data ( ) { }
i n t get_a ( ) c o n s t { r e t u r n a ; }
i n t get_b ( ) c o n s t { r e t u r n b ; }
};

14
15
16
17
18

i n t Addition ( c o n s t data& my_data )


{
r e t u r n ( my_data . get_a ( ) + my_data . get_b ( ) ) ;
}

19
20
21
22

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t a{12} , b { 4 } ;
data My_data ( a , b ) ;

23

cout << " A d d i t i o n o f " << a << " and " << b << " i s : " << Addition ( My_data ) ;

24
25

Line 6 is redundant, but I like to identify visually the characteristics of each section of a class.

12. Struct members are defined by default public. Class members are defined by default private. This is a
tricky interview question, so dont forget it.
13. The code is:
1
2
3

# i n c l u d e < iostream >


u s i n g std : : cout ;
u s i n g std : : endl ;

4
5
6
7
8
9
10
11
12

s t r u c t My_Struct {
i n t a_ , b_ ;
My_Struct ( c o n s t i n t & a , c o n s t i n t & b ) : a_ ( a ) , b_ ( b ) { }
~My_Struct ( ) { }
i n t Addition ( ) {
r e t u r n ( a_ + b_ ) ;
}
};

13
14
15
16

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t a{12} , b { 4 } ;
My_Struct Add ( a , b ) ;

17

cout << " A d d i t i o n o f " << a << " and " << b << " i s : " << Add . Addition ( ) ;

18
19

14. The code is:


1
2
3

# i n c l u d e < iostream >


u s i n g std : : cout ;
u s i n g std : : endl ;

4
5
6
7
8
9
10
11
12
13
14

c l a s s My_Class {
private :
i n t data1_ , data2_ ;
public :
My_Class ( c o n s t i n t & data1 , c o n s t i n t & data2 ) : data1_ ( data1 ) , data2_ ( data2 ) { }
~My_Class ( ) { }
i n t Addition ( ) {
r e t u r n ( data1_ + data2_ ) ;
}
};

15
16
17
18
19

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t a{12} , b { 4 } ;
My_Class Add ( a , b ) ;

20

cout << " A d d i t i o n o f " << a << " and " << b << " i s : " << Add . Addition ( ) ;

21
22

Before continue, identify the difference between a class and a struct. In exercise 13 I use a_ and b_ (avoid
using generic letters), but in exercise 14 I use data1_ and data2_, in correspondence of adding two int
data. The extra line at the end of each variable allow me to differentiate between member variables and
initializers.

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

# i f n d e f __Chapter2__MyClass__
# d e f i n e __Chapter2__MyClass__

3
4

# i n c l u d e < s t d i o . h>

5
6
7
8

c l a s s MyClass {
private :
i n t data1_ , data2_ ;

9
10
11
12
13
14

public :
MyClass ( c o n s t i n t & data1 , c o n s t i n t & data2 ) ;
~MyClass ( ) ;
i n t Addition ( ) c o n s t ;
};

15
16

# e n d i f / * d e f i n e d ( __Chapter2__MyClass__ ) * /

M yC lass.h

# i n c l u d e " MyClass . h "

2
3
4
5
6
7

MyClass : : MyClass ( c o n s t i n t & data1 , c o n s t i n t & data2 ) : data1_ ( data1 ) , data2_ ( data2 ) { }
MyClass : : ~ MyClass ( ) { }
i n t MyClass : : Addition ( ) c o n s t {
r e t u r n ( data1_ + data2_ ) ;
}

M yC lass.cpp

1
2

# i n c l u d e < iostream >


# i n c l u d e " MyClass . h "

3
4
5

u s i n g std : : cout ;
u s i n g std : : endl ;

6
7
8
9

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t a{12} , b { 4 } ;
MyClass Add ( a , b ) ;

10

cout << " A d d i t i o n o f " << a << " and " << b << " i s : " << Add . Addition ( ) ;

11
12

T ext.cpp

C. Pointers
16. Two possibilities
1

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6

i n t main ( i n t argc , c o n s t char * argv [ ] ) {

/ / Option 1
i n t data1 { 1 2 } ;
i n t * p_data1 { new i n t { data1 } } ;

8
9
10
11

cout << " Memory : " <<p_data1 << endl ;


cout << " Value : " << * p_data1 << endl ;

12
13
14

/ / Option 2
i n t * p_data2 { new i n t { 1 5 } } ;

15
16
17

cout << " Memory : " <<p_data2 << endl ;


cout << " Value : " << * p_data2 << endl ;

18
19
20

17. The code.


1
2
3

# i n c l u d e < iostream >


u s i n g std : : cout ;
u s i n g std : : endl ;

i n t data1 { 1 2 } ;
i n t * p_data1 { new i n t { data1 } } ;

5
6
7

i n t * p_data2 { p_data1 } ;

8
9

cout
cout
cout
cout

10
11
12
13
14

<<
<<
<<
<<

" Memory p_data1 : " <<p_data1 << endl ;


" Value p_data1 : " << * p_data1 << endl ;
" Memory p_data2 : " <<p_data2 << endl ;
" Value p_data2 : " << * p_data2 << endl ;

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

u s i n g std : : cout ;
u s i n g std : : endl ;

3
4
5
6

s t r u c t MyClass {
private :
i n t data1_ , data2_ ;

7
8
9
10
11
12
13
14

public :
MyClass ( c o n s t i n t & data1 , c o n s t i n t & data2 ) : data1_ ( data1 ) , data2_ ( data2 ) { }
~MyClass ( ) { }
i n t Addition ( ) c o n s t {
r e t u r n ( data1_ + data2_ ) ;
}
};

15
16
17
18
19

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


i n t * a { new i n t { 1 2 } } ;
i n t * b { new i n t { 4 } } ;
MyClass * Add ( new MyClass ( * a , * b ) ) ;

20

cout << " Add Memory : " << Add << " \ n " ;
cout << " A d d i t i o n o f Add o b j e c t : " << Add>Addition ( ) << endl ;

21
22
23

19. The code.


1

# i n c l u d e < iostream >

2
3
4

u s i n g std : : cout ;
u s i n g std : : endl ;

5
6
7
8
9
10
11
12
13

i n t main ( i n t argc , c o n s t char * argv [ ] ) {


char * letter { new char { ' a ' } } ;
cout << " l e t t e r memory address : " << &letter << endl ;
cout << " l e t t e r v a l u e : " << * letter << endl ;
letter++;
cout << " l e t t e r memory address : " << &letter << endl ;
cout << " l e t t e r v a l u e : " << * letter << endl ;
}

Final Comments
Any omission in the proposed solution (Pointer section) ? Deallocate memory appropriately (Hint: use
delete.).
Congratulations, you are one step further of understanding and programming (writing and reading code) in
C++. Redo everything with the multiplication operations, division, modulo, etc.

Vous aimerez peut-être aussi