Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 49

B.

Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201


1
Syllabus
BITL1201 OBJECT ORIENTED PROGRAMMING LAB 0 0 3 1
1. Design C++ classes with static members, methods with default arguments,
2. Develop friend function to do matrix-vector multiplication
3. Implement complex number class with required operator overloading and type
conversion.
4. Implement matrix class with dynamic memory allocation and required methods.
5. Overload the new and delete operators to provide custom dynamic allocation of
memory.
6. Write a C++ program that randomly generates complex numbers
7. Develop a program that implements inheritance
8. Implement Virtual function
9. Implement template B.Tech-IT Object Oriented Programming Lab (Staff Manual)2013 BITL1201
2
Exercises
Table of Contents
Ex.No. Name of the Exercises Page no.
1a Write a c++ program to implement the concept of static data
member in class.
3
1b Write a c++ program to implement the concept of static
function in class
4
1c Write a c++ program using function with default argument 6

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

1. Declare vector Class


2. Define matrix Class
3. Declare friend function multiply() inside the matrix class
4. Define vector Class
5. Declare friend function multiply() inside the vector class
6. Define getvector() function with for loop to get the elements for vector
7. Define disvector() function with for loop to display the contents of vector
8. Define getmatrix() function with nested for loops to get the matrix elements
9. Define dismatrix() function with nested for loops to display the matrix
10. Define the multiply() to multiply matrix and vector
i. No of columns in the matrix should be equal to no. of elements in the vector
ii. Apply the matrix- vector multiplication mechanism.
iii. Display the resultant matrix in the screen
11. Define main () to create objects and to call the defined functions
Program
#include<iostream.h> #include<conio.h>
class vector; //class declaration - needed, because this class referred before it is
defined in
the friend function.
class matrix
{
int m[3][3];
public:
void getmatrix(void); void dismatrix(void);
friend void multiply(matrix &, vector &); };
class vector
{

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

Step 1: Start the program. Step 2: Declare the class.


Step 3: Declare the variables and its member function.
Step 4: Using the function getvalue() to get the two numbers.
Step 5: Define the function operator +() to add two complex numbers. Step 6:
Define the
function operator ()to subtract two complex numbers. Step 7: Define the display
function.
Step 8: Declare the class objects obj1,obj2 and result. Step 9: Call the function
getvalue
using obj1 and obj2
Step 10: Calculate the value for the object result by calling the function operator +
and
operator -.
Step 11: Call the display function using obj1 and obj2 and result. Step 12: Return
the
values.
Step 13: Stop the program.
PROGRAM:
#include<iostream.h> #include<conio.h> class complex
{
int a,b;
public:
void getvalue()
{
cout<<"Enter the value of Complex Numbers a,b:"; cin>>a>>b;
}
complex operator+(complex ob)
{

complex t; t.a=a+ob.a; t.b=b+ob.b; return(t);


}
complex operator-(complex ob)
{
complex t; t.a=a-ob.a; t.b=b-ob.b; return(t);
}
void display()
{
cout<<a<<"+"<<b<<"i"<<"\n";
}
};
void main()
{
clrscr();
complex obj1,obj2,result,result1;B.Tech-IT Object Oriented Programming Lab (Staff
Manual)-2013 BITL1201
11
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2; result1=obj1-obj2;
cout<<"Input Values:\n"; obj1.display(); obj2.display(); cout<<"Result:";
result.display();
result1.display();
getch();
}
Output
Enter the value of Complex Numbers a, b 4 5

Enter the value of Complex Numbers a, b


22
Input Values
4+ 5i
2+ 2i
Result
6+ 7i
2+ 3i
Result
Thus the program for operator overloading is executed and verified.B.Tech-IT Object
Oriented Programming Lab (Staff Manual)-2013 BITL1201
12
Ex.No.3b COMPLEX NUMBERS WITH OPERATOR OVERLOADING AND TYPE
CONVERSION
Aim:
To write a C++ program to implement complex number class with necessary
operator
overloading and type conversion
Algorithm:
Step 1: Start the program.
Step 2: Create a class Complex.
Step 3: Define the default and two parameterized constructors. One constructor is
having
two integer arguments and another one will have the two double data type
arguments
Step 4: Declare the operator function which are going to be overloaded. Step 5:
Define the
overloaded functions such as +,-,*,/,>>,<<.

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

if (c.imag<0) out<<c.real<<c.imag<<i; else


out<<c.real<<+<<c.imag<<i; return out;
}
complex complex::operator+(complex c)
{
complex temp; temp.real = real+c.real;
temp.imag = imag+c.imag; return temp;
}
complex complex::operator-(complex c)
{
complex temp; temp.real = real-c.real;
temp.imag = imag-c.imag; return temp;
}
complex complex::operator*(complex c)
{
complex temp; float t=c.real;
temp.real = real*c.real-imag*c.imag; temp.imag = real*c.imag+imag*t; return
temp;
}
complex complex::operator/(complex c)
{
complex temp; float qt;
float res=(imag*c.real-real*c.imag); qt = c.real*c.real+c.imag*c.imag;
temp.real = (real*c.real+imag*c.imag)/qt; temp.imag = res/qt;
return temp;
}B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
14

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

