0% found this document useful (0 votes)
3 views10 pages

Exercise 3rd Chap

This document covers key concepts of Object-Oriented Programming in C++, including reserved words, header files, variable naming rules, escape sequences, and operator types. It provides examples and explanations for various programming constructs, evaluates expressions, and outlines programming exercises. Additionally, it includes long questions and lab activities related to C++ programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Exercise 3rd Chap

This document covers key concepts of Object-Oriented Programming in C++, including reserved words, header files, variable naming rules, escape sequences, and operator types. It provides examples and explanations for various programming constructs, evaluates expressions, and outlines programming exercises. Additionally, it includes long questions and lab activities related to C++ programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Unit 3 OBJECT ORIENTED PROGRAMMING IN C++

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

(i) if (ii) void (iii) break.

ii. What is the purpose of using header file in program?

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>

using namespace std;

int main()

cout « "Hello world” <<endl;

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

Variable name Valid / Invalid Reason

a123 valid --

_abcd valid --

5htm invalid Variable name cannot be started with digit.

tot.al invalid Dot not allowed in variable name.

f3ss1 valid --

c$avg invalid Special character not allowed in variable name.

net_weight valid --

cout invalid Reserved words of C++ are not allowed to be


used as variable name.

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

1) Suppose we want to print the following message on the screen

cout<< “There are many versions of”;

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:

There are many versions of Windows operating system.

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.

cout<<“\nThere are many versions of”;

cout<<“\nWindows operating system.”;

The following two statements can also obtain the same output:

cout<<“\nThere are many versions of\n”;

cout<<“Windows operating system.”,

A single output statement can also be used:

cout<< “\nThere are many versions of\nWindows operating system.”,

3) The \t escape sequence can also be used to tab over eight characters as shown in the
following statement:

Cout<<“C++ \t is \t a\t high \t level \t language"”;

The output of this statement will be

C++ is a high level language.

v. Differentiate between relational and logical operators.

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) cout<<“17/3 is equal to"<<17/3;


17/3 is equal to 6

b) cout<<“10.0/4 is equal to" <<10.0/4;


10.0/4 is equal to is equal to 2.5;

c) cout<<"40/5%3"7 is equal to" << (40/5%3*7);


40/ 5%3*7 is equal to 14;

vii. Evaluate the following integer expressions.

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

ix. What will be the output of the following program?

#include<iostream.h>

#include<conio.h>

void main( )

int num1,num2,total;

num1=13;

num2=20;

total=num1+num2; .

cout<<“The total of "<<num1<<” and "<<num2<<“is "<<total<<endl;


6
Unit 3 OBJECT ORIENTED PROGRAMMING IN C++
getch( );

Output

The total of 13 and 20 is 33

x. What will be the output of the following program?

#include<iostream.h>

#include<conio.h>

void main( )

n=10;

cout<<“The initial value of n is “<<n<<endl;

n++;

cout<<“The value of n is now "<<n<<endl;

n++;n++;

cout<<“The value of n is now "<<n<<endl;

n--;

cout<<"The value of n is now "<<n<cendl;

getch( );

Output

The initial value of n is 10

The value of n is now 11

The value of n is now 13


7
Unit 3 OBJECT ORIENTED PROGRAMMING IN C++
The value of n is now 12

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>

using namespace std;

int main()

int num1, num2, num3, num4;

cout << "Input four integers ";

cin >> num1 >> num2 >> num3 >> num4;

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>

using namespace std;

int main( )

int length, breadth, area;

cout << "Enter length of rectangle “;

cin >> length;

cout << “"Enter breadth of rectangle”;

cin >> breadth;

// Formula to calculate area of Rectangle

area = length * breadth;

cout << "Area of rectangle << area;

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>

using namespace std;

int main()

int ftemp, ctemp;

cout << “"Please enter the temperature in Fahrenheit “;

cin >> ftemp;

ctemp = (ftemp - 32) * 5 / 9;

cout << ctemp << " Temperature in Celsius: \n";

return 0;

10

You might also like