You are on page 1of 50

Object Oriented Programming SCSA6103

SATHYABAMA
Institute of Science and Technology

(Established under Section 3, UGC Act 1956)

OBJECT ORIENTED PROGRAMMING SCSA6103

LAB MANUAL

BATCH: 2022 – 2024

SEMESTER: I

NAME: RAJENDRA

REGISTER NO: 22O572021

SECTION: A

BRANCH: MSc MATHEMATICS


EXPT: 1 DATE: 17-12-2022

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

Creating an instance of the class velocity.

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:

Enter the values of u,a,t: 4 5 6

FINAL VELOCITY 34

PROGRAM 2:

#include<iostream>

#include<conio.h>

using namespace std;

class item

int number;

float cost;

public:

void getdata(int a, float b);


void putdata(void)

cout<<"number:"<<number<<endl;

cout<<"cost:"<<cost<<endl;

};

void item::getdata(int a,float b)

number=a;

cost=b;

int main()

item x;

cout<<"\n object x"<<endl;

x.getdata(100,299.95);

x.putdata();

item y;

cout<<"\n object y"<<endl;

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>

using namespace std;

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;

volume = Box1.height* Box1.length*Box1.breadth;

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

To Develop a C++ program to implement the various constructors

ALGORITHM:

Step 1: Create a class named matrix

Step 2: Declare the constructor, copy constructor, assignment operator and

destructor as public data members of the class.

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

operator overloading and third object for copy constructor.

Step 3.1:Invoke the assignment operator by assigning object2=object1

Step 3.2:Invoke the copy constructor by copying object3=object2

Step 4: Display the result

Step 5: Finally the destructor is invoked automatically.

PROGRAM 1:

#include<iostream>
#include<conio.h>

using namespace std;

class Example

int a, b; // Variable Declaration

public:

Example ()//Constructor

a = 15;

b = 25; // Assign Values in Constructor

cout<< "Im Constructor\n";

void Display()

cout<< "Values :" << a << "\t" << b;

};

int main()

Example Object;

Object.Display(); // Constructor invoked.


getch(); // Wait For Output: Screen

return 0;

OUTPUT:

Im Constructor

Values :15 25

PROGRAM 2:

#include<iostream>

#include<conio.h>// Header Files

using namespace std; //Standard Namespace Declaration

class BaseClass // Class Name

public:

BaseClass () //Constructor of the Base Class

cout<< "Constructor of the Base Class : Object Created"<<endl;

~BaseClass() //Destructor of the Base Class

cout<< "Destructor of the BaseClass : Object Destroyed"<<endl;


}

};

int main ()

cout<<"Simple Destructor Scope Measurement Example Program \n\n";

cout<<"Object 1 : Creation In Braces\n\n";// Object 1 Declaration for BaseClass

BaseClass des1;// Object 1 des1 : End of Scope

cout<<"Object 2 : Creation in Main Part\n\n";

BaseClass des2;

getch();// Wait For Output: Screen

return 0;//Main Function return Statement

OUTPUT:

Simple Destructor Scope Measurement Example Program

Object 1 : Creation In Braces

Constructor of the Base Class : Object Created

Destructor of the BaseClass : Object Destroyed

Object 2 : Creation in Main Part

Constructor of the Base Class : Object Created


Destructor of the BaseClass : Object Destroyed

PROGRAM 3:

#include<iostream>

#include<conio.h>

using namespace std;

class abc

private:

char nm[12];

public:

abc()

cout<<"Enter your name:";

cin>>nm;

void display()

cout<<nm;

};
int main()

abc d

d.display();

getch();

return 0;

OUTPUT:

Enter your name:RAJENDRA

RAJENDRA

PROGRAM 4:

#include<iostream>

#include<conio.h>

#include<string.h>

using namespace std;

class abc

private:
char nm[30];

int age;

public:

abc(){}

abc(char x[],int y);

void get()

cout<<"Enter your name:";

cin>>nm;

cout<<"Enter your age:";

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:

Enter your name:RAJENDRA

Enter your age:25

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 2: Read the values ‘ x’ and ‘ y’.

