Exercise 3rd Chap
Exercise 3rd Chap
UNIT 3:
OBJECT ORIENTED
PROGRAMMING IN C++
EXERCISE NOTES
1
Unit 3 OBJECT ORIENTED PROGRAMMING IN C++
i. Why reserved words cannot be used as variable names? Give three examples of
reserved word.
Answer
Reserved words are special words, which are reserved by a programming language for
specific purpose in program. These cannot be used as a variable names. All the reserved
words are written in lower-case letters. There are about 80 reserved words in C++ but this
may vary depending on the version being used.
Examples
Answer
Information that is needed by several different files or functions is collected into a header
file. A header file contains C++ definitions and structures. Centralizing information into a
header file facilitates the creation and update of programs. When we want to use any
function in our C++ program then first we need to import their definition from C++ Iibrary.
For importing then declaration and definition, we need to include header file in program by
using #include. Header file include at the top of any C++ program.
Example:
#include<iostream.h>
int main()
return 0;
}
2
Unit 3 OBJECT ORIENTED PROGRAMMING IN C++
In above program, print message on screen “Hello world!" by using cout but we do not
define cout here. Actually already cout has been declared in a header file called iostream.
iii. State whether the following variable names are valid or invalid. State the reason
for invalid variable names.
Answer
a123 valid --
_abcd valid --
f3ss1 valid --
net_weight valid --
iv. Why escape sequence is used? Give three examples with explanation.
Answer
Escape Sequences
Escape sequences are special! characters used to control the output look on output
devices. These characters are not printed. These are used inside the output statement.
Examples
3
Unit 3 OBJECT ORIENTED PROGRAMMING IN C++
cout<< “Windows operating system.”;
When the above two statements are executed the output will appear on a single line as
shown below:
2) If it is desired to display the output in two lines then \n escape sequence can be used
in various ways to move cursor to the beginning of next line.
The following two statements can also obtain the same output:
3) The \t escape sequence can also be used to tab over eight characters as shown in the
following statement:
Answer
The difference between relational operators and logical operators is that relational
operators can have any operand values and return Boolean, while logical always have
Boolean operands and return a Boolean. Relational operators compare two values and
produce a Boolean result.
4
Unit 3 OBJECT ORIENTED PROGRAMMING IN C++
vi. What will be the output of the following statements?
a) 3+4*5
3+20
23
b) 4*5/10+8
20/10+8
2+8
10
c) 3*(2+7*4)
3*(2+28)
3*30
90
d) 20-2/6+3
20-0+3
20+3
23
e) (20-2)/(6+3)
18/9
2
f) 25%7
4
5
Unit 3 OBJECT ORIENTED PROGRAMMING IN C++
vii. Evaluate the following expressions that have integer and floating-point data
types.
a) 10.0+15/2+4.3
10.0+7+4.3
17.0+4.3
21.3
b) 10.0+15.0/2+4.3
10.0+7.5+4.3
17.5+4.3
21.8
c) 4/6*3.0+6
0*3.0+6
0+6
6
#include<iostream.h>
#include<conio.h>
void main( )
int num1,num2,total;
num1=13;
num2=20;
total=num1+num2; .
Output
#include<iostream.h>
#include<conio.h>
void main( )
n=10;
n++;
n++;n++;
n--;
getch( );
Output
LONG QUESTIONS:
Q1. Define variable and write the rules for specifying variable names. (Refer NBF
book Page no. 45)
Q2. Why type casting is used? Explain the types of type casting with an example of
each type. (Refer NBF book Page no. 47 to 48)
Q3. Define endl and setw manipulators and give an example of each. (Refer NBF
book Page no. 54 to 56)
Q4. What is meant by precedence of operators? Write the operators with the highest
precedence at the top and the lowest at the bottom. (Refer NBF book Page no. 64 to
65)
LAB ACTIVITIES:
1. Write a program that reads four integers and prints their sum, product and
average.
Answer
#include <iostream.h>
int main()
8
Unit 3 OBJECT ORIENTED PROGRAMMING IN C++
cout << “"Sum of four numbers is = " << num1 + num2 + num3 + num4<<endl;
cout << "Product of four numbers is = " << num1 * num2 * num3 * num4<<endl;
cout << "Average of four numbers is = " << (num1 + num2 + num3 + num4)/ 4<<endl;
return 0;
ii. Write a program that reads length and breadth of a rectangle and prints its area.
Answer
#include <iostream.h>
int main( )
return 0;
iv. Write a program that reads temperature in Fahrenheit and prints its equivalent
temperature in Celsius using the following formula.
9
Unit 3 OBJECT ORIENTED PROGRAMMING IN C++
c = 5/9(f-32)
Answer
#include <iostream.h>
int main()
return 0;
10