Você está na página 1de 3

C++ Excercises - Chapter 2

In this document you can find exercises related to the content of C++ Programming Language (Fourth Edition)
written by Bjarne Stroustrup. Solution is provided. However, if you follow the book carefully, you will be able to
solve the proposed problems easily.
Dont get panic If you have difficulties trying to solve these exercises. The material provided in chapter 2 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. Major two task are: reading and programing in C++.

A. Getting Familiar with the IDE and Compiler


1. Identify how to add: .h; .cpp file.
2. Differentiate between .h and .hpp
3. Create MyHeader.cpp file; and try to implement the following calculations in main without including any
other header file.
1
2
3
4

i n t main ( )
{
...
}

(a) Initialize two int data member: int data1, int data2.
(b) Make int data3 = data1 + data2.
(c) Identify how to run and compile (any play button)
(d) Identify how to stop debugging with your IDE.
(e) Identify the variable view section.
(f) Identify the output view section.
4. Repeat exercise 3, but now include the header file <iostream> and use std::cout to print the value of data3.
5. (*)Identify in you compiler where to modify header search path, library search path, linkers, preprocessor.

B. My First Function
My first program (Dont get panic or frustrated, follow the recommendations). In the following lines the same
procedure will be implemented in different ways. For this purpose include a .cpp file.
6. Create a function (outside main) called Addition, that return the sum of two int. Compile and see what
happen.

1
2
3
4
5
6
7
8

/ / A d d i t i o n f u n c t i o n here
/ / A d d i t i o n c a l l here
i n t main ( )
{
...
}

7. Repeat eecercise 6, but this time make a call of Addition inside main:
1
2
3
4
5
6
7

/ / A d d i t i o n f u n c t i o n here
i n t main ( )
{
/ / A d d i t i o n c a l l here
...
}

8. Can you rename the function in main ? (Hint: Does the following work: Addition MyFunction(12,4)).
9. Repeat exercise 6, but now print the result (Hint: include the header file < iostream > and use (std :: cout)).
10. In exercise 6 you should have something like this:
1
2
3
4
5
6
7
8
9

# include < iostream >


i n t A d d i t i o n ( i n t data1 , i n t data2 ) { r e t u r n ( data1 + data2 ) ; }
i n t main ( )
{
/ / A d d i t i o n c a l l here
...
}

Use a struct to initialize data1 and data2. (Hint: Create a struct named data that contains two int members. Initialize in main and set the two int member. At the end, you should have something like this:
Addition(MyData)).
11. (*)Repeat exercise 10 but now instead of struct use class. (If you have some problems, take a look at the
end of page 48.)
12. (*)Exercise 11 allow to identify the primary difference between a class and a struct. Say it loudly.
13. Make a struct that contains:
(a) Two int members.
(b) A constructor that initialize the previous two int.
(c) A member function called Addition that return the sum of the two int.
(d) Can you rename the struct in main? (Hint: Lets say that the struct name is My_Struct. Does this
work: My_Struct Add(12,4)? )

14. (*) Repeat exercise 13 with a class.


My_Class
x:int
y:int
My_Class(int,int):My_Class
Addition():int

Figure 1: UML Class Representation


15. (*) Repeat exercise 13 and 14 but separate the definition (.h) and implementation (.cpp). Now add another
.cpp file and implement in main the two versions of Addition (struct and class one).

C. Pointers
16. Create an integer pointer and initialize with a value of 35. Print the memory address and value (Hint: include
the header file < iostream > and use (std :: cout)).
17. Create a int pointer and assign the memory address of the previous pointer.
18. Create two integer pointers and My_struct (exercise 13) pointer. Initialize My_struct with the previous two
integer pointers and print the memory address and Addition result of My_struct.
19. Create and initialize a char pointer. Next, increase the address value by any number and print the memory
address and the value associated to that address.
Observation!!!
Create a file called Chapter 2 and save the proposed solution. As you continue reading the book you will learn a
lot of new stuffs, that will allow you to improve your previous solutions. In case of any doubt feel free to contact
Mauricio Bedoya at: javierma36@hotmail.com.

Você também pode gostar