Result of subtraction is:-1-1i


Result of multiplication is:-14+22i
Result of division is:0.764706+0.058824i
Integer->complex:4+9i
Double->complex:3.23004+4.666305i
Converted to double7.896345
Result:
Thus the program for operator overloading for complex numbers and their type
conversions
was executed.B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013
BITL1201
15
Ex.No.4 a CONSTRUCTOR
Aim
To write a C++ program to Calculate Prime Number Using Constructor
Algorithm
Step 1: Start the program.
Step 2: Declare the class as Prime with data members,Member functions.
Step 3: Consider the argument constructor Prime() with integer Argument.
Step 4: To cal the function calculate() and do the following steps.
Step 5: For i=2 to a/2 do
Step 6: Check if a%i==0 then set k=0 and break.
Step 7: Else set k value as 1.
Step 8: Increment the value i as 1.
Step 9: Check whether the k value is 1 or 0.
Step 10: If it is 1 then display the value is a prime number.
Step 11: Else display the value is not prime.

Step 12: Stop the program.


Program
#include<iostream.h> #include<conio.h>
class prime
{
int a,k,i; public: prime(int x)
{
a=x;
}
void calculate()
{
k=1;
{
for(i=2;i<=a/2;i++)
if(a%i==0)
{
k=0;
break;
}
else
{
k=1;
}
}
}
void show()

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

Consider the constructor Line()


STEP 4: Declare the destructor using tilde symbol as same as created constructor
Line().
STEP 5: Declare the Length as double under the private specifier. STEP 6: Set the
value for
length in main function
STEP 7: print the length of line STEP 8: Stop the program.
Program
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len ); double getLength( void );
Line(); // This is the constructor declaration ~Line(); // This is the destructor:
declaration
private: double length;
}; // Member functions definitions including constructor Line::Line(void)
{cout << "Object is being created" << endl;}
Line::~Line(void)
{cout << "Object is being deleted" << endl;}
void Line::setLength( double len )
{length = len;}
double Line::getLength( void )
{return length;}
// Main function for the program int main( )
{Line line;

// set line length line.setLength(6.0);


cout << "Length of line : " << line.getLength() <<endl;
return 0;}
Output:
Object is being created
Length of line : 6
Object is being deleted
Result:
Thus the Program for executed and verified.B.Tech-IT Object Oriented Programming
Lab (Staff Manual)-2013 BITL1201
18
Ex. No: 4c COPY CONSTRUCTOR
Aim
To write the c++ program to implement the concept of copy constructor.
Algorithm
1) Start the program
2) Create a class code, declare the data members and define the member functions
code ()
and display ().
3) Create the object and call the constructors.
4) Assign the value of one object to another object
5) Call the display () to print the id of created objects.
6) Stop the program
Program
#include<iostream.h> #include<conio.h> class code
{
int id; public: code() {}

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;
}

matrix(int r ,int c); ~matrix();


void getmatrix(); void showmatrix(); matrix(matrix &m2);
//copy constructor matrix& operator=(matrix &m2); };
matrix::~matrix()
{
for(int i=0;i<row;i++) delete m[i];
delete m;
}
matrix::matrix(int r ,int c)
{
row = r; col = c;
m = new int*[row]; for(int i=0;i<row;i++) m[i]=new int[col];
}
matrix::matrix(matrix &m2)
{
cout<<"\nCopy constructor invoked...\n"; row = m2.row;B.Tech-IT Object Oriented
Programming Lab (Staff Manual)-2013 BITL1201
20
col = m2.col;
m = new int*[row]; for(int i=0;i<row;i++) m[i]=new int[col]; for(i=0;i<row;i++)
for(int
j=0;j<row;j++) m[i][j]=m2.m[i][j];
}
matrix& matrix::operator=(matrix &m2)
{
cout<<"\nAssignment Operator Overloading...\n"; row = m2.row;
col = m2.col;

m = new int*[row]; for(int i=0;i<row;i++) m[i]=new int[col]; for(i=0;i<row;i++)


for(int
j=0;j<row;j++) m[i][j]=m2.m[i][j]; return *this;
}
void matrix::getmatrix()
{
for(int i=0;i<row;i++) for(int j=0; j<col; j++) cin>>m[i][j];
}
void matrix::showmatrix()
{
for(int i=0;i<row;i++)
{
for(intj=0;j<col;j++) cout<<"\t"<<m[i][j]; cout<<"\n";
}
}
void main()
{
int r,c; clrscr();
cout<<"\nEnter rows and cols of the matrix...\n"; cin>>r>>c;
matrix m1(r,c);
cout<<"\nEnter the matrix elements one by one..."; m1.getmatrix();
cout<<"\nEntered matrix is...\n";
m1.showmatrix();//invoking copy constructor matrix m2=m1;
cout<<"\nResult of copy constructor is...\n"; m2.showmatrix();
matrix m3; m3=m1;
cout<<"\nResult of assignment operator overloading...\n"; m3.showmatrix();
getch();

}
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

