Professional Documents
Culture Documents
Oops
Oops
2
Write C++ program to define matrix and vector class, to use
function with default argument and to do matrix-vector
multiplication using friend function
7
3a Write a program to add two complex numbers using binary
operator overloading
10
3b Write a C++ program to implement complex number class with
necessary operator overloading and type conversion
12
4a Write a C++ program to Calculate Prime Number Using
Constructor
15
4b Write a C++ program to delete the object Using Destructor 17
4c Write the c++ program to implement the concept of copy
constructor
18
4d Write C++ program to define matrix class with dynamic
memory allocation and to use constructor, destructor, copy
constructor and assignment operator overloading.
19
5 Write C++ program to overload new and delete operators to
provide custom dynamic memo allocation
22
6 Write a C++ program to perform addition of complex numbers
using files.
24
7a Write a c++ program to multiply the positive numbers using
single inheritance.
26
7b Write a c++ program using multiple inheritances for collecting
employee details.
28
8a Write a c++ program for calculation of area of shapes using virtual
functions
30
8b Write a c++ program for a student mark list processing using
virtual base class.
31
9a write a c++ program using function template to find the
maximum of two datas 33
9b Write a c++ program using class template to find the greater of
the given two datas.
34B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
3
Ex no: 1a CLASS WITH STATIC DATA MEMBER
Aim
To write a c++ program to implement the concept of static data member in class.
Algorithm
Step 1: Start the program.
Step 2: Declare the static variable in the class.
Step 3: Increment the value of static variable and store it in another variable.
Step 4: Print the output of the static variable and the ordinary variable.
Step 5: Repeat the Step 3, 4 again and again.
Step 6: Stop the program.
Program:
#include<iostream.h>
class item
{
static int count; int num; public: void getdata(int a)
{
num=a; count++; cout<<Number<<num;
}
void showcount()
{
cout<<count; cout<<count<<\n;
} };
int item::count; int main()
{
item a,b,c; a.showcount(); b.showcount(); c.showcount(); a.getdata(20);
b.getdata(30);
c.getdata(40); a.showcount(); b.showcount();
c.showcount();
}
OUTPUT:
count0
count0
count0
Number20
Number30
Number40
count3
count3
count3
Result:
Thus the static data member of class is shared by all the instances of the
class.B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
4
Ex no: 1b CLASS WITH STATIC MEMBER FUNCTION
Aim
To write a c++ program to implement the concept of static function in class.
Algorithm
Step 1: Start the program.
Step 2: Declare the static variable and static function in the class.
Step 3: Increment the value of static variable and store it in another variable.
Step 4: Print the output of the static variable and the ordinary variable.
Step 5: Repeat the step 3, 4 again and again.
Step 6: Stop the program.
Program:
#include<iostream. h>
#include<conio.h>
class name
{
static int count; int num;
public:
void setnum()
{
cout<<"\nInitialycount:"<<count; //cin>>count;
num=++count;
}
void shownum()
{
cout<<"\nNum:"<<num;
}static void show(); };
int name::count;
void name::show(){
cout<<"\nCount:"<<count;
}
void main()
{
name n1,n2; clrscr(); n1.setnum(); n1.shownum(); n2.setnum(); n2.shownum();
name::show(); name n3; n3.setnum(); name::show(); n3.shownum(); getch();
}
Output:
Initial count=0
Num=1
Count=1
Initial count=1
Num=2
Count=2B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
5
Initial count=2
Count=3
Num=3
Result:
Thus static member function of class is shared by all the instances of the class and
a static
member functions cannot access auto members of a classB.Tech-IT Object Oriented
Programming Lab (Staff Manual)-2013 BITL1201
6
Exno: 1c FUNCTION WITH DEFAULT ARGUMENT
Aim
To write a c++ program using function with default argument.
Algorithm:
Step 1: Start the program.
Step 2: Declare the function containing a character and an integer.
Step 3: Print a character for specified number of times by
i. Passing no arguments
ii. Passing one arguments
iii. Passing both the arguments.
Step 4: Stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class value
{
public:
void print(char='*',int=5); }; void value::print(char y,int z)
{
int i; for(i=1;i<=z;i++) cout<<y; cout<<"\n";
}
void main()
{
value v1,v2,v3,v4; clrscr();
v1.print();
v2.print(67);
v3.print('+');
v4.print('-',6);
getch();
}
Output:
*****
ccccc
+++++
-----Result:
Thus in c++ an operation can be performed using functions with or without passing
arguments.B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013
BITL1201
7
Ex.No.2 MATRIX-VECTOR MULTIPLICATION USING FRIEND FUNCTION
Aim
To write C++ program to define matrix and vector class, to use function with default
argument and to do matrix-vector multiplication using friend function.
Algorithm
int v[10];
public:
//default argument is 3 void getvector(int n=3);
void disvector(void);
friend void multiply(matrix &, vector &); };
void vector::getvector(int n)
{ int i;
cout<<"\nEnter elements for vector one by one... \n"; for(i=0;i<n;i++) cin>>v[i];
}
void vector::disvector()
{
int i;B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
8
cout<<"\nThe vector elements are...\n"; for(i=0;i<3;i++)
cout<<v[i]<<"\t";
}
void matrix::getmatrix()
{
int i,j;
cout<<"\nEnter the matrix...\n"; for(i=0;i<3;i++) for(j=0;j<3;j++)
cin>>m[i][j];
}
void matrix::dismatrix()
{
int i, j;
cout<<"\nEntered matrix is...\n"; for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<m[i][j]<<"\t";
cout<<"\n";
}
}
void multiply(matrix &m1, vector &v1)
{
int ans[3], i, j;
cout<<"\nThe resultant matrix... \n"; for(i=0;i<3;i++)
{
ans[i]=0;
for(j=0;j<3;j++) ans[i]+=m1.m[i][j] * v1.v[j]; cout<<ans[i]<<"\t";
}
}
void main()
{
matrix m1; vector v1; clrscr();
m1.getmatrix(); m1.dismatrix();
v1.getvector(); //no argument, default value will be taken v1.disvector();
multiply(m1,v1);
getch();
}
Output
Enter the matrix...
2
2
2
2
2
2
2
2
2B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
9
Entered matrix is...
22
22
22
Enter elements for vector one by one...
2
The resultant matrix...
1
2
1
2 12
Result
Thus a friend function is used for accessing the non-public members of a
class.B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
10
Ex.No.3a BINARY OPERATOR OVERLOADING
Aim
To write a program to add two complex numbers using binary operator overloading.
Algorithm
a. Addition:
(a+bi) + (x + yi) = ((a+x)+(b+y)i)
b. Subtraction:
(a+bi) - (x + yi) = ((a-x)+(b-y)i)
c. Multiplication:
(a+bi) * (x + yi) = (((a*x)-(b*y)) + ((a*y) + (x*b))i)
d. Division :
i.d=(x*x) + (y*y)
ii.(a+bi) / (x + yi) = (((a*x)+(b*y))/d) + (((b*x)-(a*y))/d)i
Step 6: Create the objects and pass the complex values.
Step 7: Invoke the overloaded functions.
Step 8: Display the converted result.
Step 9: Stop the process.
Program:
#include<iostream.h>
#include<conio.h>
#include<iomani.h>
class complex
{
private:
float real; float imag;
public: complex()
{
real=imag=0.0;
}
complex(int r,int i) //conversion constructor
{
real = r; imag = i;
}
complex(double r, double i)//conversion constructorB.Tech-IT Object Oriented
Programming Lab (Staff Manual)-2013 BITL1201
13
s{
real = r; imag = i;
}
friend istream& operator>>(istream &, complex &);
friend ostream& operator<<(ostream &, complex &);
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
friend double condou(complex t); //complex>double };
double condou(complex t)
{
return t.real+t.imag;
}
istream& operator >>(istream &in, complex &c)
{
cout<<\nReal Part:; in>>c.real; cout<<Imag Part:; in>>c.imag;
return in;
}
ostream& operator<<(ostream &out, complex &c)
{
void main()
{
complex c1, c2, c3,c4(4,9),c5(3.23004,4.666304444); double t;
clrscr();
t=condou(c5);
cout<<\nEnter complex number 1: ; cin>>c1;
cout<<\nEnter complex number 2: ; cin>>c2;
cout<<\nEnter complex numbers are:; cout<<\nComplex 1: <<c1;
cout<<\nComplex
2: <<c2; c3=c1+c2;
cout<<\nResult of addition is:<<c3; c3=c1-c2;
cout<<\nResult of subtraction is:<<c3; c3=c1*c2;
cout<<\nResult of multiplication is:<<c3; c3=c1/c2;
cout<<\nResult of division is:<<c3; cout<<\nInteger>complex:<<c4;
cout<<\nDouble>complex:<<c5; cout<<\nConverted to double<<t; getch();
}
Output:
Enter complex number 1:
Real Part:2
Imag Part:4
Enter complex number 2:
Real Part:3
Imag Part:5
Enter complex numbers are:
Complex 1: 2+4i
Complex 2: 3+5i
Result of addition is:5+9i
{
if(k==1)B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
16
cout<< \n\tA is prime Number. "; else
cout<<"\n\tA is Not prime.";
}
};
void main()
{
clrscr(); int a;
cout<<"\n\tEnter the Number:"; cin>>a;
prime obj(a); obj.calculate(); obj.show(); getch();
}
Output
Enter the number: 7
Given number is Prime Number
Result
Thus the program for constructor is executed and verified.B.Tech-IT Object Oriented
Programming Lab (Staff Manual)-2013 BITL1201
17
Ex.No.4b DESTRUCTOR
Aim
To write a C++ program to delete the object Using Destructor.
Algorithm
STEP 1: Start the program.
STEP 2: Declare the class as Line with data members and Member functions. STEP
3:
code(int a)
{
id=a;
}
code(code&x)
{
id=x.id;
}
void display(void)
{
cout<<id;
}
};
int main()
{
clrscr(); code a(100); code b(a); code c=a; code d; d=a;
cout<<"\nid of a:"; a.display(); cout<<"\nid of b:"; b.display(); cout<<"\nid of c:";
c.display(); cout<<"\nid of d:"; d.display(); getch();
return 0;
}
Output:
id of A:100 id of B:100 id of C:100 id of D:100
Result:
Thus the c++ program for implementation of copy constructor was successfully
executed.B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
19
Ex.No.4d MATRIX CLASS WITH DYNAMIC MEMORY ALLOCATION
Aim
To write C++ program to define matrix class with dynamic memory allocation and
to use
constructor, destructor, copy constructor and assignment operator overloading.
Algorithm
1. Define matrix Class.
2. Define default constructor
3. Declare constructor for dynamic memory allocation
4. Declare matrix destructor
5. Declare the functions
6. Declare the assignment operator overloading function within the class
7. Define all the functions.
8. Create objects for matrix class in main() function.
9. The memory for the objects is dynamically allocated.
10. Invoke copy constructor function
11. Invoke assignment operator overloading function.
Program
//Matrix Class - Dynamic Memory Allocation
/*Program to explain Constructor, Destructor, Copy constructor and Assignment
operator
overloading*/
#include<iostream.h> #include<conio.h> class matrix
{
int **m;
int row, col; public: matrix()
{ row=col=0; m=NULL;
}
}
Output
Enter rows and cols of the matrix...
2
2
Enter the matrix elements one by one...
4
4
4
4
Entered matrix is...B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013
BITL1201
21
44
44
Copy constructor invoked...
Result of copy constructor is...
Assignment Operator Overloading...
Result of assignment operator overloading...
44
44
Result
Thus the class with dynamic memory allocation is created and executed.B.Tech-IT
Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
22
Ex.No.5 OVERLOADING NEW AND DELETE OPERATOR
Aim
To write C++ program to overload new and delete operators to provide custom
dynamic
memo allocation.
Algorithm
1. Define vector Class.
2. Define new overloaded method a. Accepts the size as input
3. Memory is created using malloc()
4. If there is no memory available, execution will be stopped.
5. Address is returned.
6. Define delete overloaded method
7. Object is removed from the memory using free()
8. Declare the functions read(), max() and sum()
9. Define all the functions.
10. Create objects for vector class in main() function.
11. The memory for the objects is dynamically allocated.
12. Invoke the functions dynamically.
13. Display the results.
14. Invoke the delete operator.
Program
/*Overloading new and delete operator using malloc and free*/
#include<iostream.h>
#include<conio.h> #include<stdlib.h> class vector
{ private: int *array; public:
void *operator new(size_t size)
{
void *v;
cout<<"\nOperator new invoked..."; v=malloc(size);
if(!v)
{
cout<<"Unable to allocate memory"; exit(0);
}
return v;
}
void operator delete(void* v)
{
cout<<"\nOperator delete invoked..."; free (v);
}
void read(int); int max(int); int sum(int); };
void vector::read(int s)
{
for(int i=0; i<s; i++)B.Tech-IT Object Oriented Programming Lab (Staff Manual)2013 BITL1201
23
{
cout<<"\nEnter element "<<i+1<<":"; cin>>array[i];
}
}
int vector::sum(int s)
{
int tot=0;
for(int i=0; i<s; i++) tot+=array[i]; return tot;
}
int vector::max(int s)
{
int max=0;
for(int i=0;i<s;i++) if(array[i]>max) max = array[i]; return max;
}
void main()
{
int s; clrscr();
cout<<"\nEnter how many elements..."; cin >> s;
vector *vec = new vector; cout<<"Enter vector data... \n"; vec->read(s);
cout<<"\nThe max is..."<<vec->max(s); cout<<"\nThe sum is..."<<vec->sum(s);
delete
vec;
getch();
}
Output
Enter how many elements... 3
Operator new invoked...
Enter vector data...
Enter element 1:45
Enter element 2:32
Enter element 3:67
The max is 67
The sum is 144
Operator delete invoked...
Result
Thus the new and delete operator is overloaded along with dynamic memory
allocation.B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
24
real = imag = 0;
}
void display(void); };
void complex::display(void)
{
cout<<real<<((imag<0)?"-i":"+i")<<imag<<"\n";
}
void main()
{
clrscr();
ofstream ocom("complex1.txt"); float real,imag;
time_t ti;
srand((unsigned) time(&ti)); real = rand()%100;
imag = rand()%100; ocom<<"("<<real<<((imag<0)?"-i":"+i")<<imag<<")"<<"+";
real =
rand()%100;B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013
BITL1201
25
imag = rand()%100; ocom<<"("<<real<<((imag<0)?"i":"+i")<<imag<<")"<<"\n";
ocom.close();
ifstream icom("complex1.txt"); char no,t,ch,op;
icom>>no;
icom>>real;
icom>>ch;
icom>>no;
icom>>imag; imag=(ch=='+')?imag:-imag; icom>>no;
icom>>op;
complex a(real,imag); icom>>no; icom>>real; icom>>ch; icom>>no;
icom>>imag;
imag=(ch=='+')?imag:-imag; icom>>no;
icom>>op;
complex b(real,imag); complex c; switch(op)
{
case '+':
c.real = a.real+b.real; c.imag = a.imag+b.imag; break;
case '-':
c.real = a.real-b.real; c.imag = a.imag-b.imag; break;
case '*':
c.real = (a.real*b.real)-(a.imag*b.imag); c.imag = (a.real*b.imag)+(a.imag*b.real);
break;
case '/': float qt;
qt = b.real*b.real+b.imag*b.imag;
c.real = (a.real*b.real+a.imag*b.imag)/qt; c.imag = (a.imag*b.reala.real*b.imag)/qt; break;
default:
cout<<"\n Invalid";
}
cout<<"\n complex 1:"; a.display();
cout<<"\n complex 2:"; b.display();
cout<<"\n Resultant complex:"; c.display();
ofstream out("result.txt"); out<<"("<<c.real<<((c.imag<0)?"i":"+i")<<c.imag<<")";
out.close();
}
Output:
complex1.txt:
(0+i72)+(39+i71)
result.txt
(39+i143)
Result:
Thus the program for addition of complex numbers using files was executed.B.TechIT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
26
Ex No:7A MULTIPLICATION OF POSTIVE NUMBERS USING SINGLE
INHERITANCE
Aim:
To write a c++ program to multiply the positive numbers using single inheritance.
Algorithm:
1. Define a Nase class B
2. Class B has one private data member ,one public data member and three
member
functions.
3. Define Derived class D.All the Properties of B is privately inherited.
4. Create a derived class object.
5. Access the base class data members .
6. Do the multiplication.
7. And display the results.
Program:
#include<iostream.h>
#include<conio.h> class B
{
27
showall();
cout<<"b="<<b<<"\n"<<"c="<<c<<"\n";
}
void main()
{
clrscr(); D d; d.mul();
d.display();
getch();
}
Output:
Enter the values for a&b:3 4
a=3
b=4
c=12
Result:
Thus the C++ program to implement Single inheritance was executed and output
verified.B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
28
Ex No:7b EMPLOYEE DETAILS USING MULTIPLE INHERITANCE
Aim:
To write a c++ program using multiple inheritance for collecting employee details.
Algorithm:
1. Define a person class.
2. Define a Employee class.
3. Inherit all the properties of employee, person class into manager class, in public
mode.
cout<<"empno:"<<empno<<endl;
cout<<"salary:"<<salary<<endl;
}};
class manager:public person,public employee
{
char position[26]; public:
void read()
{B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
29
get();
getdata();
cout<<"\n enter the position:"<<endl; cin>>position;
}
void write()
{
disp();
display();
cout<<"\n position:"<<position; }};
void main()
{
clrscr(); manager m; m.read(); m.write(); getch();
}
Output
empno:23
salary:2500
enter the name:kannan
void main()
{
clrscr();
char ch1,ch2,ch;
cout<<"enter two characters(ch1,ch2):"; cin>>ch1>>ch2;
ch=max(ch1,ch2);
cout<<"max(ch1,ch2):"<<ch<<endl; int a,b,c;
cout<<"enter 2 integers"; cin>>a>>b;
c=max(a,b);
cout<<"max(a,b):"<<c<<endl; float f1,f2,f3;
cout<<"enter 2 floats<f1,f2>:"; cin>>f1>>f2;
f3=max(f1,f2);
cout<<"max(f1,f2):"<<f3<<endl;
getch();
}
Output:
enter two characters(ch1,ch2):a s max(ch1,ch2):s
enter 2 integers2 4 max(a,b):4
enter 2 floats<f1,f2>:2.6 4.5 max(f1,f2):4.5
Result:
Thus the C++ program to implement virtual function was executed and output
verified.B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
31
Ex No:8b STUDENT MARKLIST USING VIRTUAL BASE CLASS
Aim:
To write a c++ program for a student mark list processing using virtual base class.
Algorithm:
void puts()
{
cout<<"enter the sports marks:\t"; cin>>m4;
}};
class result:virtual public marks,virtual public sportsmarks
{
public: int tot; float avg;
void display()
{
tot=m1+m2+m3+m4;
avg=tot/4;
}B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
32
void demo()
{
cout<<"\n m1="<<m1<<"m2="<<m2<<"m3="<<m3<<"m4="<<m4; cout<<"
the sports
marks";
cout<<"total=\t"<<tot; cout<<"\n avg is\t"<<avg; }};
void main()
{
clrscr();
result r; r.gets(); r.details(); r.puts(); r.display(); r.demo(); getch();
}
Output
student name: isac
enter student roll no: 10
{
char a1[10],b1[10]; cout<<"\n enter 2 strings:"; cin>>a1>>b1; if(strcmp(a1,b1)>0)
{
cout<<"a1 has the higest length"<<a1;
}
else
{B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
35
cout<<"b1 has the highest length"<<b1;
}}};
void main()
{
clrscr();
demo<int>d1;
d1.gets();
d1.operation();
demo<float>d2;
d2.gets1();
d2.operation();
demo<char>d3;
d3.compare();
getch();
}
Output:
Enter 2 integers: 2 3
b is greater:3 enter 2 floats: 5 4