Step 3:Call the friend function mean() /*(Pass the object of the class as

argument)*/ from the main function

Algorithm for mean(abcob)

Step 1:Average=(ob.x+ob.y)/2

Step 2:Display the result.

ALGORITHM:

Step 1:Declare the class and declare two data members ‘a’ as integer type

and ‘b’ as float type.

Step 2:Define the functions, multiply() and divide()using the keyword inline.

Step 3:Create an instance and invoke the inline functions.

Algorithm for inline float mul(int x,float y)

Step 1: return x*y

Algorithm for inline float div(int x,float y)

Step 1: return y/x


PROGRAM: FRIEND FUNCTION

#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:

THE MEAN VALUE IS:37.5


EXPT: 4

AIM: Develop a C++ program to implement an operator overloading concept

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:

Step 1: Declare the class,complexwith the variables, r and im as integer type.


Step 2: Define a function, getdata() to get the input from the user.
Step 3: Define a function,operator+ (complex x) to add two complex numbers and display
the result.
Step 4: Call the getdata() and operator+ (complex x) functions from the main function by
Creating an instance of the class complex.

PROGRAM: UNARY OPERATOR OVERLOADING

#include<iostream>

#include<conio.h>

using namespace std;

class unary

{
int x,y,z;

public:

void getdata()

cout<<"Enter the x,y,z values:";

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:

Enter the x,y,z values:1 2 5

Before Overload

1 2 5

After Overload

-1 -2 -5

PROGRAM: BINARY OPERATOR OVERLOADING

#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

RESULT:The program to execute and implement an operator overloading concept is

successfully executed.

EXPT: 5

AIM: Develop a C++ program to implement a function overloading concept.

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>

using namespace std;

class overload1

int a,b;
char x,y;

public:

void area(int a)

int sq;

sq=a*a;

cout<<"AREA OF SQUARE:"<<sq<<endl;

void area(int l,int b,int h)

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

RESULT:The program to execute and implement a function overloading concept is successfully


executed.

EXPT: 6

AIM: Develop a C++ program to implement the inheritance Single, multiple.

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: SINGLE INHERITANCE

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:

Enter the values:15 18

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()

cout<<"Your name please:";

cin>>name;

cout<<"Your age please:";

cin>>age;

void worker::show()

cout<<"\n My name is:"<<name<<"\n My age is:"<<age;

class manager:public worker


{

int now;

public:

void get();

void show();

};

void manager::get()

worker::get();

cout<<"Number of workers under you:";

cin>>now;

void manager::show()

worker::show();

cout<<"\n No.of workers under me are:"<<now;

int main()

worker W1;

manager M1;
M1.get();

M1.show();

return 0;

OUTPUT:

Your name please:RAJENDRA

Your age please: 27

Number of workers under you:15

My name is:RAJENDRA

My age is:27

No.of workers under me are:15

PROGRAM: MULTIPLE INHERITANCE

#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 12000

Enter the marks for two subjects:90 90

Enter the score:190

GRADE A

RESULT:The program to execute and implement the inheritance Single, multiple is successfully

executed.

PROGRAM: INLINE FUNCTION

#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

Enter 2 float values:2.1 5.1


0.411765

RESULT: The program to execute and implement a friend function and Inline functionis

successfully executed.

EXPT: 7

AIM: Develop a C++ program to implement the inheritance multilevel

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: MULTILEVEL INHERITANCE

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

Enter the marks for two subjects:51 52


Enter the score:50

GRADE C

PROGRAM 2:

#include<iostream>

#include<conio.h>

using namespace std;

class worker

int age;

char name[20];

public:

void get();

void show();

};

void worker::get()

cout<<"Your name please:";

cin>>name;

cout<<"Your age please:";

cin>>age;
}

void worker::show()

cout<<"\n My name is:"<<name<<"\n My age is:"<<age;

class manager:public worker

int now;

public:

void get();

void show();

};

void manager::get()

worker::get();

cout<<"No. of workers under you:";

cin>>now;

void manager::show()

worker::show();
cout<<"\n No.of workers under me are:"<<now;

class ceo:public manager

int nom;

public:

void get();

void show();

};

void ceo::get()