Ex.No 6 OPERATIONS ON COMPLEX NUMBERS USING FILES AS STORAGE


Aim:
To write a C++ program to perform addition of complex numbers using files.
Algorithm:
Step 1: Start the program.
Step 2: Create a class called COMPLEX and create its members: real and imaginary.
Step 3: Generate the random numbers by using rand() function.
Step 4: Write the randomly generated numbers in a file called complex1.txt.
Step 5: Add the two complex numbers.
Step 6: Store the Resultant complex number in another file called result.txt
Step 7: Display the resultant value.
Step 8: Stop the program.
Program:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<time.h> class complex
{
public: int real; int imag;
complex(int r, int i)
{
real = r; imag = i;
}
complex()
{

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
{

int a; public: int b;


void getab(); int geta(void);
void showall(void); };
class D:private B
{
int c; public:
void mul(void); void display(void); };
void B::getab()
{
cout<<"\n enter the values for a&b:"; cin>>a>>b;
}
int B::geta()
{
return a;
}
void B::showall()
{
cout<<"a="<<a<<"\n";
}
void D::mul()
{
getab();
c=b*geta();
}
void D::display()
{B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201

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.

4. Display Manager Information.


Program:
#include<iostream.h>
#include<conio.h> class person
{
int age,empno; char name[15]; public:
void getdata()
{
cout<<"\n enter the name:"; cin>>name;
cout<<"\n enter the age:"; cin>>age;
}
void display()
{
cout<<"\n Age:"<<age; cout<<"\n Name:"<<name; }};
class employee
{
public:
int empno; float salary; void get()
{
cout<<"empno:"<<endl;
cin>>empno;
cout<<"salary:";
cin>>salary;
}
void disp()
{

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

enter the age:26


enter the position: supervisor empno:23 salary:2500
Age:26
Name:kannan
position:supervisor
RESULT:
Thus the C++ program to implement Multiple inheritance was executed and output
verified.B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
30
Ex.No 8a CALCULATION OF AREA OF SHAPES USING VIRTUAL
FUNCTION
Aim:
To write a c++ program for calculation of area of shapes using virtual functions.
Algorithm:
1. Define a shape class .
2. Define a virtual function show().
3. Define Rectangle class and inherit the base class.
4. Create a pointer and object for base class.
5. Display the rectangle and triangle information.
Program:
#include<iostream.h>
#include<conio.h>
#include<string.h> template<class t> t max(t a,t b)
{
if (a>b) return a; else return b;
}

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:

1. Define a student class and make this as virtual.


2. Define marks class and Inherit the student class virtually.
3. Define sports class and inherit properties of student virtually.
4. Define result class and inherit marks, sports into result.
5. Display student information with marks.
Program:
#include<iostream.h>
#include<conio.h>
class student
{
public:
char name[10]; int rollno; void gets()
{
cout<<"student name:\t"; cin>>name;
cout<<"enter student roll no:\t"; cin>>rollno;
}};
class marks:virtual public student
{
public:
int m1,m2,m3; void details()
{
cout<<"\n enter the 3 subjects marks:\t"; cin>>m1>>m2>>m3;
}};
class sportsmarks:virtual public student
{
public: int m4;

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

enter the 3 subjects marks: 78 78 82


enter the sports marks: 65
m1=78m2=78m3=82m4=65 the sports markstotal= 303 avg is 75
Result:
Thus the C++ program to implement virtual base class was executed and output
verified.B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
33
Ex No:9a FUNCTION TEMPLATE
Aim:
To write a c++ program using function template to find the maximum of two datas.
Algorithm:
1.Create the function template, to find the maximum of two datas.
2.Get different data type values like integer, float and character.
3.Find maximum of those data using template function.
4.Finally print the result.
Program:
#include<iostream.h>
#include<conio.h>
#include<string.h>
template<class t> t max(t a,t b)
{
if (a>b) return a; else return b;
}
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 Function Template was executed and output
verified.B.Tech-IT Object Oriented Programming Lab (Staff Manual)-2013 BITL1201
34
Ex No:9B CLASS TEMPLATE
Aim:
To write a c++ program using class template to find the greatest of the given two
datas.
Algorithm:
1. Create the class template, to find the greatest of two datas.
2. Get different data type values like integer, float and character.
3. Use a function to find the maximum of two values for both int, float .

4. Find maximum of those data using template function.


5. Finally print the result.
Program
#include<iostream.h>
#include<conio.h>
#include<string.h>
template<class t>
class demo
{
public: int i;
t a,b;
void gets()
{
cout<<"\n enter 2 integers:"; cin>>a>>b;
}
void gets1()
{
cout<<"\n enter 2 floats:"; cin>>a>>b;
}
void operation()
{
if(a>b)
cout<<"\n a is greater:"<<a; else
cout<<"\n b is greater:"<<b;
}
void compare()

{
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

a is greater:5 enter 2 strings: 5 6


b1 has the highest length6
Result:
Thus the C++ program to implement class template was executed and output
verified.

You might also like