Object-Oriented Programming Lab Manual R
Object-Oriented Programming Lab Manual R
Exercise – 1 (Basics)
Aim: Write a Simple Program on printing “Hello World” and “Hello Name” where name is
the input from the user
Source Code(Hello.cpp)
#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
cout<<"\nHello World\n";
getch();
return 0;
}
Output
Hello World
Aim: Write a simple program for printing “Hello Name” where name is the input from the
user
Source Code(Name.cpp)
#include <iostream.h>
#include <conio.h>
int main()
{
char name[25];
clrscr();
cout<<"\nEnter your name:";
cin>>name;
cout<<"Hello "<<name;
getch();
return 0;
}
Output
Enter your name: Vijay
Hello Vijay
Aim: Write a C++ program to check whether the given number is even or odd.
Source Code (even.cpp)
#include <iostream.h>
#include <conio.h>
int main()
{
int n;
clrscr();
cout << "Enter a number: ";
cin >> n;
if(n%2 == 0)
Output
Enter a number: 13
13 is a positive number
Enter a number: -1
-1 is anegative number
Aim: Write a C++ program to check whether the given number is Armstrong or not.
Source Code (armstrong.cpp)
#include <iostream.h>
#include <conio.h>
int main()
{
int n, temp, rem, sum = 0;
clrscr();
cout << "Enter a positive integer: ";
cin >> n;
temp = n;
while( n != 0)
{
rem = n % 10;
sum = sum +(rem * rem * rem);
n = n / 10;
}
if(temp == sum)
cout << temp << " is an armstrong number";
else
cout << temp << " is not an armstrong number.";
getch();
return 0;
}
Ouput
Enter a positive integer: 153
153 is an armstrong number
Aim: Write a Program to calculate the fare for the passengers traveling in a bus. When a
Passenger enters the bus, the conductor asks “What distance will you travel?” On knowing
distance from passenger (as an approximate integer), the conductor mentions the fare to the
passenger according to following criteria.
Distance (in KMS) Fare (per KM)
0 – 20 65 paisa
21 – 40 75 paisa
41 – 60 78 paisa
61 – 80 80 paisa
81 – 100 95 paisa
101 and above 1.05 paisa
Output
What distance will you travel(in kms): 30
Fare: 22.5
Output
Enter value of a: 5
-----------------------------------------
Value of 'a' before cv() calling::5
Value of 'a' after cv() called ::5
-----------------------------------------
Value of 'a' before cr() calling::5
Value of 'a' after cr() called::6
-----------------------------------------
Aim: Write a program to illustrate scope resolution operator.
Source Code ( ppass.cpp )
#include<iostream.h>
#include<conio.h>
int a=20; //Global variable
int main()
{
int a=10; //Local variable
clrscr();
cout<<"\nLocal Variable a="<<a;
cout<<"\nGlobal Variable ::a="<<::a;
getch();
return 0;
}
Output
Local Variable a=10
Global Variable ::a= 20
Aim: Write a program to illustrate new and delete Operators. (Dynamic Memory Allocation)
Source Code ( ne w_del.cpp )
#include<iostream.h>
Output
Enter a number: 10
Enter a number: 20
Enter a number: 30
Entered numbers are:
10 3798
20 3800
30 3802
int main()
{
clrscr();
int a=17; //automatic variable, initially holds garbage value
test();
cout<<"\nInside main\n-----------";
cout<<"\nLocal variable a = "<<a;
cout<<"\nGlobal variable g = "<<g;
test();
getch();
return 0;
}
Output
Inside test()
-----------------
Global variable g = 0
Static variable s = 0
Register variable r = 0
Inside main
--------------------";
Local variable a = 17
Global variable g = 1
Inside test()
-----------------
Global variable g = 1
Static variable s = 1
Register variable r = 0
Output
x= 0
y=1
z=1
a=2
Exercises –4 (Functions)
Aim: Write a program illustrating Inline Functions
Source Code(inline.cpp)
#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
getch();
return 0;
}
Output
Max (20,10): 20
Max (0,200): 200
Aim: Write a program illustrates function overloading. Write 2 overloading functions for
power.
int main()
{
clrscr();
cout<<"\nPower(2,3):"<<power(2,3);
cout<<"\nPower(2.5,2):"<<power(2.5,2);
getch();
return 0;
}
Output:
Power(2,3): 8
Power(2.5,2): 6.25
b) Write a program illustrates the use of default arguments for simple interest function.
int main()
{
clrscr();
float p,t,r;
double si;
cout<<"Enter principle amount:";
cin>>p;
cout<<"Enter period of time:";
cin>>t;
si =interest(p,t)
cout<<"\nSimple interest(1000,5,8.0):"<<si;
si =interest(p,t,5.0);
cout<<"\nSimple interest(1000,5,5.0):"<<si;
getch();
return 0;
}
Output
Enter principle amount: 1000
Enter period of time: 5
Simple interest(1000,5,8.0): 400
Simple interest(1000,5,5.0): 250
int main()
{
clrscr();
int x,y;
double d,e;
cout<<"Enter two integers:";
cin>>x>>y;
add(x,y);
cout<<"\nEnter two real values:";
cin>>d>>e;
add(d,e);
getch();
return 0;
}
Output
Enter two integers:2 6
Sum =8
Enter two real values: 5.6 .7
Sum = 6.3
int main()
{
clrscr();
cout<<"\nPower(4,3):"<<power(4,3);
cout<<"\nPower(3.5,2):"<<power(3.5,2.0);
getch();
return 0;
}
Output
Power(4,3): 64
Power(3.5,2): 12.25
Aim: Write a program to illustrate function template for swapping of two numbers.
Source Code (tem_s wap.cpp)
#include<iostream.h>
#include<conio.h>
template <class T>
void power(T x, T y) //Temlate Definition
{
T temp;
temp = x;
x= y;
y = temp;
cout<<"\nA= "<<x<<"\tB= "<<y;
}
int main()
{
int a,b;
double d1,d2;
clrscr();
cout<<"Enter two integers:";
cin>>a>>b;
cout<<"Before swapping....";
cout<<"\nA= "<<a<<"\tB= "<<b;
cout<<"\nAfter Swapping....";
power(a,b);
cout<<"\nEnter two real values:";
cin>>d1>>d2;
cout<<"Before swapping....";
cout<<"\nA= "<<d1<<"\tB= "<<d2;
cout<<"\nAfter Swapping....";
power(d1,d2);
getch();
return 0;
}
Aim: Write a main function to create objects of DISTANCE class. Input two distances and
output the sum.
Sorce Code (distance.cpp)
#include<iostream.h>
#include<conio.h>
class Distance
{
private:
int feet;
int inch;
public:
void input()
{
cout<<"Enter FEET : ";
cin>>feet;
cout<<"Enter INCH : ";
cin>>inch;
}
void show()
{
cout<<feet<<" FEET and "<<inch<<" INCH"<<endl;
}
void add(Distance d1,Distance d2)
{
inch=d1.inch+d2.inch;
feet=inch/12;
feet+=d1.feet+d2.feet;
inch=inch%12;
}
};
Output
Distance d1
---------------
Enter FEET : 5
Enter INCH : 6
Distance d2
---------------
Enter FEET : 6
Enter INCH : 7
Aim: Write a C++ Program to illustrate the use of Constructors and Destructors (use the
above program.)
Sorce Code (dis_impr.cpp)
#include<iostream.h>
#include<conio.h>
class Distance
{
private:
int feet;
int inch;
public:
Distance( ) //Default constructor
{
feet=0;
inch=0;
}
Distance(int a,int b) //Two argument constructor
{
feet=a;
inch=b;
}
int main()
{
clrscr();
Distance d1(10,4);
Distance d2(10,2);
d1.add(d1,d2);
cout<<endl<<"The sum of d1 and d2 is : ";
d1.show();
getch();
}
Output
The sum of d1 and d2 is : 20 FEET and 6 INCH
Aim: Write a C++ program demonstrating a Bank Account with necessary methods and
variables
Note: Do not use nume ric keypad.
Souce Code(bank.cpp)
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include<stdlib.h>
#include<string.h>
class bank
{
char name[20];
int acno;
char actype[20];
float bal;
public :
bank()
{
strcpy(name,"");
acno=0;
strcpy(actype,"");
void main()
{
clrscr();
bank b;
int choice;
cout<<"BANK OF INDIA\n------------";
do
{
Output
BANK OF INDIA
------------------------
1. New Account
2. Deposit
3. Withdraw
4. View Balance
5. Exit
Enter your choice: 1
Enter Name: Vijay
Enter A/c no.: 1001
Enter A/c Type: Savings
Enter Opening Balance:500
1. New Account
2. Deposit
3. Withdraw
4. View Balance
5. Exit
Enter your choice: 2
Enter Deposit amount: 1500
Total balance = 2000
1. New Account
2. Deposit
3. Withdraw
4. View Balance
5. Exit
Enter your choice: 3
Balance Amount = 2000
Enter Withdraw Amount:1025
After Withdraw Balance is 975
1. New Account
2. Deposit
3. Withdraw
4. View Balance
5. Exit
Enter your choice: 4
ACCOUNT DETAILS
-----------------------------
Name : Vijay
A/c no.: 1001
A/c Type: Savings
Balance: 975
1. New Account
2. Deposit
3. Withdraw
4. View Balance
5. Exit
Enter your choice: 5
Exercise – 7 (Access)
Write a program for illustrating Access Specifiers public, private, protected
a) Write a program implementing Friend Function
Aim: Write a program to illustrate this pointer
int main()
{
clrscr();
Number n,n1,n2;
n1.input();
n2.input();
n= n1.min(n2);
n.show();
return 0;
}
Output
Enter a number: 3
Enter a number: 5
The minimum number is 3.
int main()
{
Man m = { " Vijayanand ",30};
Man *ptr;
Output:
Name=Vijayanand Age=30
Exercise -9 (Inheritance)
Aim: Write C++ Programs and incorporating various forms of Inheritance
int main()
{
clrscr();
Salary s;
s.input();
s.input1();
s.calculate();
s.show();
getch();
return 0;
}
Output:
Enter employee number: 1001
Enter employee name: Vijayanand
Enter designation: Manager
Enter basic pay: 25000
Enter House Rent Allowance: 2500
Enter Dearness Allowance: 5000
Enter Provident Fund: 1200
int main( )
{
Output
It is motor vehicle
It has two wheels
Speed: 80 kmph";
---------------------
It is motor vehicle
It has three wheels
Speed: 60 kmph";
---------------------
It is motor vehicle
It has four wheels
Speed: 120 kmph";
class Sports
{
protected:
int sm; // sm = Sports mark
public:
void getsm()
{
int main()
{
clrscr();
Report r;
r.input();
r.getsm();
r.show();
getch():
return 0;
}
Output:
Enter the roll no: 10
Enter the two marks : 70 90
Enter the sports mark: 60
Roll no : 10
Total : 110
Average: 73
int main( )
{
clrscr();
Maruti800 m;
m.speed();
getch();
}
Output:
Vehicle type: Car
Company: Maruti
Model: Maruti 800
Speed: 120Kmph
}
void input()
{
cout<<"\nPlayer Information";
cout<<"\nName: "<<name;
cout<<"\nGenger: "<<gender;
cout<<"\nAge: "<<age;
cout<<"\nHeight<<height;
cout<<"\nWeight: "<<weight;
cout<<"\nCity: "<<city;
cout<<"\nPincode: "<<pin;
cout<<"\nGame: "<<game;
}
};
int main( )
{
clrscr();
Game g;
Output
Enter Player Information
Name: Azar
Genger: M
Age: 38
Height and Weight: 5.8 70
City: Hyderabad
Pincode: 522183
Game played: Cricket
Player Information
Name: Azar
Genger: M
Age: 38
Height and Weight: 5.8 70
City: Hyderabad
Pincode: 522183
Game played: Cricket
int main()
{
D d;
d.input();
d.show();
return 0;
}
Output
Enter a1, a2, a3 and a4 values: 10 20 30 40
a1=10
a2=20
a3=30
a4=40
class B : public A
{
public:
B( )
{
cout<<"\n Class B constructor called";
class C : public B
{
public:
C( )
{
cout<<"\n Class C constructor called";
}
~C( )
{
cout<<"\nClass C destructor called";
}
};
int main()
{
C c;
return 0;
}
Output
Class A constructor called
Class B constructor called
Class C constructor called
Class C destructor called
Class B destructor called
Class A destructor called
class B: public A
{
int b;
int main()
{
clrscr();
A *bptr;
A a;
bptr= &a;
bptr->show();
B b;
bptr= &b;
bptr->show();
getch();
return 0;
}
Output:
a=1
b=2
Aim: Write a program illustrates pure virtual function and calculate the area of different
shapes by using abstract class.
#include <iostream.h>
#include <conio.h>
int main()
{
Square s;
Circle c;
cout << "Enter the value of side: ";
s.getData();
cout<<"Area of square: " << s.area();
cout<<"\nEnter radius of a circle: ";
c.getData();
cout << "Area of circle: " << c.area();
return 0;
}
Output
Enter the value of side: 4
Area of square: 16
Enter radius of a circle: 2
Area of circle: 12.56
Output
Value = A Size = 1 byte(s)
Value = 100 Size = 2 byte(s)
int main()
{
clrscr();
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);
cout << "Integer results\n-----------------------";
intCalc.show();
cout << endl << "\nFloat results\n---------------------";
floatCalc.show();
getch();
return 0;
}
Output
Integer results
---------------------------------
Numbers are: 2 and 1
Addition is: 3
Subtraction is: 1
Float results
---------------------------------
Numbers are: 2.4 and 1.2
Addition is: 3.6
Subtraction is: 1.2
Product is: 2.88
Division is: 2
Output
Max (39, 20): 39
Max (13.5, 20.7): 20.7
Max (Z, A): Z
Output
Enter values of a and b: 4 2
Result of (a/b): 2
Output
---------------------
List Implementation in Stl
---------------------
1.Insert Element at the Front
2.Insert Element at the End
3.Delete Element at the Front
4.Delete Element at the End
5.Front Element of List
6.Last Element of the List
7.Size of the List
8.Resize List
Output
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 4
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 8
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 9
---------------------
Vector Implementation in Stl
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 2
Output
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
8.Display Deque
9.Exit
Enter your Choice: 1
Enter value to be inserted at the end: 9
---------------------
Deque Implementation in Stl
---------------------
---------------------
Deque Implementation in Stl
---------------------
1.Insert Element at the End
2.Insert Element at the Front
3.Delete Element at the End
4.Delete Element at the Front
5.Front Element at Deque
6.Last Element at Deque
7.Size of the Deque
Output
---------------------
Map Implementation in Stl
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
---------------------
Map Implementation in Stl
---------------------
1.Insert Element into the Map
2.Delete Element of the Map
3.Size of the Map
4.Find Element at a key in Map
5.Dislplay by Iterator
6.Count Elements at a specific key
7.Exit
Enter your Choice: 7
------------------
Follow these steps to install g++ (the GNU C++ compiler) for Windows.
1. Pick the drive and a folder in which you want to install g++. I'll assume that it is C:,
but you can choose a different one. If you choose a different drive or a different
folder, you'll need to adapt the directions below accordingly.
4. Locate where the bin folder was created for the g++ installation. On my Windows
machine, it was created in the following path:
C:\cygnus\cygwin-b20\H- i586-cygwin32\bin
5. You now should add it to the PATH environment variable. You do that by following:
Start -> Control Panel -> System -> Advanced -> Environment Variables
At this point you can see the PATH variable either in the User Variables or in
the System Variables. Add the g++ path into the PATH variable. You add it to the end
of the existing value separated by a semicolon (';').
6. Restart your computer. You should now be able to run g++ from a DOS command
prompt window. You will use it from DOS command prompt as explained below. For
example, to compile a file called C:\dir_name\hello.cpp, go to the C:\dir_name folder
and enter
g++ -g hello.cpp -o hello –lm
Note: Use lm only when you include math header file in your program.
7. You'll then be able to run the compiled program by entering hello in the DOS
command prompt window.
For latest compiler, use minGW. To install the this compiler, follow the following link
“https://www.youtube.com/watch?v=lqzuR2USKRM&vl=en”