manager::get();

cout<<"\n No. of managers under you are:";

cin>>nom;

void ceo::show()

manager::show();

cout<<"\n The no. of managers under me are:"<<nom;

int main()
{

ceo c1;

c1.get();

cout<<"\n\n";

c1.show();

return 0;

OUTPUT:

Your name please:RAJ

Your age please:28

No. of workers under you:200

No. of managers under you are:23

My name is:RAJ

My age is:28

No.of workers under me are:200

The no. of managers under me are:23

RESULT: The program to execute and implement the inheritance multilevel is successfully

executed.
EXPT: 8

AIM: Develop a C++ program to implement the Abstract class

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

and member function.

Step 3: Initialise the main function and provide arguments to the function.

PROGRAM

#include<iostream>

using namespace std;

class Base // An abstract class with constructor

protected:

int x;

public:

virtual void fun() = 0;

Base(int i) { x = i; }

};

class Derived: public Base


{

int y;

public:

Derived(int i, int j):Base(i) { y = j; }

void fun()

cout<< "x = " << x << ", y = " << y;

};

int main(void)

Derived d(4, 5);

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 base class ‘ mystring’


Step 2: Create the constructor for ‘mystring’ class and initialise a
string and calculate string length.
Step 3: Create the virtual member function disp(), for the base class
‘mystring’ and display the string and its length
Step 4: Declare the class ‘char_ct’ which is publicly derived from the
class ‘mystring’
Step 5: Create the constructor for the ‘char_ct’ class and calculate the
number of characters in the given string
Step 6: Create the member function disp(), for the derived class
‘char_ct’ and display the number of characters
Step 7: Declare the class ‘word_ct’ which is publicly derived from the
class ‘mystring’
Step 8: Create the constructor for the ‘word_ct’ class and calculate the
number of characters in the given string
Step 9:Create the member function disp(), for the derived class
‘word_ct’ and display the number of words.
Step 10: In main(), create object for base and derived classes. Create a
base class pointer and invoke all the functions using base
pointer.

Algorithm for Base Class

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.

PROGRAM: VIRTUAL FUNCTION

#include <iostream>

using namespace std;

class employee

int code ;

char name [20] ;

public:

virtual void getdata ( ) ;

virtual void display ( ) ;

};
class grade: public employee

char grd [90] ;

float salary ;

public :

void getdata ( ) ;

void display ( );

};

void employee :: getdata ( )

void employee:: display ( )

void grade :: getdata ( )

cout<< "Enter employee’s grade: ";

cin>> grd ;

cout<< "\n Enter the salary: ";

cin>> salary;

}
void grade :: display ( )

cout <<" Grade salary \n";

cout<< grd<< salary << endl;

int main ()

employee *ptr ;

grade obj ;

ptr = &obj ;

ptr->getdata ( ) ;

ptr->display ( ) ;

//getche ( ) ;

return 0;

OUTPUT:

Enter employee’s grade: MANAGER

Enter the salary: 250000

Grade salary

MANAGER250000
PROGRAM: VIRTUAL BASE CLASS

#include <iostream>

using namespace std;

class student // Base class declaration

protected:

int r_no;

public:

void get_n (int a)

r_no = a;

void put_n (void)

cout << "Roll No." << r_no;}

};

class test : virtual public student // Virtually declared common

{ //base class 1

protected:
int part1;

int part2;

public:

void get_m (int x, int y)

part1= x; part2=y;}

void putm (void)

cout << "\npart1 = "<< part1 << "\n";

cout << "part2 = "<< part2 <<"\n";

};

class sports : public virtual student // virtually declared common

{ //base class 2

protected:

int score;

public:

void get_s (int a)

score = a ;

}
void put_s (void)

{ cout << "sports wt.: " <<score<< "\n";}

};

class result: public test, public sports //derived class

private : int total ;

public:

void show (void) ;

};

void result :: show (void)

{ total = part1 + part2 + score ;

put_n ();

putm ();

put_s ();

cout << "\n total score= " <<total<< "\n" ;

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

total score= 112

RESULT: The program to execute and implement a Virtual function and virtual base class is

successfully executed.

You might also like