Professional Documents
Culture Documents
SATHYABAMA
Institute of Science and Technology
LAB MANUAL
SEMESTER: I
NAME: RAJENDRA
SECTION: A
AIM: To Develop a C++ program to implement a class, object creation, member function
invocation concept.
ALGORITHM:
Step 1: Declare the class, velocity with the variables, u,aand t as integer type.
Step 2: Define a function, getdata() to get the input from the user.
Step 3: Define a function, calculate () to find final velocity v=u+a*t and display
the result.
Step 4: Call the getdata () and calculate () functions from the main function by
PROGRAM 1:
#include<iostream>
#include<conio.h>
using namespace std;
class velocity
{
int v,u,a,t;
public:
void getdata()
{
cout<<"Enter the values of u,a,t:";
cin>>u>>a>>t;
}
void calculate()
{
v=u+(a*t);
cout<<"FINAL VELOCITY"<<v;
}
};
int main()
{
velocity ob;
ob.getdata();
ob.calculate();
return 0;
}
OUTPUT:
FINAL VELOCITY 34
PROGRAM 2:
#include<iostream>
#include<conio.h>
class item
int number;
float cost;
public:
cout<<"number:"<<number<<endl;
cout<<"cost:"<<cost<<endl;
};
number=a;
cost=b;
int main()
item x;
x.getdata(100,299.95);
x.putdata();
item y;
y.getdata(200,175.5);
y.putdata();
return 0;
OUTPUT:
object x
number:100
cost:299.95
object y
number:200
cost:175.5
PROGRAM 3:
#include<iostream>
#include<conio.h>
class Box
public:
double length;
double breadth;
double height;
};
int main()
Box Box1;
Box Box2;
double volume=0.0;
Box1.height=5.0;
Box1.length=6.0;
Box1.breadth=7.0;
Box2.height=10.0;
Box2.length=12.0;
Box2.breadth=13.0;
cout<<"Volume of Box1:"<<volume<<endl;
volume = Box2.height*Box2.length*Box2.breadth;
cout<<"Volume of Box2:"<<volume<<endl;
return 0;
OUTPUT:
Volume of Box1:792
Volume of Box2:2340
RESULT: The program to execute and implement a class, object creation, member function
invocation concept is successfully completed.
EXPT: 2
ALGORITHM:
Step 3: In the main function create the object of the class thereby the constructor
gets automatically invoked and then create the second object to perform
PROGRAM 1:
#include<iostream>
#include<conio.h>
class Example
public:
Example ()//Constructor
a = 15;
void Display()
};
int main()
Example Object;
return 0;
OUTPUT:
Im Constructor
Values :15 25
PROGRAM 2:
#include<iostream>
public:
};
int main ()
BaseClass des2;
OUTPUT:
PROGRAM 3:
#include<iostream>
#include<conio.h>
class abc
private:
char nm[12];
public:
abc()
cin>>nm;
void display()
cout<<nm;
};
int main()
abc d
d.display();
getch();
return 0;
OUTPUT:
RAJENDRA
PROGRAM 4:
#include<iostream>
#include<conio.h>
#include<string.h>
class abc
private:
char nm[30];
int age;
public:
abc(){}
void get()
cin>>nm;
cin>>age;
void display()
cout<<nm<<endl;
cout<<age;
};
abc::abc(char x[],int y)
strcpy(nm,x);
age=y;
int main()
abc i;
abc m =abc("Computer",20000);
i.get();
i.display();
m.display();
return 0;
OUTPUT:
RAJENDRA
25Computer
20000
RESULT: The program to execute and implement the various constructors is successfully
executed.
EXPT: 3
AIM: To Develop a C++ program to implement a friend function and Inline function
ALGORITHM:
Step 1: Create a class ‘abc’ and declare the required data members.
Step 3:Call the friend function mean() /*(Pass the object of the class as
Step 1:Average=(ob.x+ob.y)/2
ALGORITHM:
Step 1:Declare the class and declare two data members ‘a’ as integer type
Step 2:Define the functions, multiply() and divide()using the keyword inline.
#include<iostream>
#include<conio.h>
using namespace std;
class sample
{
float x,y;
public:
void setvalue()
{
x=25;
y=50;
}
friend void mean(sample s)
{
float z=(s.x+s.y)/2;
cout<<"THE MEAN VALUE IS:"<<z;
}
};
int main()
{
sample o;
o.setvalue();
mean(o);
return 0;
}
OUTPUT:
ALGORITHM:
Step 1: Declare the class, unary with the variables, x,y and z as integer type.
Step 2: Define a function, getdata() to get the input from the user.
Step 3: Define a function, operator-()to find the negation of variables and display
the result.
Step 4: Call the getdata() and operator-()functions from the main function by
Creating an instance of the class unary.
ALGORITHM:
#include<iostream>
#include<conio.h>
class unary
{
int x,y,z;
public:
void getdata()
cin>>x>>y>>z;
void display()
cout<<x<<"\t"<<y<<"\t"<<z;
void operator-()
x=-x;
y=-y;
z=-z;
};
int main()
unary ob;
ob.getdata();
cout<<"Before Overload"<<"\n";
ob.display();
-ob;
cout<<endl<<"After Overload"<<endl;
ob.display();
return 0;
OUTPUT:
Before Overload
1 2 5
After Overload
-1 -2 -5
#include<iostream>
#include<conio.h>
using namespace std;
class complex
{
private:
int r,im;
public:
void getdata()
{
cout<<"Enter the value of real and imag.part \n";
cin>>r>>im;
}
complex operator+(complex x)
{
complex temp;
temp.r=r+x.r;
temp.im=im+x.im;
return(temp);
}
void display()
{
cout<<r<<"+i"<<im;
}
};
int main()
{
complex a,b,c;
a.getdata();
b.getdata();
c=a+b;
c.display();
return 0;
}
OUTPUT:
Enter the value of real and imag.part
1 5
Enter the value of real and imag.part
89
9+i14
successfully executed.
EXPT: 5
ALGORITHM:
Step 1: Declare the class,overload1with the variables, a and b as integer type and x and y
as character type.
Step 2:Define a functions, area(int a),area(int l,intb,int h) and area(float r,int h1)to find the
area of square, rectangle and circle respectively.
Step 3: Call the class and functions from the main function by
Creating an instance of the class overload1.
PROGRAM:
#include<iostream>
#include<conio.h>
class overload1
int a,b;
char x,y;
public:
void area(int a)
int sq;
sq=a*a;
cout<<"AREA OF SQUARE:"<<sq<<endl;
int rect;
rect=l*b*h;
cout<<"AREA OF RECTANGLE:"<<rect<<endl;
void area(float r)
float cir;
cir=3.14*r*r;
cout<<"AREA OF CIRCLE:"<<cir<<endl;
};
int main()
overload1 ob;
int a,l,b,h,h1;
float r;
cout<<"ENTER SIDE:";
cin>>a;
ob.area(a);
cout<<"ENTER L,B,H:";
cin>>l>>b>>h;
ob.area(l,b,h);
cout<<"ENTER r:";
cin>>r;
ob.area(r);
return 0;
OUTPUT:
ENTER SIDE:12
AREA OF SQUARE:144
ENTER L,B,H:23 10 11
AREA OF RECTANGLE:2530
ENTER r : 8.5
AREA OF CIRCLE:226.865
EXPT: 6
ALGORITHM:
Step 1: Declare the class ‘agency’ and define the member function getdata()
and get the salesprice as input.
Step 2: Declare the class ‘agent’ which is derived from ‘agency’ class and
define the member function calc () to calculate the commission
as 10% of salesprice and display the result.
Step 3: In the main (), create an instance of the derived class and invoke the
getdata() and calc() member functions of the base class and derived
class respectively.
AGENCY
ALGORITHM:
AGENT
Step 1: Declare the class ‘salary’ with variables Basic, HRA and DA.
Step 2: Define the member function getsal() in the class ‘salary’ to find
the gross=basic+hra+da.
Step 3: Declare the class ‘ tax’ and find the tax amount as to be deducted in the
member function deduct()
Step 4: Declare the class ‘netsalary’ which is publicly derived from classes
salary and tax.
Step 5: Define the member function netpay() int the ‘netsalary’ class to deduce
the tax from total.
Step 6: Create an instance for the derived class netsalary and invoke the
member functions of both base classes and the derived class.
SALARY TAX
NETSALARY
PROGRAM 1:
#include<iostream>
#include<conio.h>
using namespace std;
class base
{
public:
int x,y;
void getdata()
{
cout<<"Enter the values:";
cin>>x>>y;
}
};
class derived : public base
{
int z;
public:
void addition()
{
z=x+y;
cout<<"Addition:"<<z;
}
};
int main()
{
derived ob;
ob.getdata();
ob.addition();
return 0;
}
OUTPUT:
Addition:33
PROGRAM 2:
#include<iostream>
#include<conio.h>
using namespace std;
class worker
int age;
char name[10];
public:
void get();
void show();
};
void worker::get()
cin>>name;
cin>>age;
void worker::show()
int now;
public:
void get();
void show();
};
void manager::get()
worker::get();
cin>>now;
void manager::show()
worker::show();
int main()
worker W1;
manager M1;
M1.get();
M1.show();
return 0;
OUTPUT:
My name is:RAJENDRA
My age is:27
#include<iostream>
#include<conio.h>
class student
public:
int rollno;
char name[20];
void getdata()
cin>>name>>rollno;
};
public:
int m1,m2;
void getmarks()
cin>>m1>>m2;
};
int score,total;
public:
void getscore()
cin>>score;
void calculate()
total=m1+m2+score;
if(total>=290)
cout<<"GRADE A";
cout<<"GRADE B";
else
cout<<"GRADE C";
else
cout<<"FAIL";
}
};
int main()
grade g;
g.getdata();
g.getmarks();
g.getscore();
g.calculate();
return 0;
OUTPUT:
GRADE A
RESULT:The program to execute and implement the inheritance Single, multiple is successfully
executed.
#include<iostream>
#include<conio.h>
using namespace std;
class inlinefunc
{
int a,b;
float c,d;
public:
inline void multiply()
{
cout<<"Enter 2 integers:";
cin>>a>>b;
int x=a*b;
cout<<x;
}
inline void divide()
{
cout<<endl<<"Enter 2 float values:";
cin>>c>>d;
float y=c/d;
cout<<y;
}
};
int main()
{
inlinefunc f;
f.multiply();
f.divide();
return 0; }
OUTPUT:
Enter 2 integers:2 5 10
RESULT: The program to execute and implement a friend function and Inline functionis
successfully executed.
EXPT: 7
ALGORITHM:
Step 1: Declare the class ‘ student’ with variables rollno and name.
Step 2: Define the member function getdata() in the student class to get
the rollno and name.
Step 3: Declare the class ‘ academic’ which is derived publicly from
student class and read the marks in member function getmarks()
of the academic class and calculate the average.
Step 4: Declare the class ‘grade’ which is publicly derived from
academic class and declare the member function calc() to
calculate the grade of each student
Step 5: Create array of objects for the derived class ‘grade’ and calculate
the grade for a set of students.
STUDENT
ACADEMIC
GRADE
PROGRAM 1:
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
public:
int rollno;
char name[20];
void getdata()
{
cout<<"Enter the student name & roll no:";
cin>>name>>rollno;
}
};
class academic : public student
{
public:
int m1,m2;
void getmarks()
{
cout<<"Enter the marks for two subjects:";
cin>>m1>>m2;
}
};
class grade : public academic
{
int score,total;
public:
void getscore()
{
cout<<"Enter the score:";
cin>>score;
}
void calculate()
{
total=m1+m2+score;
if(m1>=50 && m2>=50 && score>=50)
{
if(total>=290)
cout<<"GRADE A";
else if(total<=290 && total>=280)
cout<<"GRADE B";
else
cout<<"GRADE C";
}
else
cout<<"FAIL";
}
};
int main()
{
grade g;
g.getdata();
g.getmarks();
g.getscore();
g.calculate();
return 0;
}
OUTPUT:
Enter the student name & roll no:RAJ 1502
GRADE C
PROGRAM 2:
#include<iostream>
#include<conio.h>
class worker
int age;
char name[20];
public:
void get();
void show();
};
void worker::get()
cin>>name;
cin>>age;
}
void worker::show()
int now;
public:
void get();
void show();
};
void manager::get()
worker::get();
cin>>now;
void manager::show()
worker::show();
cout<<"\n No.of workers under me are:"<<now;
int nom;
public:
void get();
void show();
};
void ceo::get()
manager::get();
cin>>nom;
void ceo::show()
manager::show();
int main()
{
ceo c1;
c1.get();
cout<<"\n\n";
c1.show();
return 0;
OUTPUT:
My name is:RAJ
My age is:28
RESULT: The program to execute and implement the inheritance multilevel is successfully
executed.
EXPT: 8
ALGORITHM:
Step 1: Initialise the Base class with member data and member functions.
Step 2: Initialise the Derived class from the Base class with member data
Step 3: Initialise the main function and provide arguments to the function.
PROGRAM
#include<iostream>
protected:
int x;
public:
Base(int i) { x = i; }
};
int y;
public:
void fun()
};
int main(void)
d.fun();
return 0;
OUTPUT:
x = 4, y = 5
RESULT: The program to execute and implement the Abstract class is successfully executed.
EXPT: 9
AIM: Develop a C++ program to implement a Virtual function and virtual base class.
ALGORITHM:
Step 1: Declare the class ‘ employee’ and define the member function
getname() to get the employee name.
Step 2: Declare the class ‘ salary ‘ which is publicly derived from
‘employee’ with the keyword virtual. Define the member function
getdetails() to get basic , HRA and DA and find the total.
Step 3: Declare the class ‘ taxcal’ which is publicly derived from ‘employee’
with the keyword virtual. Find the tax amount to be deducted in the
function deduct().
Step 4: Declare the class ‘netsalary ‘which is publicly derived from the
classes salary and tax. Define the member function netpay() to
deduce the tax from total.
Step 5: Create an instance for the derived class netsalary and invoke all
functions.
#include <iostream>
class employee
int code ;
public:
};
class grade: public employee
float salary ;
public :
void getdata ( ) ;
void display ( );
};
cin>> grd ;
cin>> salary;
}
void grade :: display ( )
int main ()
employee *ptr ;
grade obj ;
ptr = &obj ;
ptr->getdata ( ) ;
ptr->display ( ) ;
//getche ( ) ;
return 0;
OUTPUT:
Grade salary
MANAGER250000
PROGRAM: VIRTUAL BASE CLASS
#include <iostream>
protected:
int r_no;
public:
r_no = a;
};
{ //base class 1
protected:
int part1;
int part2;
public:
part1= x; part2=y;}
};
{ //base class 2
protected:
int score;
public:
score = a ;
}
void put_s (void)
};
public:
};
put_n ();
putm ();
put_s ();
int main ( )
//clrscr ( ) ;
result S1 ;
S1.get_n (521) ;
S1.get_m (51, 52) ;
S1.get_s (9) ;
S1. show ( ) ;
return 0;
OUTPUT:
Roll No.521
part1 = 51
part2 = 52
sports wt.: 9
RESULT: The program to execute and implement a Virtual function and virtual base class is
successfully executed.