OOPS Lab file
OOPS Lab file
f) Inline Function 18
f) Friend Function 33
c) Function Overloading 40
a) Single Inheritance 43
b) Multiple Inheritance 47
c) Multilevel Inheritance 49
d) Virtual Function 52
4
e) Virtual Base Class 55
f) Class Templates 60
g) Function Template 63
AIM:
To write a C++ program to find the sum of the given variables using function with
default arguments.
ALGORITHM:
SOURCE CODE:
#include<iostream>
int main()
{
float sum(float a,int b=10,int c=15,int =20);
int a=2,b=3,c=4,d=5;
cout<<"\nsum="<<sum(0);
cout<<"\nsum="<<sum(a,b,c,d);
cout<<"\nsum="<<sum(a,b,c);
cout<<"\nsum="<<sum(a,b);
cout<<"\nsum="<<sum(a);
cout<<"\nsum="<<sum(b,c,d);
return 0;
}
OUTPUT:
sum=45
sum=14
sum=29
sum=40
sum=47
sum=32
RESULT:
Thus, the given program is verified and executed successfully.
DATE:
AIM:
To implement the concept of function with default arguments.
ALGORITHM:
SOURCE CODE:
11
#include<iostream>
void printLine(char='_',int =70);
int main()
{
printLine();
printLine('/');
printLine('*',40);
printLine('R',55);
return 0;
}
void printLine(char ch, int Repeatcount)
{
int i;
std::cout<<std::endl;
for(i=0;i<Repeatcount;i++)
std::cout<<ch;
}
OUTPUT:
-----------------------------------
//////////////////////////////////////
****************************
RRRRRRRRRRRRRRRRRRRRRRR
RESULT:
AIM:
To write a C++ program to find the value of a number raised to its power using call by
value.
ALGORITHM:
SOURCE CODE:
#include<iostream>
int main()
{
int x,y;
double power(int,int);
std::cout<<"Enter x,y:"<<std::endl;
std::cin>>x>>y;
std::cout<<x<<" to the power "<<y <<" is "<< power(x,y);
}
OUTPUT:
Enter X, Y: 2 3
2 To the Power 3 is 8
RESULT:
Thus, the given program is verified and executed successfully.
DATE:
AIM:
To write a C++ program to implement the concept of Call by Address.
ALGORITHM:
SOURCE CODE:
15
#include<iostream>
void swap(int *x,int *y);
int main()
{
int i,j; i=10; j=20;
std::cout<<"\n the value of i before swapping is:"<<i;
std::cout<<"\n the value of i before swapping is:"<<j;
swap (&i,&j);
std::cout<<"\n the value of i after swapping is:"<<i;
std::cout<<"\n the value of i after swapping is:"<<j;
return(0);
}
RESULT:
Thus a program implementing the concept of Call by Address was verified and
successfully implemented.
AIM:
To write a program in C++ to implement the concept of call by reference.
ALGORITHM:
SOURCE CODE:
#include<iostream>
using namespace std;
void refer(int &a, int &b)
{
cout<<"\n\n The value of integer is:"<<a;
cout<<"\n\n The value of integer is:"<<b;
}
void refer(float a, float b)
{
cout<<"\n\n Value of floating is:"<<a; cout<<"\n\n Value of floating is:"<<b;
}
int main()
{
int x,y; float n,m;
cout<<"\n Enter the Integer number of two number\n";
cin>>x>>y;
cout<<"\n Enter the Floating point of two number\n";
cin>>n>>m;
refer(x,y);
refer(n,m);
return 0;
}
OUTPUT:
RESULT:
The Program to implement Call by Reference is successfully verified and executed using
C++.
18
AIM:
To write C++ program to implement inline function.
ALGORITHM:
SOURCE CODE:
//inline function
#include<iostream>
return(x*y); }
return(p/q);}
int main()
cout<<"\nINLINE FUNCTION\n";
OUTPUT:
INLINE FUNCTION
Multiplication:121.23
Division:1.25
RESULT:
The Program to implement the Inline Function has been successfully verified and
executed using C++.
EX.NO.2A CLASSES WITH PRIMITIVE DATA MEMBERS
20
DATE:
AIM:
To write a program in C++ to prepare a student Record using classes with primitive data
members.
ALGORITHM:
RESULT:
Thus the program in C++ to prepare a student Record using classes with primitive data
members was verified and executed successfully.
22
EX.NO.2B CLASSES WITH ARRAYS AS DATA MEMBERS
DATE:
AIM:
To write a program in C++ to display product detail using classes with array as data
members.
ALGORITHM:
SOURCE CODE:
#include<iostream>
using namespace std;
class product
{
int pro_code[50]; float pro_price[50]; int count;
public: void cnt()
{
count=0;
}
void getproduct(); void displaysum(); void displayproduct();
};
void product::getproduct()
{
cout<<"Enter product Code:";
cin>>pro_code[count];
cout<<"Enter product Cost:";
cin>>pro_price[count];
count++;
}
void product::displaysum()
{
float sum=0;
for(int i=0;i<count;i++)
sum=sum+pro_price[i];
cout<<"Total Value:"<<sum<<"\n";
}
void product::displayproduct()
{
cout<<" \nCode Price\n";
for(int i=0;i<count;i++)
{
cout<<"\n"<<pro_code[i];
cout<<" "<<pro_price[i];
}
cout<<"\n";
}
int main()
{
product obj;
obj.cnt();
int x;
do
{
cout<<"Enter choice\n";
cout<<"\n ";
cout<<"\n1.Add a product";
cout<<"\n2.Display a product total value";
24
OUTPUT:
Code Price
1234 3000
2345 4000
Enter choice
1. Add a product
2. Display a product total value
3. Display all products
4.Quit
2
Total Value:7000
Code Price
1234 3000
2345 4000
Enter choice
1. Add a product
2. Display a product total value
3. Display all products
4.Quit
4
RESULT:
Thus the C++ program for implementing arrays as data members was created, executed
and verified successfully.
EX.NO.2C CLASSES WITH POINTERS AS DATA MEMBERS
26
DATE:
AIM:
Write a program in C++ to implement the classes with pointers as data members.
ALGORITHM:
SOURCE CODE:
#include<iostream>
class data
{
public: int a;
void print()
{
std::cout<<"a:"<<a;
}
};
void main()
{
data d,*dp; dp=&d;
int data::*ptr=&data::a; d.*ptr=10;
d.print();
dp->*ptr=20; dp->print();
}
OUTPUT:
a:10
a:20
RESULT:
Thus the C++ program for implementing classes with pointers as data members was
verified and executed successfully.
28
AIM:
To write a program in C++ implements the concept of class with constant data member.
ALGORITHM:
SOURCE CODE:
#include <iostream>
using namespace std;
class Test
{
const int t; public:
Test(int t): t(t)
{}
int getT()
{
return t;
}
};
int main()
{
cout<<"constant data member"; Test t1(10);
cout<<"\nDefault t1:"<<t1.getT(); Test t2(20);
cout<<"\nDefault t2:"<<t2.getT();
return 0;
}
OUTPUT:
Default t1:10
Default t2:20
RESULT:
Thus the C++ program for implementing classes with constant data members was
verified executed successfully.
30
AIM:
To write a program in C++ to implement the concept of class with static member
functions.
ALGORITHM:
SOURCE CODE:
//STATIC MEMBER FUNCTION
#include<iostream>
using namespace std;
class test
{
public:
int code;
static int count;
void setcode(void)
{
code=++count;
}
void showcode(void)
{
cout<<"object number:"<<code<<"\n";
}
static void showcount(void)
{
cout<<"count:"<<count<<"\n";
}
};
int test::count;
int main()
{
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3; t3.setcode();
test :: showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
32
OUTPUT:
count:2
count:3
object number:1
object number:2
object number:3
RESULT:
Thus the C++ program for implementing classes with static member function was
verified and executed successfully.
33
AIM:
To write a C++ program to implement the friend function concept.
ALGORITHM:
1. Start the program.
2. Declare the class sample
3. Declare the variables a and b.
4. Define the member function setvalue() to assign the values for a and b.
5. Declare a friend function mean() using the keyword friend.
6. Define a parameterized function mean() with object s as a argument variable.
7. The object s is used to access the private data variables a and b.
8. Create the object x for the class sample.
9. Invoke the member function setvalue using x.
10. Display the value for mean.
11. Stop the program
34
SOURCE CODE:
#include<iostream>
using namespace std;
class sample
{
public:
int a; int b;
void setvalue()
{
a=25; b=40;
}
friend float mean(sample s);
};
float mean(sample s){
return float(s.a+s.b)/2.0;}
int main()
{
sample x;
x.setvalue();
cout<<"FRIEND FUNCTION:\n";
cout<<"Mean value ="<<mean(x)<<"\n";
return 0;
}
OUTPUT:
FRIEND FUNCTION:
Mean value =32.5
RESULT:
Thus the Program to implement the Friend Function has been successfully verified and
executed using C++.
35
AIM:
ALGORITHM:
1. Start the Program.
2. Create a class space and declare necessary data members and member functions and
operator function as member function.
3. The operator unary minus is overloaded to perform the operation of changing sign
4. Define member function getdata(), to get three values that is passed as arguments.
5. Define the operator overloading function to change the sign of the values.
6. Define the function display() to display the values before and after sign change.
7. Stop the program.
36
SOURCE CODE:
#include<iostream>
using namespace std;
class space
{
int x,y,z;
public:
void getdata(int a, int b, int c);
void display(void);
void operator-();
};
void space::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void space::display(void)
{
cout<<x<<" ";
cout<<y<<" ";
cout<<z<<"\n";
}
void space::operator-()
{
x=-x;
y=-y;
z=-z;
}
int main()
{ space s;
s.getdata(10,-20,30);
cout<<"s:";
s.display();
-s;
cout<<"s:";
s.display();
return 0;
}
37
OUTPUT:
S = 10 -20 30
S = -1020 -30
RESULT:
Thus the unary operator overloading concept was successfully executed and verified.
AIM :
To write a C++ program to implement the concept of Binary operator overloading.
ALGORITHM:
SOURCE CODE:
#include<iostream>
using namespace std;
class complex
{
float x;
float y;
public:
complex() {}
complex(float real, float imag)
{
x=real;
y=imag;
}
complex operator+(complex c);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<"+j"<<y<<"\n";
}
int main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1=";
c1.display();
40
cout<<"c2=";
c2.display();
cout<<"c3=";
c3.display();
return 0;
}
OUTPUT:
C1 = 2.5+j3.5
C2 = 1.6+j2.7
C3 = 4.1+j6.2
RESULT:
Thus the implementation concept of binary operator overloading was successfully
completed.
DATE:
AIM:
ALGORITHM:
SOURCE CODE:
#include<iostream>
using namespace std;
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);
int main()
{
cout<<"!!!VOLUME!!!\n";
cout<<volume(10)<<endl;
cout<<volume(10,20)<<endl;
cout<<volume(10,20,30)<<endl;
return 0;
}
OUTPUT:
1000
157.26
112500
RESULT:
Thus the implementation of function overloading was successfully implemented and
verified.
43
AIM:
To implement single inheritance using c++.
ALGORITHM:
1. Start the program.
2. Declare the base class emp.
3. Define and declare the function get() to get the employee details.
4. Declare the derived class salary.
5. Declare and define the function get1() to get the salary details.
6. Define the function calculate() as the member of class salary to find the net pay.
7. Define the function display() as the member of class salary to display the details of the
employee.
8. Create the object s for the derived class.
9. Read the number of employees.
10. Invoke the member functions get(),get1() and calculate().
11. Invoke the member function display().
12. Stop the program.
44
SOURCE CODE:
#include<iostream>
using namespace std;
class emp
{
public:
int eno;
char name[20],des[20];
void get()
{
cout<<"Enter the Employee Id : ";
cin>>eno;
cout<<"Enter the employee name:";
cin>>name;
cout<<"Enter the designation:";
cin>>des;
}
};
class salary:public emp
{
float bp,hra,da,pf,np;
public:
void get1()
{
cout<<"Enter the basic pay:";
cin>>bp;
cout<<"Enter the House Rent Allowance:";
cin>>hra;
cout<<"Enter the Dearness Allowance :";
cin>>da;
cout<<"Enter the Provident Fund:";
cin>>pf;
}
void calculate()
{
np=bp+hra+da-pf;
}
45
void display()
{
cout<<eno<<"\t"<<name<<"\t"<<des<<"\t "<<bp<<"\t "<<hra<<"\t "
<<da<<"\t"<<pf<<"\t"<<np<<"\n";
}
};
int main()
{
int i,n;
char ch;
salary s[10];
cout<<"\t EMPLOYEE DETAILS\n";
cout<<"\t \n";
cout<<"Enter the number of records :";
cin>>n;
for(i=0;i<n;i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<<"EmpId EmpName Designation BasicPay HRA\tDA\tPF\tNetPay\n";
for(i=0;i<n;i++)
{
s[i].display();
}
return 0;
}
46
EMPLOYEE DETAILS
RESULT:
The Program to implement Single Inheritance successfully verified and executed.
47
AIM:
To write a C++ program to implement multiple inheritance.
ALGORITHM:
1. Start the program.
2. Declare the base class student.
3. Declare and define the function get() to get the student details.
4. Declare the base class sports.
5. Declare and define the function getsm() to read the sports mark.
6. Declare the class statement derived from class student and class sports.
7. Declare and define the function display() to find out the total and average.
8. Declare the derived class object, call the functions get(),getsm() and display().
9. Stop the program.
48
SOURCE CODE:
#include<iostream>
using namespace std;
class student
{
protected:
int rollno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll Number :";
cin>>rollno;
cout<<"Enter the mark1 :";
cin>>m1;
cout<<"Enter the mark2 :";
cin>>m2;
}
};
class sports
{
protected:
int sm;
public:
void getsm()
{
cout<<"Enter the sports mark :";
cin>>sm;
}
};
class report: public student, public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
49
cout<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
};
int main()
{
report obj;
cout <<"\t STUDENT DETAILS\n";
cout <<"\t \n";
obj.get();
obj.getsm();
obj.display();
return 0;
}
OUTPUT:
STUDENT DETAILS
Total 248
Average 82
RESULT:
The Program to implement the Multiple Inheritance has been successfully verified and
executed using C++.
DATE:
AIM:
To write a C++ program to implement multilevel inheritance.
ALGORITHM:
1. Start the program.
2. Declare the base class student.
3. Declare and define the function getdata() to get the student details.
4. Declare and define the function putdata() to display the student details.
5. Declare the class test derived from student.
6. Declare and define the function gettest() to read the marks.
7. Declare and define the function puttest() to display the marks.
8. Declare the class result derived from test.
9. Declare and define the function displayresult() to find out the total marks.
10. Declare the derived class object, call the functions getdata(),gettest() and displayresult().
11. Stop the program.
51
SOURCE CODE:
#include<iostream>
using namespace std;
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<"Roll Number :"<<roll_number<<"\n";
}
};
class test :public student
{
protected:
float sub1,sub2;
public:
void get_marks(float x,float y)
{
sub1=x;
sub2=y;
}
void put_marks(void)
{
cout<<"\nMarks in Subject 1 ="<<sub1;
cout<<"\nMarks in Subject 2 ="<<sub2<<"\n";
}
};
class result: public test
{
protected:
52
float total;
public:
void display(void)
{
total=sub1+sub2;
put_number();
put_marks();
cout<<"Total :"<<total<<"\n";
}
};
int main()
{
result s;
cout<<"\t MULTILEVEL INHERITANCE";
cout<<"\n\t \n";
s.get_number(111);
s.get_marks(80.5,75.5);
s.display();
return 0;
}
OUTPUT:
MULTILEVEL INHERITANCE
RESULT:
The Program to implement the Multilevel Inheritance has been successfully verified and
executed using C++.
DATE:
AIM:
ALGORITHM:
1. Start the program.
2. Define the base class base.
3. Define the base class virtual function display() using the keyword virtual.
4. Derive the classes sub1, sub2 from base class base and define the function display() in
the respective classes.
5. Declare a base class pointer in main function.
6. Declare objects for d1 and d2 for the classes sub1 and sub2.
7. Assign the objects to the base pointer *bptr.
8. Invoke the display() function using -> pointer.
9. Depending upon the object in the bptr the appropriate
display() function is displayed.
SOURCE CODE:
#include<iostream>
class base
{
public:
virtual void display()
{
cout<<"Base class display is called\n";
}
};
OUTPUT:
VIRTUAL FUNCTION
RESULT:
Thus the C++ program for virtual function is verified and executed successfully.
AIM:
ALGORITHM:
1. Start the program
2. Include suitable header files
3. Create a base class student.
4. In the base class student define the function get_number() and put_number().
5. In the class sports define the function get_score() and put_score().
6. Derive a class test form base student and define the function get_mark() and
put_mark().
7. Derive a class result from test and sports class and define function display().
8. Create the object s for the class result using the object invoke the functions
get_number(), get_score(), get_mark() and display().
9. Stop the program
57
SOURCE CODE:
#include<iostream>
using namespace std;
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<"ROLL NO:"<<roll_number<<"\n";
}
};
class test :public virtual student
{
protected:
float part1,part2;
public:
void get_marks(float x,float y)
{
part1=x;
part2=y;
}
void put_marks(void)
{
cout<<"Marks Obtained:\n";
cout<<"Part 1 = "<<part1;
cout<<"\nPart 2 = "<<part2;
}
};
class sports: public virtual student
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
58
void put_score(void)
{
cout<<"\nSports wt : "<<score;
}
};
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_number();
put_marks();
put_score();
cout<<"\nTotal Score : "<<total<<"\n";
}
int main()
{
result s;
cout<<"\t VIRTUAL BASECLASS";
cout<<"\n\t \n";
s.get_number(678);
s.get_marks(30.5,25.5);
s.get_score(7.0);
s.display();
return 0;
}
59
OUTPUT:
VIRTUAL BASECLASS
ROLL NO:678
Marks Obtained:
Part 1 = 30.5
Part 2 = 25.5
Sports wt : 7
Total Score : 63
RESULT:
Thus the C++ program for virtual base class is verified and executed successfully.
60
ALGORITHM:
1. Start the program.
2. Create the class template test using the keyword template.
3. Declare the two member variables a and b of type T.
4. Define the parameterized constructor with two variables x and y in the argument list of
type T1 and T2.
5. The variables a and b acts as alias variable for x and y.
6. Objects test1,test2,test3 and test4 are created and the different type of data values are
passed to the constructor test.
7. Display the vales of a and b
8. Stop the process.
61
SOURCE CODE:
#include <iostream>
using namespace std;
template <class T1, class T2>
class test
{
T1 a;
T2 b;
public :
test (T1 x, T2 y)
{
a= x;
b= y;
cout << "\n\nThe value of a is : "<<a;
cout << "\nThe value of b is : "<<b;
cout <<" \n Sum is :" << a+b;
}
};
int main()
{
cout<< " CLASS TEMPLATES";
cout<<"\n ";
test <int,int> test1(10,20);
test <float,float> test2(15.5,25.5);
test <int,float> test3(20,25.5);
test <float,int> test4(15.5,30);
return 0;
}
62
OUTPUT:
CLASS TEMPLATES
The value of a is : 10
The value of b is : 20
Sum is :30
The value of a is : 20
The value of b is : 25.5
Sum is :45.5
RESULT:
Thus the program for implementing the concept of class template was successfully
verifies and executed successfully.
63
To write a C++ program for swapping two values using function templates
ALGORITHM:
1. Start the program.
2. Create the template class X using the keyword template.
3. Create the parameterized member function bubble() with two variables in the argument
list to perform bubble sort.
4. Define the member function swap() to swap the values of the variables a and b.
5. Display the values in the array before and after sorting.
6. Stop the program.
64
SOURCE CODE:
#include <iostream>
using namespace std;
template <class T>
void bubble(T a[],int n)
{
for(int i=0;i<n-1;i++)
for(int j=n-1;i<j;j--)
if ( a[j]<a[j-1])
swap(a[j],a[j-1]);
}
template <class X>
void swap(X &a, X &b)
{
X temp=a;
a=b;
b=temp;
}
int main()
{
int x[5]={6,4,8,2,1};
float y[5]={1.1,3.5,4.5,2.2,3.3};
cout<< " FUNCTION TEMPLATES";
cout<<"\n ";
cout <<"\nGiven X Array";
cout <<"\n ------------- \n";
for (int i=0;i<5;i++)
cout <<x[i]<<"\t";
bubble(x,5);
cout <<"\nSorted X Array";
cout <<"\n ------------- \n";
for ( i=0;i<5;i++)
cout <<x[i]<<"\t";
bubble(y,5);
return 0;
}
OUTPUT:
FUNCTION TEMPLATES
Given X Array
6 4 8 2 1
Sorted X Array
1 2 4 6 8
Given Y Array
RESULT:
Thus the C++ program for function template was verified successfully executed.