4 CP 9 / 4 IT 7 – OOP Lab

C++ Programs

1.    Programs Inheritance

2.    Programs of Operator Overloading (Complex Number Arithmetic, Polar Coordinates)

3.    Programs using Friend Functions

4.    Various Matrices Operation

5.    Stack Operations using OOPs Concepts

6.    To Implement Tower of Hanoi Problem

 

LAB MANUAL

 

Program 1: A sample C++ Program

 

Text Box: // Filename	:	program01.cpp
// Purpose	:	A Simple C++ Program
#include <iostream>
using namespace std;
int main()
{
	cout<< "Hello! What is your age: ";
	int age;
	cin >> age;
	cout<< "Welcome, you are "<<age<<" years old."<<endl;
	system("pause");
	return 0;
}

The first two lines in the programs are

// Filename  :   program01.cpp

// Purpose   :   A Simple C++ Program

These lines begin with // (double forward slash), these are line comments.
C++ support two type of comments: -

Block comments – Traditional C style comments
/*anything within this pair of forward slash, asterisk and asterisk, forward slash is a comment, this may be a part of the line or may be multiline*/

Line comments – C++ style comments
// anything on this line after a pair of forward slash up to end of line is a comment.
// If we need more than one line, every line must begin with double forward slash

The third line is

#include <iostream>

It begins with # (hash or pond), it is preprocessor directive. Any line that begins with a # is a preprocessor directive that instructs the preprocessor to follow the instruction given by the following command (include in this case).
This is same as in C and copies the content of the file "iostream" at this place.

The file included here is iostream. Note that this does not have ".h" extension. In fact, it is standard C++ style header file. (Traditional style headers were having .h extension like iostream.h).  This header supports C++ style I/O operations.

Caution!!!  Turbo C++ 3.0 IDE does not support Standard C++, it supports only Traditional C++. To use all features on Standard C++, you may use any new compiler and IDE. Turbo C++ 4.5, Borland C++ 5.0, Microsoft Visual C++, or Developer C++ 5 may work fine. A support for Standard C++ is inbuilt in most of Linux distributions. On windows, I recommend to install Developer C++ 5 (4.9.9.1) which may be obtained freely from Internet or over windows share //campus/public/software (preferred in campus).

The next line in the program is

using namespace std;

This tells the compiler to use the std (standard) namespace. Namespaces are a recent addition to C++. A namespace creates a declarative region in which various program elements can be placed. Namespaces help in the organization of large programs.

The using statement informs the compiler that you want to use the std namespace. This is the namespace in which the entire C++ library is declared. By using the std namespace, you simply use the standard library.

Now examine the following line

int main()

This line simply tells that the function main takes no parameter and returns an integer value. In C++, an empty argument list like main ( ) simply declares that the function take no parameter, the use of void like main (void) is redundant and unnecessary.

As far as the return value is concerned, you may not use void main ( ), as standard C++ does not permit the return type of main to be void. The main function must return an integer value.

Note: This is in contrast to tradition C/C++, where you may have used void as the preferred type of return value for main.

The {, opening brace and }, closing brace enclose the body of the main as in C.

The next line is an output statement

cout << "Hello! What is your age: ";

This line causes the string "Hello! What is your age: " to be printed over output device (monitor). In C++, cout (pronounced as see out) is an object of ostream class and << is the insertion operator (in addition to the existing role of left shift operator since C). The insertion operator inserts the data on its right in the output stream through an object of ostream class (cout in this case), which flows from CPU to output device (monitor by default).

The next line is

int age;

This is variable declaration like C. But, not is the beginning, Hum! C++ allows the declaration of variables anywhere in the program in contrast to C, which allows the declaration of variables only in declaration section before any executable statement.

The next line is

cin >> age;

This line reads an integer value from key board and stores it in the variable age. In C++, cin (pronounced as see in) is an object of istream class and >> is the extraction operator (in addition to the existing role of right shift operator since C). The extraction operator extracts the data from input stream through an object of istream class (cin in this case), which flows from input device (key board by default) to the CPU and stores it in the variable or object on its right.

The next line is

cout<< "Welcome, you are "<<age<<" years old."<<endl;

This is same as the previous output statement but here, multiple << (insertion operators) are used to insert multiple data items of different types into a single object (cout). This is called cascading. Both insertion & extraction operators may be cascaded in this way.

The last word endl is an output manipulator which is used to cause linefeed in C++.

The next line is

system ("pause");

This is same as in C, calls system function (operating system) with "pause" as parameter, which is a DOS command ported in Windows 9x/NT series and causes the display to halt (pause) until a key is pressed. It also prompts a message "Press a key to continue…"

Caution!!! Do not use clrscr( ) & getch( ), these are exclusive Borland implementations and are not standard, hence not portable. Instead, use system ("cls") and system ("pause") on windows and system ("clear") and system ("pause") on Linux.

The last line is

return 0;

This return backs the control to the operating system and passes an integer 0, indicating that there was ZERO ERROR during execution.

The output on execution of this program is

Hello! What is your age:

You are expected to input an integer value here e.g. 17

Now the program reads the value typed and proceeds further to display

Welcome, you are 17 years old.

Press any key to continue . . .

 

Home of Learning Center                             Index of Lab Manual                      Next Program