C++ Proposed Exercises (Chapter 7: The C++ Programing Language, Fourth Edition)
C++ Proposed Exercises (Chapter 7: The C++ Programing Language, Fourth Edition)
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 the
proposed 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
B. Array
7. Implement an array (3 elements) of pointers to int and initialize them with value: 1, 2, 3. Use for loop to print
the value in array. Remember to deallocate memory appropriately (Hint: use delete).
8. Repeat exercise 7 with int (not pointers).
9. Implement an array (10 elements) of pointer to char and initialice them with valur: a,b,c,d,e,f,.... . Use for
loop to print the value in array. Remember to deallocate memory appropriately (Hint: use delete).
10. Repeat exercise 9 with char (not pointers).
1 2 3
A= 7 8 9
4 5 6
15. (*)Define a function Addition with:
(a) Two input two dimensional matrix of the same size.
(b) Return a two dimensional matrix equal to de addition operations of the two matrix provided.
:
:
:
:
:
Identify visually any possible error. If does, gives an explanation. Next, implement the code in your IDE and
compare results (Note: This is important).
/ / E r r o r : l v a l u e needed .
/ / OK
Now close the book and explain the difference of these two lines.
25. Try this:
1
2
3
4
5
6
7
8
9
10
11
26. (*) Check the following code (class object was introduced in chapter 3 - dont get panic). The purpose is to
show how move (rvalue) and copy (lvalue) differs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
The following code correspond to the main section. Try to identify which member function: constructor, copy
constructor, move constructor, copy Assignment or move assignment is invoked.
(a)
1 i n t main ( ) {
2
Obj A1 ( 1 2 ) ;
3 }
(b)
1 i n t main ( ) {
2
Obj A1 ( 1 2 ) ;
3
Obj A2 ( A1 ) ;
4 }
(c)
1 i n t main ( ) {
2
Obj A1 ( Obj ( 1 3 ) ) ;
3 }
(d)
1 i n t main ( ) {
2
v e c t o r <Obj > V ;
3
V . push_back ( Obj ( 1 ) ) ;
4
V . push_back ( Obj ( 2 ) ) ;
5 }
(e)
1 i n t main ( ) {
2
v e c t o r <Obj > V ;
3
V. reserve ( 5 ) ;
4
V . push_back ( Obj ( 1 ) ) ;
5
V . push_back ( Obj ( 2 ) ) ;
6 }