Lab Manual: Object Oriented Programming Using C++ (ETIT - 209) B.Tech Programme (CSE)
Lab Manual: Object Oriented Programming Using C++ (ETIT - 209) B.Tech Programme (CSE)
Lab Manual: Object Oriented Programming Using C++ (ETIT - 209) B.Tech Programme (CSE)
(ETIT – 209)
B.Tech Programme
(CSE)
1. Introduction
2. Hardware and Software requirements
3. Marking scheme for the practical lab exam
4. Details of Experiments
5. List of experiments
6. Expected viva voce questions
7. References
2. Lab Requirements
Marking Scheme :
Total Marks: 40
1. Regularity: 30
2. Viva Voce: 10
NOTE: For the regularity, marks are awarded to the student out of 10 for each
experiment performed in the lab and at the end the average marks are giving out of
30.
External Practical Exam:
It is taken by the concerned faculty member of the batch and by an external
examiner. In this exam student needs to perform the experiment allotted at the
time of the examination, a sheet will be given to the student in which some
details asked by the examiner needs to be written and at last viva will be taken
by the external examiner.
Marking Scheme:
Total Marks: 60
Division of 60 marks is as follows:
a. Evaluation of the answer sheet 20
b. Viva Voce 15
c. Experiment performance 15
d. File submitted 10
NOTE:
Internal marks + External marks = Total marks given to the students
(40 marks) (60 marks) (100 marks)
Experiments given to perform can be from any section of the lab.
List of Experiments
1. Define a class student and include the following
Data members
1. Name
2. Roll no
3. Marks of five subjects.
Member functions
1. To assign initial values and calculate percentage
2. To display percentage and grade according to percentage.
Write main function to test the program.
Data members
1. Name of the depositor
2. Account number
3. Balance amount in the bank
Member functions
1. To assign initial values
2. To deposit an amount
3. To withdraw an amount after checking the balance
4. To display name and balance
Write a main program to test the program.
3. To illustrate the use of object as function argument.
Define a class time to represent time in hours and minutes. Include the following
Data members
1. Time
2. Hours
Member functions
1. Gettime( ) to assign initial values.
2. Sum( ) to perform the addition of time in the hours and minutes format.
3. Display() to display time.
Pass objects of class time as arguments to sum function. Write the main function to test
the program.
4. To study function friendly to two classes.
Define two classes class A and class B and find greatest number from private members of
class A and class B. Include following things in both classes
Class A data members
1. One integer value
Member function
1. To assign initial value
Class B data members
1. One integer value
Member function
1. To assign initial value
Define friend function with object as arguments of class A and Class B to find the max
value.
Write main function to test the program.
4. Write a program to use common friend function exchange the private values of two classes.
The function is called by reference.
Define two classes A and B and include the following
Data members
1. Single integer value
Member function
1. To initialize integer value
2. To display swapped values
Define friend function to both classes with arguments as object references to swap the private
values.
Write main function to test the program.
6. To illustrate how an object can be created (within a function) and returned to another function.
(Add two complex numbers using friend function returning object).
Define a class complex including following
Friend function with object as arguments
Data member
1. Two real numbers
Member function
1. To initialize two numbers
2. To display complex number
Define friend function to add two numbers in the definition.
Write main function to test the program.
Define class creactangle and include data members width and height. In public section create two
constructor one with no arguments to initialize height and weight with some values. Second
constructor will parameterize. Include member function area to return area.
15. Write a program to create a template function for Quicksort and demonstrate sorting of
integers and doubles.
Experiment 1
Objective:
Procedure:
#include<iostream.h>
#include<conio.h>
class student
{
char name[20];
int roll_no;
int mar[5];
public:
void getdata(); //for input
void showdata(); // to display data
};
void student::getdata()
{
int i;
cout<<”enter name:\n”;
cin>>name;
cout<<”enter roll no:\n”;
cin>>roll_no;
cout<<”enter the marks:\n”;
for(i=0;i<=4;i++)
{
cin>>mar[i];
}//end of for loop
}//end of getdata()
void student::showdata()
{
int i,s=0,p=0;
for(i=0;i<=4;i++)
{
s=s+mar[i];
}
p=(s/5);
if(p>75)
cout<<”you have got”<<p<<”% marks and grade- distinction”;
if((p<74) && (p>60))
cout<<” you have got”<<p<<”% marks and grade- 1st class”;
if(p<60)
cout<<” you have got”<<p<<”% marks and grade- IInd class”;
}//end of showdata()
void main()
{
student a;
a.getdata();
a.showdata();
}
Output:
enter name:
Ram
Enter roll no:
10
enter the marks:
99
98
97
98
99
you have got 98% marks and grade- distinction
Experiment 2
Objective:
Procedure:
#include<iostream.h>
#include<conio.h>
class bank
{
char name[20];
int ac_no;
int amount;
public:
void getdata();
void display();
void deposit();
void withdraw();
};
void bank:: getdata()
{
cout<<”enter the customer name:\n”;
cin>>name;
cout<<”enter the account no:\n”;
cin>>ac_no;
cout<<”enter the amount\n”;
cin>>amount;
}
void bank::deposit()
{
int a;
cout<<”enter the amunt to be deposited:\n”;
cin>>a;
amount=amount+a;
cout<<”New amount in your account is:”<<amount;
}
void bank::withdraw()
{
int a;
cout<<”enter the amount of withdraw:\n”;
cin>>a;
amount=amount-a;
if(a>amount)
cout<<”Sorry! Balance is not enough”;
else
cout<<”new amount in your account is:”<<amount;
}
void display:: display()
{
cout<<”\n name is:”<<name;
cout<<”\n account no:”<<ac_no;
cout<<”\n amount is:”<<amount;
}
void main()
{
bank a1;
a1.getdata();
a1.deposit();
a1.withdraw();
a1.display();
getch();
}
Output:
Enter customer name:
Ram
Enter account no:
1001
enter amount:
2000
enter amount to be deposited:
1500
new amount in your account is:
3500
enter amount of withdraw:
1200
new amount in your account is:
2300
name is:Ram
account is:1001
amount is:2300
Experiment 3
Objective:
To illustrate the use of object as function argument.
Define a class time to represent time in hours and minutes. Include the following
Data members
3. Time
4. Hours
Member functions
4. Gettime( ) to assign initial values.
5. Sum( ) to perform the addition of time in the hours and minutes format.
6. Display() to display time.
Pass objects of class time as arguments to sum function. Write the main function to test the
program.
Procedure:
#include<iostream.h>
#include<conio.h>
class time
{
int hours;
int minutes;
public:
void gettime(int h, int m)
{
hours=h;
minutes=m;
}
void display()
{
cout<<”hours:”<<hours;
cout<<”minutes:”<<minutes;
}
void sum(time,time);
}; // end of class time
Output:
hours:2 minutes:60
hours:3 minutes:0
hours:5 minutes:60
Experiment 4.
Objective:
To study function friendly to two classes.
Define two classes class A and class B and find greatest number from private members of class
A and class B. Include following things in both classes
Class A data members
2. One integer value
Member function
2. To assign initial value
Class B data members
2. One integer value
Member function
2. To assign initial value
Define friend function with object as arguments of class A and Class B to find the max value.
Write main function to test the program
Code:
#include<iostream.h>
class abc;
class xyz
{
int x;
public:
void setvalue(int i)
{
a=I;
}
};
class abc
{
int a;
public:
void setvalue(int i)
{
a=i;
}
friend void max(xyz,abc);
};
Void max(xyz m, abc n)
{
If (m.x>=n.a)
Cout<<m.x;
Else
cout<<n.a;
}
int main( )
{
abc a;
a.setvalue(10);
xyz b;
b.setvalue(20);
max(a,b);
return 0;
}
Experiment 5
Objective:
Write a program to use common friend function exchange the private values of two classes. The
function is called by reference.
Define two classes A and B and include the following
Data members
2. Single integer value
Member function
3. To initialize integer value
4. To display swapped values
Define friend function to both classes with arguments as object references to swap the private
values.
Write main function to test the program.
Procedure:
#include<iostream.h>
#include<conio.h>
class b;
class a
{
int m;
public:
void getdata()
{
cout<<”enter the value of m:\n”;
cin>>m;
}
void display(void)
{
cout<<m;
}//end of display
friend void swap (a&a1,b&b1);
}; //end of class a
class b
{
int n;
public:
void getdata()
{
cout<<”enter the value of n:\n”;
cin>>n;
}
void display(void)
{
cout<<n;
} //end of display()
friend void swap(a&a1,b&b1);
}; //end of class b
void swap(a&a1,b&b1)
{
int temp=0;
temp=a1.m;
a1.m=b1.n;
b1.n=temp;
cout<<”m:\n”<<a1.m;
cout<<n:\n”<<b1.n;
} //end of swap()
void main()
{
a c;
b d;
c.getdata();
d.getdata();
cout<<”before swaping:\n”;
c.display();
d.display();
swap(c.d);
cout<<”after swaping:\n”;
c.display();
d.display();
} //end of main()
Output:
Enter value of m:
3
enter value of n
2
before swaping:
m:3
n:2
after swaping:
m:2
n:3
Experiment 6
Objective:
To illustrate how an object can be created (within a function) and returned to another function.
(Add two complex numbers using friend function returning object).
Define a class complex including following
Friend function with object as arguments
Data member
2. Two real numbers
Member function
3. To initialize two numbers
4. To display complex number
Define friend function to add two numbers in the definition.
Write main function to test the program.
Code:
#include<iostream.h>
class complex
{
float x;
float y;
public:
void input(float real, float imag)
{
x=real;
y=imag;
}
friend complex sum( complex, comlex);
void show( complex);
};
complex sum ( complex c1, complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
Return ( c3);
}
void complex :: show( complex)
{
cout<<c.x<<”+j”<<c.y<<”\n”;
}
int main( )
{
complex a,b,c;
a.input(3.1.5.65);
b.input(2.75,1.2);
c=sum(a,b);
cout<<”a=”;
a.show(a)
cout<<”b=”;
b.show(b)
cout<<”c=”;
c.show(c)
}
Experiment 7
Objective:
To illustrate function overloading
Write a program to declare three function of same name volume and calculate volume of cube,
cylinder and rectangular box.
Code:
#include<iostream.h>
int volume(int);
double volume(double,int);
long volume( long, int, int);
int main()
{
cout<< volume(10)<<’\n”;
cout<< volume(2.5,8)<<’\n”;
cout<< volume(100L,75,15)<<’\n”;
return 0;
}
int volume( int s)
{
return (s*s*s);
}
double volume(double r, int h)
{
return (3.14*r*r*h);
}
long volume( long l,int b, int h)
{
return (l*b*h);
}
Experiment 8
Objective:
To perform the constructor overloading
Define a class crectangle and find area of rectangle using constructor overloading
Define class creactangle and include data members width and height. In public section create two
constructor one with no arguments to initialize height and weight with some values. Second
constructor will parameterize. Include member function area to return area.
Code :
#include <iostream>
using namespace std;
class CRectangle
{
int width, height;
public:
CRectangle ();
CRectangle (int,int);
int area (void) {return (width*height);}
};
CRectangle::CRectangle ()
{
width = 5;
height = 5;
}
int main ()
{
CRectangle rect (3,4);
CRectangle rectb;
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
Experiment 9
Objective:
Write a program to overload ++ operator
Create two objects of class counter c1 and c2.the counts in the objects are displayed, they are
initially 0.then using the overloaded ++ operator, we increment c1 once and c2 twice, and
display the resulting values.
Code:
#include<iostream.h>
class counter
{
private:
unsigned int count;
public:
counter( )
{
count=0;
}
int getcount( )
{
return count;
}
void operator ++( )
{
count++;
};
void main( )
{
counter c1,c2;
cout<<”\nc1=”<<c1.getcount( );
cout<<”\nc2=”<<c2.getcount( );
c1++;
c2++;
++c2;
cout<<”\nc1=”<<c1.getcount( );
cout<<”\nc2=”<<c2.getcount( );
}
Experiment 10
Objective:
Write a program to overload operator + to add two polar coordinates.
Define a class polar and include following
Private data members of type double
3. radius
4. angle
Private member function (for converting polar coordinates to rectangular)
4. getx( )
5. gety( )
6. display( )
Also define operator function (operator +) to add two polar variables. Constructor in polar
initialize a variable to 0 or it to specified values.
Code:
#include<iostream.h>
#include<math.h>
class polar
{
private:
double radius;
double angle;
double getx( )
{
return radius*cos(angle);
}
double gety( )
{
return radius *sin(angle)
}
public:
polar( )
{
radius=0.0;
angle=0.0;
}
polar( float r, float a)
{
radius=r;
angle=a;
}
void display( )
{
cout<<”(“<<radius<<”,”<<angle<<”)”;
}
polar operator + (polar p2)
{
double x=getx( )+p2.getx( );
double y=gety( )+p2.gety( );
double r =sqrt(x*x + y*y);
double a=atan(y/x);
return polar(r,a)
}
};
void main( )
{
polar p1(10.0,0.0);
polar p2(10.0,1.570796325);
polar p3;
p3=p1+p2
cout<<”\np1=”;
p1.display( );
cout<<”\np2=”;
p2.display( );
cout<<”\np3=”;
p3.display( );
}
Experiment 11
Objective:
Write a program to overload insertion and extraction operator.
Code :
#include <iostream>
using namespace std;
class Distance
{
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance()
{
feet = 0;
inches = 0;
}
Distance(int f, int i)
{
feet = f;
inches = i;
}
friend ostream &operator<<( ostream &output, const Distance &D )
{
output << "F : " << D.feet << " I : " << D.inches;
return output;
}
return 0;
}
Experiment 12
Objective:
Write a program to overload = operator.
class myclass
{
int a;
int b;
public:
myclass(int, int);
void show();
};
myclass::myclass(int x,int y)
{
a=x;
b=y;
}
void myclass::show()
{
cout<<a<<endl<<b<<endl;
}
public:
myclass2(int, int);
void show();
myclass2 operator=(myclass2);
};
return *this;
};
myclass2::myclass2(int x,int y)
{
a=x;
b=y;
}
void myclass2::show()
{
cout<<a<<endl<<b<<endl;
}
// main
void main()
{
myclass ob(10,11);
myclass ob2(20,21);
myclass2 ob3(100,110);
myclass2 ob4(200,210);
Code:
#include<iostream.h>
#include<conio.h>
template<class T>
void quicksort(T a[], int low, int high)
{
int pos;
if(low<high)
{
pos=partition(a, low, high);
quicksort(a, low, pos-1);
quicksort(a, pos+1, high);
}
}
template<class T>
int partition(T a[], int low, int high)
{
T key, temp;
int left, right;
key=a[low];
left=low+1;
right=high;
while(1)
{
while((left<high) &&(key>=a[left]))
left++;
while(key<a[right])
right--;
if(left<right)
{
temp=a[left];
a[left]=a[right];
a[right]=temp;
}
else
{
temp=a[low];
a[low]=a[right];
a[right]=temp;
return (right);
}
}
}
void main()
{
clrscr();
int a[10],n,i,low, high;
double b[10];
cout<<"QUICK SORT USING FUNCTION TEMPLATE(To perform int,double array
operatons"<<endl;
cout<<"*************************************************************************"<<en
dl;
cout<<"enter array size\n";
cin>>n;
cout<<"Enter the elements of integer array\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the elements of double array\n";
for(i=0;i<n;i++)
cin>>b[i];
low=0;
high=n-1;
quicksort(a, low, high);
quicksort(b, low, high);
cout<<"The sorted list of integer:\n";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
cout<<"\nThe sorted list of double:\n";
for(i=0;i<n;i++)
cout<<"\t"<<b[i];
getch();
}