C++ Exercises Chapter 2 (Solution)
C++ Exercises Chapter 2 (Solution)
C++ Exercises Chapter 2 (Solution)
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.
i n t data1 { 2 3 } ;
i n t data2 { 1 2 } ;
i n t data3 { data1+data2 } ;
4
5
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.
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
In this case Im using the scope resolution operator to search in the namespace std the function cout and
endl. Alternatives are:
1
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
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.
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
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
2
3
4
5
6
7
8
i n t Add { Addition ( 1 2 , 4 ) } ;
9
10
11
12
2
3
4
5
6
7
8
9
i n t a{12} , b { 4 } ;
i n t Add { Addition ( a , b ) } ;
10
11
12
13
7. In this case we have two possibilities: Define the integers to add as global or in the main scope.
1
2
3
4
u s i n g std : : cout ;
u s i n g std : : endl ;
5
6
7
8
9
10
11
i n t a{12} , b { 4 } ;
12
13
14
15
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
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 will
fail to compile.
1
2
3
4
5
6
7
i n t Addition MyFunction ( 1 2 , 4 ) ;
9
10
11
/ / E r r o r here
2
3
4
5
6
7
8
9
10
11
12
Addition Add1 ;
i n t A { Add1 ( 1 2 , 4 ) } ;
13
14
15
9. Printing Addition
1
2
3
4
5
6
7
8
9
10
11
12
cout << " A d d i t i o n o f " << a << " and " << b << " i s : " << Addition ( a , b ) ;
13
14
4
5
6
7
s t r u c t data {
int a, b;
};
8
9
10
11
12
13
14
15
16
17
18
19
cout << " A d d i t i o n o f " << a << " and " << b << " i s : " << Addition ( My_data ) ;
20
21
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
15
16
17
18
19
cout << " A d d i t i o n o f " << a << " and " << b << " i s : " << Addition ( My_data ) ;
20
21
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
19
20
21
22
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
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
17
cout << " A d d i t i o n o f " << a << " and " << b << " i s : " << Add . Addition ( ) ;
18
19
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
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
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
3
4
5
u s i n g std : : cout ;
u s i n g std : : endl ;
6
7
8
9
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
2
3
4
u s i n g std : : cout ;
u s i n g std : : endl ;
5
6
/ / Option 1
i n t data1 { 1 2 } ;
i n t * p_data1 { new i n t { data1 } } ;
8
9
10
11
12
13
14
/ / Option 2
i n t * p_data2 { new i n t { 1 5 } } ;
15
16
17
18
19
20
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
<<
<<
<<
<<
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
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
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
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.