Object Oriented Programming Laboratory

SUDHARSAN ENGINEERING COLLEGE
Sathiymangalam, Pudukottai – 622 501
Department of MCA
MC9226 Object Oriented Programming Lab
Lab Manual
Year/Sem
: I/ II
Batch
: 2012-2015
Academic year : 2012- 2013
Faculty Name
: V. JAYALAKSHMI
ENUMERATION
Aim:
To write a c++ program to implement the enumerated data type.
Enumeration
It adds elegance to programs and can be used to set up collections of named integer
constants.
Algorithm
Step 1: Start the program.
Step 2: Define the class ‘lights’.
Step 3: declare the enumerated data type ‘state’ and contain values ‘red, green, yellow’ as
data
Step 4: define the member function ‘setstate’ that prints the messages stop, go, ready
depending upon the value of the enumerated variable
Step 5: create the object ‘a1’ and invoke the function.
Step 6: stop the program
Program:
#include<iostream.h>
#include<conio.h>
class lights
{
public:
enum state {red, green, yellow} as;
void setstate (state s)
{
switch(s)
{
case red:
cout<<"\t\tSTOP";
break;
case green:
cout<<"\t\tGO";
break;
case yellow:
cout<<"\t\tREADY";
break;
default:
return;
}
}
};
void main ()
{
int choice;
char opt;
lights a1;
clrscr ();
do
{
cout<<"\n\n\n\n\t\t 0 (or) 1 (or) 2\t ENTER YOUR CHOICE”;
cin>>choice;
If (choice==0)
a1.setstate (lights::red);
else if (choice==1)
a1.setstate (lights::green);
else if (choice==2)
a1.setstate (lights::yellow);
cout<<"\t\tCONTINUE[Y/N]?:";
cin>>opt;
} while (opt=='y');
cout<<endl;
getch ();
}
Output:
0 (or) 1 (or) 2 ENTER YOUR CHOICE 0
STOP
CONTINUE[Y/N]
Y
0 (OR) 1 (OR) 2 ENTER YOUR CHOICE 1
READY
CONTINUE[Y/N]
N
Result:
Thus the program is executed that using enumerated data type.
FUNCTION OVERLOADING
Aim:
To write c++ program that overload the functions.
Concept:
When a function is redefined with different set of arguments, then it is known as
overloaded function. This process is known as function overloading.
Algorithm:
Step 1: start the program.
Step 2: create functions ‘add’ with different number of arguments, data types.
Step 3: invoke the overloading function ‘add’ by passing different data types.
Step 4: print the values.
Step 5: stop.
Programs:
/*function overloading*/
#include<iostream.h>
#include<conio.h>
int add(int,int);
void main()
{
clrscr();
int add(int,int);
float add(float,float);
int add(int,int,int);
f loat add(int,float,float);
int i,j,k,l;
float w,x,y,z;
i=10;j=20;k=30;w=1.1;x=2.2;y=3.3;
l=add(i,j);
cout<<"\nl="<<l;
l=add(i,j,k);
cout<<"\nl="<<l;
z=add(w,x);
cout<<"\nz="<<z;
z=add(i,w,x);
cout<<"\nz="<<z;
getch();
}
int add(int a,int b)
{
return a+b;
}
float add(float xx,float yy)
{
return xx+yy;
}
int add(int a,int b,int c)
{
return a+b+c;
}
float add(int a,float xx,float yy)
{
return a+xx+yy;
}
Output:
l=30
l=60
z=3.3
z=13.3
Result:
Thus the program is executed that overload the function and performing different tasks.
SCOPE
Aim:
To write a c++ program to illustrate the scope of the variables.
Concept:
Local scope: the variable is only available in the code block in which it was defined. It is
no longer visible once you left out the code block.
Global scope: the variable can be used within a single module. Only the functions within
this module can use the variable. Other modules cannot access it directly.
Algorithm:
Step 1:
start the program.
Step 2:
Initializes the value for a and b.
Step 3:
Display the global scope.
Step 4:
Display the local scope.
Step 5:
stop the program.
Source code:
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr ();
cout<<"I am a global scope:"<<endl;
int a=20;
cout<<"a value is :"<<a<<endl;
{
cout<<"I am a local scope :"<< endl;
int b=3;
cout<<"b value is :"<< b<<endl;
}
cout<<++a<<endl;
getch ();
}
Output:
I am a global scope:
a value is: 20
I am a local scope:
b value is: 3
21
Result:
Thus the program is executed to illustrate the scope of the variable.
STORAGE CLASS
Aim:
To write a c++ program for using storage class.
Concept:
The storage class specifies the life time of the variable, that is period of time from the
construction of the variable until its destruction.
Algorithm:
Step 1: start the program.
Step 2: create a class ‘myclass’ and declare static variable ‘i’.
Step 3: create the objects ‘myobject1, myobject2’ for the class ‘myclass’.
Step 4: print the static values.
Step 5: stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class myclass
{
public:
static int i;
};
int myclass::i=0;
myclass myobject1;
myclass myobject2;
int main()
{
clrscr();
cout<<”storage class”<<endl;
cout<<myobject1.i<<endl;
cout<<myobject2.i<<endl;
myobject1.i=1;
cout<<myobject1.i<<endl;
cout<<myobject2.i<<endl;
myobject2.i=2;
cout<<myobject1.i<<endl;
cout<<myobject2.i<<endl;
myclass::i=3;
cout<<myobject1.i<<endl;
cout<<myobject2.i<<endl;
getch();
return(0);
}
OUTPUT:
storage class
0
0
1
1
2
2
3
3
RESULT:
The program is created for using storage class.
STACK USING ARRAY
Aim:
To write a c++ program for using stack.
Concept:
Stack is an ordered of collection of elements in which insertions and deletions are
performed at one end that is called top of the stack.
Algorithm:
Step 1: start the program.
Step 2: in the switch case call the functions ‘push, pop, display’.
Step 3: define the function ‘push’ to insert the values at top of the stack.
Step 4: define the function ‘pop’ to delete the values at top of the stack.
Step 5: define the function ‘display’ the values of the stack.
Step 6: stop the program.
Program:
#include<iostream.h>
#include<conio.h>
#include<process.h>
int stack[20],ch,n,top=0;
int push ();
int pop ();
int display ();
void main ()
{
clrscr ();
cout<<"enter the number of elements\n";
cin>>n;
while (ch! =4)
{
cout<<"\n1.push\n2.pop\n3.display\n4.exit\n";
cout<<"\nenter your choice";
cin>>ch;
switch (ch)
{
case 1:
push ();
break;
case 2:
pop ();
break;
case 3:
display ();
break;
case 4:
exit (0);
}
cout<<"\nenter your choice b/w 1 and 4";
}
}
push ()
{
int item;
if (top>=n)
{
cout<<"stack is full\n";
return (0);
}
else
{
cout<<"enter the item:";
cin>>item;
stack[top]=item;
top=top+1;
cout<<"\nThe item is stored into stack\n";
}
return (0);
}
pop ()
{
int item;
if (top==0)
{
cout<<"stack is empty\n";
return (0);
}
else
{
item=stack[top-1];
cout<<"\npoped out element"<<item;
top=top-1;
}
return (0);
}
display ()
{
int i;
if (top==0)
{
cout<<"stack is empty";
return (0);
}
else
{
for (i=top-1; i>=0; i--)
cout<<stack[i]<<"\n";
}
return (0);
}
Output:
enter the number of elements: 4
1. push
2. Pop
3. Display
4. Exit
Enter your choice 1
Enter the item: 12
The item is stored into stack
Enter your choice b/w 1 and 4
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 1
Enter the item: 14
The item is stored into stack
Enter your choice b/w 1 and 4
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 1
Enter the item: 15
The item is stored into stack
Enter your choice b/w 1 and 4
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 1
Stack is full
Enter your choice b/w 1 and 4
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 3
15 14 13 12
Enter your choice b/w 1 and 4
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 2
Poped out element 14
Enter your choice b/w 1 and 4
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 2
Poped out element 13
Enter your choice b/w 1 and 4
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 2
Poped out element 12
Enter your choice b/w 1 and 4
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 2
Stack is empty
Enter your choice b/w 1 and 4
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 3
Stack is empty
Enter your choice b/w 1 and 4
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 4
Result:
Thus the program is executed to store and retrieve the elements using stack.
QUEUE USING ARRAY
Aim:
To write a c++ program for using queue.
Concept:
Queue is an ordered collection of items where data are inserted at the rear and deleted
at the front.
Algorithm:
Step 1: start the program.
Step 2: in the switch case call the functions ‘enqueue, dequeue, display’.
Step 3: define the function ‘enqueue’ to insert the values at top of the queue.
Step 4: define the function ‘dequeue’ to delete the values at top of the
Step 5: define the function ‘display’ the values of the queue.
Step 6: stop the program.
queue.
program:
#include<iostream.h>
#include<conio.h>
int q[20],i,n,ch,front=0,rear=0;
int enqueue ();
int dequeue ();
int display ();
void main ()
{
clrscr ();
cout<<"\n\n\nEnter The Size of QUEUE\t";
cin>>n;
do
{
cout<<"\n\t\t1. ENQUEUE \n";
cout<<"\n\t\t2. DEQUEUES \n";
cout<<"\n\t\t3. DISPLAY \n";
cout<<"\n\t\t4. EXIT \n";
cout<<"\n\t\t ENTER YOUR CHOICE\t";
cin>>ch;
switch (ch)
{
case 1:
enqueue ();
break;
case 2:
dequeue ();
break;
case 3:
display ();
break;
}
} while (ch<=3);
}
int enqueue ()
{
int item;
if (rear>=n)
{
cout<<"\n\t\tQUEUE IS FULL\t";
return (1);
}
else
{
cout<<"\n\t\tENTER THE ELEMENT\t";
cin>>item;
q[rear]=item;
rear=rear+1;
}
return (0);
}
int dequeue ()
{
int item;
if (front==rear)
{
cout<<"\n\t\tQUEUE IS EMPTY";
return (0);
}
else
{
item=q[front];
cout<<"\n\t\tPOP UP ELEMENT"<<item;
front=front+1;
}
return (0);
}
int display ()
{
if(front==rear)
{
cout<<"\n\t\tQUEUE IS EMPTY";
return (0);
}
else
{
cout<<"\n\t\tTHE ELEMENT(s)....";
for (i=front;i<rear;i++)
cout<<"\t"<<q[i];
}
return (0);
}
OUTPUT:
Enter The Size Of QUEUE
6
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE
1
ENTER THE ELEMENT
5
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE
1
ENTER THE ELEMENT
6
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE
1
ENTER THE ELEMENT
7
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE
1
ENTER THE ELEMENT
8
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE
1
ENTER THE ELEMENT
9
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE
1
ENTER THE ELEMENT
10
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE
THE ELEMENT(s)....
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
3
5
6
7
8
9
10
4. EXIT
ENTER YOUR CHOICE
POP UP ELEMENT
2
5
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE
2
POP UP ELEMENT 6
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE
POP UP ELEMENT
2
7
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
ENTER YOUR CHOICE
THE ELEMENT(s)....
3
8
9
10
RESULT:
This program is created using queue.
CONSTRUTOR
Aim:
To write a program for initializing the values for the object.
Concept:
A constructor is special member function whose task is to initialize the objects of its
class.
Algorithm:
Step 1: start the program
Step 2: Declare a class ‘integer’.
Step 3: create the parameterized constructor same as class name.
Step 4: Create object ‘o1’ for passing values to ‘o1’ implicitly.
Step 5: Create object ‘o2’ for passing values ’o1’ explicitly.
Step 6: Stop the program.
Program:
//CONSTRUCTOR
PROGRAM//
#include<iostream.h>
#include<conio.h>
class integer
{
int m, n;
public:
integer (int, int);
void display (void)
{
cout<<" M="<<m<<endl;
cout<<" N="<<n<<endl;
}
};
integer::integer (int x, int y)
{
m=x;
n=y;
}
void main ()
{
clrscr ();
integer o1 (0.100);
integer o2=integer (25, 75);
cout<<”
IMPLICITLY CALL “<<endl;
o1.display();
cout<<”
EXPLICITLY CALL “<<endl;
o2.display();
}
Output:
IMPLICITLY CALL
M=0
N=100
EXPLICITLY CALL
M=25
N=75
Result:
Thus the program is created to initialize the values of object using
constructor.
DESTRUCTOR
Aim:
To write a c++ program to destroy the values of the object.
Concept:
A destructor destroys an object of its class type. Destructors are special functions, used
to execute automatically when the object of a class goes out of scope.
Algorithm:
Step 1: Start the program.
Step 2: Declare the variable c as global.
Step 3: Create the class ‘num’.
Step 4: Create constructor and use the variable c to indicate how constructor works.
Step 5: Create destructor and use the variable c to indicate how destructor works.
Step 6: Create the objects ‘a, b’ for class num in the main block.
Step 7: Create the object ‘c’ for class num in the block.
Step 8: Stop the program.
Program:
#include<iostream.h>
#include<conio.h>
int c=0;
class num
{
public:
num()
{
c++;
cout<<"\nobject created\n"<<c;
}
~num()
{
cout<<"\n object destroyed\n"<<c;
c--;
}
};
void main()
{
clrscr();
cout<<"~~~~~~~~~~ DESTRUCTOR ~~~~~~~~~~~~";
cout<<"\n IN MAIN \n" ;
num a,b;
cout<<" IN BLOCK A\n";
{
num c;
}
cout<<"\n Again in main\n";
}
OUTPUT:
~~~~~~~~~~ DESTRUCTOR ~~~~~~~~~~~~
IN MAIN
object created
1
object created
2 IN BLOCK A
object created
3
object destroyed
3
Again in main
object destroyed
2
object destroyed
1
Result:
Thus the program is created for using destructor.
CONSTRUCTOR OVERLOADING
Aim:
To write a c++ program that overloads the constructor of different types.
Concept:
It is possible to have multiple class constructors for a class. Since the constructor
function takes arguments, it is possible to overload it
Algorithm:
Step 1: start the program.
Step 2: create the class ‘time’.
Step 3: create default constructor with default values.
Step 4: create single argument constructor and pass the one value.
Step 5: create parameterized constructor and pass more than one values.
Step 6: create the copy constructor and pass the reference object.
Step 7: create the function as show and display the hour, minutes
Step 8: create the objects and invoke the constructor objects.
Step 9: stop the program.
and seconds.
Program:
#include<iostream.h>
#include<conio.h>
class time
{
private:
int hrs,mins,sec;
public:
time()
{
hrs=0;
mins=0;
sec=0;
}
time(int h)
{
hrs=h;
mins=0;
sec=0;
}
time(int h,int m)
{
hrs=h;
mins=m;
sec=0;
}
time(int h,int m,int s)
{
hrs=h;
mins=m;
sec=s;
}
void show()
{
cout<<"HOUR:MINUTE:SECONDS:"
<<hrs<<":"<<mins<<":"<<sec<<endl;
}
};
void main()
{
clrscr();
cout<<" ****CONSTRUCTOR OVERLODING***"<<endl;
cout<<endl;
time t1,t2(5),t3(6,10),t4(6,7,10),t5(t1);
t1.show();
t2.show();
t3.show();
t4.show();
getch();
}
Output:
***************CONSTRUCTOR OVERLODING*************
HOUR:MINUTE:SECONDS:
0:0:0
HOUR:MINUTE:SECONDS:
5:0:0
HOUR:MINUTE:SECONDS:
6:10:0
HOUR:MINUTE:SECONDS:
6:7:10
Result:
Thus the program is created using constructor overloading.
STATIC DATA MEMBER
Aim:
To write a c++ program that using the static data member.
Concept:
Static variables are normally used to maintain values common to the entire class. They
are initialized only once. They retain their values between calls.
Algorithm:
Step 1: Start the process.
Step 2: Create the class ‘book’.
Step 3: Declare the static variable ‘count’.
Step 4: Declare the variable ‘bookid’.
Step 5: define the function ‘addbook’ to that increment the static variable and assign the
values to variable.
Step 6: define the function ‘displaybook’ and display the count value
Step 7: Create the object b1, b2 and access the functions;
Step 8: End the process
Program:
#include<iostream.h>
#include<conio.h>
class book
{
private:
static int count;
int bookid;
public:
addbook (void)
{
count ++;
bookid=count;
}
displaybook()
{
cout<<"The Number Of the Books Are:"<<count;
}
};
int book::count;
void main()
{
book b1,b2;
clrscr();
b1.addbook();
b2.addbook();
b1.displaybook();
cout<<endl;
b2.displaybook();
getch();
}
Output:
The Number Of the Books Are:2
The Number Of the Books Are:2
Result:
Thus the program is executed that using the static data members.
STATIC MEMBER FUNCTION
Aim:
To write a c++ program to create the static member function.
Concept:
A static function can have access to only other static member declared in the same
class.
Algorithm:
Step 1: start the program.
Step 2: create the class ‘test’ and declare the static variable ‘count’.
Step 3: define the member function ‘setcode’ that use normal variable.
Step 4: define the function ‘setcode’ to display the variable ‘code’.
Step 5: define the static member function ‘showcount’ to display the static variable.
Step 6: create the object ‘t1, t2’ to access the variable and functions.
Step 7: stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class test
{
int code;
static int count;
public:
void setcode(void)
{
code=++count;
}
void showcode(void)
{
cout<<"object number"<<code<<endl;
}
static void showcount(void)
{
cout<<"count"<<count<<endl;
}
};
int test::count;
int main()
{
clrscr();
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
return(1);
}
Output:
count2
count3
object number1
object number2
object number3
Result:
Thus the program is created using the static member function.
BITFIELDS
Aim:
To write a c++ program for the usage of bitfields.
Concept:
Structure can contain members that occupy less storage than an integral type. These
numbers are specified as bit fields.
Algorithm;
Step 1: Start the program.
Step 2: Create a class “Mytime”.
Step 3: Create a data members hour secs,mins with bitfields.
Step 4: define the function ‘sethour’ and get values of the hrs.
Step 5: define the function ‘sethour’ and get values of the mins.
Step 6: define the function ‘sethour’ and get values of the secs.
Step 7: define the function ‘display’ to display the values.
Step 8: Stop the program.
Programs:
#include<iostream.h>
#include<conio.h>
#include<assert.h>
class mytime
{
unsigned hour: 5;
unsigned mins: 6;
unsigned secs: 6;
public:
void sethour(int ahour)
{
assert(ahour<24);
hour=ahour;
}
void setmins(int amins)
{
assert(amins<60);
mins=amins;
}
void setsecs(int asecs)
{
assert(asecs<60);
secs=asecs;
}
void print()
{
cout<<"OUTPUT;\n"<<endl;
cout<<"_______\n";
cout<<"\nDEMONSTRATE THE USAGE OF BITFIELDS\n";
cout<<"----------------------------------------------------------------\n";
cout<<"\n\nHOURS : MINS : SECS\n\n";
cout<<"
----------- -------- -------\n";
cout<<hour<<" :\t"<<mins<<" :\t"<<secs<<endl;
}
};
int main()
{
clrscr();
mytime t;
t.sethour(12);
t.setmins(58);
t.setsecs(23);
t.print();
cout<<"\n\n";
cout<<"SIZE OF MYTIME=\t"<<sizeof(t)<<endl;
cout<<"------------------------";
getch();
}
Output:
DEMONSTRATE THE USAGE OF BITFIELDS
HOURS: MINS: SECS
12
: 58
: 23
SIZE OF MYTIME= 3
Result:
Thus the program is successfully executed that implements the usage of bitfields.
BINARY OPERATOR OVERLOADNG USING MEMBER FUNCTION
Aim:
To write a c++ program that overload the binary operator using member function.
Concept:
The binary operator are overloaded using member function that takes only one
reference object as argument explicitly.
Algorithm:
Step1: start the program.
Step2: create a class complex with data members and functions.
Step3: define the member function getvalue () that get the value.
Step4: define the operator function operator + (complex ob)that add the real and
Imaginary numbers.
Step5: define the operator function operator - (complex ob) that subtracts the real and
imaginary numbers.
Step6: define the member function display () that prints the values
Step7: invoke operator function + and - through obj1 and obj2 of the class complex
obj1+obj2 and obj1-obj2.
Step8: stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void getvalue()
{
cout<<"\nEnter the value of complex number a,b:\n";
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;
cout<<"\nEnter the values an for 1st complex number\n";
obj1.getvalue();
cout<<"\nEnter the value for 2nd complex number\n";
obj2.getvalue();
result=obj1+obj2;
cout<<"\nThe addition values of two complex number is\n";
result.display();
result=obj1-obj2;
cout<<"\nThe subtraction values of two complex number is\n";
result.display();
getch();
}
Output:
Enter the values an for 1st complex number
Enter the value of complex number a,b:
25
35
Enter the value for 2nd complex number
Enter the value of complex number a,b:
50
20
The addition values of two complex number is
75+55i
The subtraction values of two complex number is
-25+15i
Result:
Thus the program is executed that binary operator using friend function.
BINARY OPERATOR OVERLOADNG USING FRIEND FUNCTION
Aim:
To write a c++ program that overload the binary operator using member function.
Concept:
The binary operator can be overloade using the friend function that takes two reference
objects as arguments explicitly.
Algorithm:
Step1: start the program.
Step2: create a class complex with data members and functions.
Step3: define the member function getvalue() that get the value.
Step4: define the operator function as friend operator + (complex ob1,complex ob2)that
add the real and imag numbers.
Step5: define the operator function as friend operator - (complex ob1,object ob2) that
subtract the real and imag numbers.
Step6: define the member function display() that prints the values
Step7: invoke operator function + and - throught obj1 and obj2 of the class complex
obj1+obj2 and obj1-obj2.
Step8: stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void getvalue()
{
cout<<"\nEnter the value of complex number a,b:\n";
cin>>a>>b;
}
friend complex operator+(complex ob1,complex ob2)
{
complex t;
t.a=ob1a+ob2.a;
t.b=ob1.b+ob2.b;
return(t);
}
friend complex operator-(complex ob1,complex ob2)
{
complex t;
t.a=ob1.a-ob2.a;
t.b=ob1.b-ob2.b;
return t;
}
void display()
{
cout<<a<<"+"<<b<<"i"<<"\n";
}
};
void main()
{
clrscr();
complex obj1,obj2,result;
cout<<"\nEnter the values an for 1st complex number\n";
obj1.getvalue();
cout<<"\nEnter the value for 2nd complex number\n";
obj2.getvalue();
result=obj1+obj2;
cout<<"\nThe addition values of two complex number is\n";
result.display();
result=obj1-obj2;
cout<<"\nThe subtraction values of two complex number is\n";
result.display();
getch();
}
Output:
Enter the values an for 1st complex number
Enter the value of complex number a,b:
25
35
Enter the value for 2nd complex number
Enter the value of complex number a,b:
50
20
The addition values of two complex number is
75+55i
The subtraction values of two complex number is
-25+15i
Result:
Thus the program is executed that binary operator using friend function.
UNARY OPERATOR OVERLOADING
IN
POSTFIX & PREFIX USING MEMBER FUNCTION
Aim:
To write a c++ program that overloads the increment and decrement operator using member
function.
Concept:
The unary operator can be overloaded using member function that takes no reference
object as argument.
Algorithm:
Step 1: start the program.
Step 2: create the class ‘unary’.
Step 3: create the operator function ‘++’with passing no reference object and to
increment the object in postfix.
Step 4: create the operator function ‘++’ with passing no reference object and to
increment the object in prefix.
Step 5: create the operator function ‘--’ with passing no reference object and to decrement
the object in postfix.
Step 6: create the operator function ‘--’ with passing no reference object and to decrement
the object in prefix.
Step 7: define the function ‘print’ to display the values.
Step 8: create the object ‘u1’,’u2’ to perform increment and decrement.
Step 9: stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class unary
{
private:
int a,b;
public:
unary(int x,int y)
{
a=x;
b=y;
}
void operator ++()
{
a++;
b++;
}
void operator ++(int dummy)
{
a++;
b++;
}
void operator --()
{
a--;
b--;
}
void operator --(int dummy)
{
a--;
b--;
}
void print()
{
cout<<"\t*******************"<<endl;
cout<<"\tThe result is:"<<endl;
cout<<"\ta:"<<a<<endl;
cout<<"\tb:"<<b<<endl;
cout<<"\t*******************"<<endl;
}
};
void main()
{
clrscr();
unary u1(4,-3);
u1++;
u1.print();
unary u2(-5,8);
++u2;
u2.print();
unary u1(4,-3);
u1--;
u1.print();
unary u2(-5,8);
--u2;
u2.print();
getch();
}
OUTPUT:
*******************
The result is:
a:4
b:-3
*******************
*******************
The result is:
a:-4
b:9
*******************
*******************
The result is:
a:4
b:-3
*******************
*******************
The result is:
a:-5
b:8
*******************
Result:
Thus the program is executed that overloads the increment and decrement operator
using member function.
UNARY OPERATOR OVERLOADING
IN
POSTFIX & PREFIX USING FRIEND FUNCTION
Aim:
To write a c++ program that overloads the increment and decrement operator using friend
function.
Concept:
The unary operator can be overloaded using member function that takes no reference
object as argument.
Algorithm:
Step 1: start the program.
Step 2: create the class ‘unary’.
Step 3: create the operator function as friend ‘++’ with passing one reference object and
to increment the object in postfix.
Step 4: create the operator function as friend ‘++’ with passing one reference object and
to increment the object in prefix.
Step 5: create the operator function as friend ‘--’ with passing one reference object and to
decrement the object in postfix.
Step 6: create the operator function as friend ‘--’ with passing one reference object and to
decrement the object in prefix.
Step 7: define the function ‘print’ to display the values.
Step 8: create the object ‘u1’,’u2’ to perform increment and decrement.
Step 9: stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class unary
{
private:
int a,b;
public:
unary(int x,int y)
{
a=x;
b=y;
}
friend void operator ++(unary u)
{
u.a++;
u.b++;
}
friend void operator ++(unary u,int dummy)
{
u.a++;
u.b++;
}
friend void operator --(unary u)
{
u.a--;
u.b--;
}
void operator --(unary u,int dummy)
{
u.a--;
u.b--;
}
void print()
{
cout<<"\t*******************"<<endl;
cout<<"\tThe result is:"<<endl;
cout<<"\ta:"<<a<<endl;
cout<<"\tb:"<<b<<endl;
cout<<"\t*******************"<<endl;
}
};
void main()
{
clrscr();
unary u1(4,-3);
u1++;
u1.print();
unary u2(-5,8);
++u2;
u2.print();
unary u1(4,-3);
u1--;
u1.print();
unary u2(-5,8);
--u2;
u2.print();
getch();
}
OUTPUT:
*******************
The result is:
a:4
b:-3
*******************
*******************
The result is:
a:-4
b:9
*******************
*******************
The result is:
a:4
b:-3
*******************
*******************
The result is:
a:-5
b:8
*******************
Result:
Thus the program is executed that overloads the increment and decrement operator
using member function
FUNCTION TEMPLATE
Aim:
To write a c++ program to implements the concept of function template to perform bubble
sort.
Concept:
The templates declared for functions are called function template. Function templates
are generic function, which for any data type that is passed to them.
Algorithm:
Step 1: start the program.
Step 2: create template’ T’
Step 3: define function ‘bubble’ to sort the values in ascending order.
Step 4: define the function swap to swap the values if values are greater than the next
element.
Step 6: sort the integer and floating values using the template function.
Step7: display the values
Step 8: stop the program.
Program:
#include<iostream.h>
#include<conio.h>
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()
{
clrscr();
int x[5]={10,50,30,40,20};
float y[5]={1.1,5.5,2.2,4.4,3.3};
bubble(x,5);
bubble(y,5);
cout<<"\t"<<"BUBBLE SORT"<<"\n"<<"\n";
cout<<"sorted x-array"<<"\n"<<"\n";
for(int i=0;i<5;i++)
cout<<"\t"<<x[i]<<"\n";
cout<<endl;
cout<<"sorted y-array"<<"\n"<<"\n";
for(int j=0;j<5;j++)
cout<<"\t"<<y[j]<<"\n";
cout<<endl;
getch();
return 0;
}
Output:
BUBBLE SORT
sorted x-array
10
20
30
40
50
sorted y-array
1.1
2.2
3.3
4.4
5.5
Result:
Thus the program is executed to implement the concept of function template.
CLASS TEMPLATE
Aim:
To write a c++ program for using class template.
Concept:
A class can also be declared to operate on different data types. Such classes are called
class templates.
Algorithm:
Step 1: Start the program.
Step 2: Create template with arguments class t1,class t2,
Step 3: Create class ‘ data’ .
Step 4: define function ‘show’ to display the values of different data types.
Step 5: Declare the array variables int i[],float f[] and assign the values.
Step 6: Create an object’ h’.
Step 7: print the values of the integer and floating variables by accessing the class
template.
Step 8: stop the program.
Program:
#include<iostream.h>
#include<conio.h>
template<class t1,class t2>
class data
{
public:
void show(t1 a,t2 b)
{
cout<<"a="<<a<<"\n";
cout<<"b="<<b<<"\n";
}
};
void main()
{
int i[]={3,5,6,7,8,9,10};
float f[]={2.5,4.5,6.3,6.2,6.4,7.4,6.8};
clrscr();
cout<<"\n\n\t\t\tCLASS TEMPLATE\t\t\n";
data<int,float>h;
for(int m=0;m<7;m++)
{
h.show(i[m],f[m]);
}
getch();
}
Output:
CLASS TEMPLATE
a=3
b=2.5
a=5
b=4.5
a=6
b=6.3
a=7
b=6.2
a=8
b=6.4
a=9
b=7.4
a=10
b=6.8
Result:
Thus the program is executed to create the template the class.
SINGLE INHERITANCE
Aim:
To develop a c++ program to display the student details using single inheritance.
Concept:
When a single class is derived from single base class. It is called single inheritance.
Algorithm:
Step 1: start the program.
Step 2: create a class student and declare a member of class.
Step3: Get the student name, rollno and marks.
Step 4: create a class result and inherit the class student.
Step 5: Calculate the total marks and result. Then display the student details.
Step 6: create a object as t and declare the variables.
Step 7: Read the student name, rollno and marks.
Step 8: Invoke the function as getdata (), calculate () and display ().
Step 9: Stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class student
{
public:
int m1,m2,m3,tot,rno;
char *name,*res;
public:
void getdata(char *n,int r,int k1,int k2,int k3)
{
name=n;
rno=r;
m1=k1;
m2=k2;
m3=k3;
}
};
class result:public student
{
public:
void calculate()
{
tot=m1+m2+m3;
if(m1>=35 &&m2>=35 &&m3>=35)
res="pass";
else
res="fail";
}
void display()
{
cout<<"*******************"<<endl;
cout<<"student name is:"<<name<<endl;
cout<<"Student roll no is:"<<rno<<endl;
cout<<"Student mark1:"<<m1<<endl;
cout<<"Student mark2:"<<m2<<endl;
cout<<"Student mark3:"<<m3<<endl;
cout<<"Total is:"<<tot<<endl;
cout<<"Result is:"<<res<<endl;
cout<<"********************"<<endl;
}
};
void main()
{
int v1,v2,v3,no;
char *n;
clrscr();
result t;
cout<<"\t\tSINGLE INHERITANCE\n\n";
cout<<"Enter the student name"<<endl;
cin>>n;
cout<<"Enter the student roll no:"<<endl;
cin>>no;
cout<<"Enter the student marks:"<<endl;
cin>>v1>>v2>>v3;
t.getdata(n,no,v1,v2,v3);
t.calculate();
t.display();
getch();
}
OUTPUT:
SINGLE INHERITANCE
Enter the student name
Divya
Enter the student roll no:
1142
Enter the student marks:
89
78
67
*******************
Student name is: Divya
Student roll no is: 1142
Student mark1:89
Student mark2:78
Student mark3:67
Total is:234
Result is: pass
Result:
Thus the program is executed to implement the concept of single inheritance.
MULTIPLE INHERITANCES
Aim:
To develop a c++ program to implements the multiple inheritance.
Concept:
Multiple inheritances refer to derivation of a single class from several base classes.
Algorithm:
Step 1: Start the program.
Step 2: Create the two classes M and N.
Step 3: Create derived class P which are derived from class M and N.
Step 4: create an object p for the class P.
Step 5: get the values for data members and display the values.
Step 6: Stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class M
{
protected:
int m;
public:
void get_m(int);
};
class N
{
protected:
int n;
public:
void get_n(int);
};
class P:public M,public N
{
public:
void display(void);
};
void M::get_m(int x)
{
m=x;
}
void N::get_n(int y)
{
n=y;
}
void P::display(void)
{
cout<<"the value of m is="<<m<<"\n";
cout<<"the value of n is="<<n<<"\n";
cout<<"total value of m+n is="<<m+n<<"\n";
}
int main()
{
clrscr();
cout<<"*****MULTIPLE INHERITANCE*****"<<endl;
P p;
p.get_m(10);
p.get_n(20);
p.display ();
getch();
return 0;
}
Output:
*****MULTIPLE INHERITANCE*****
the value of m is=10
the value of n is=20
total value of m+n is=30
Result:
Thus the program is executed to implement the concept of multiple inheritances.
MULTILEVEL INHERITANCE
Aim:
To write a c++ program to implements the concept of multilevel inheritance.
Concept:
Derivation of a class from another derived class is called multilevel inheritance.
Algorithm:
Step 1: start the program.
Step 2: create a class ‘library’ with data members and functions.
Step 3: create a derived class ‘days’ that inherit the properties from the base class ‘library’.
Step 4: create a derived class ‘amount’ that inherit the properties from the derived class.
Step 5: create a object for derived class ‘amount’
Step 6: stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class library
{
public:
int code;
char *bname,*author;
public:
void getdata(char *s,char *a,int n)
{
bname=s;
author=a;
code=n;
}
};
class days: public library
{
public:
int day;
public:
void getdays(int k)
{
day=k;
}
};
class amount: public days
{
int amt;
public:
void calculate()
{
if(day<=15)
amt=0;
else if(day>15 && day<=30)
amt=10;
else if(day>30 && day<=45)
amt=20;
else if(day>45)
amt=30;
}
void display()
{
cout<<"---------------------\n";
cout<<"Book name:"<<bname<<endl;
cout<<"Author name:"<<author<<endl;
cout<<"Book code:"<<code<<endl;
cout<<"Fine amount is:"<<amt<<endl;
cout<<"----------------------\n";
}
};
void main()
{
char *b1,*b2;
int c1,d1;
clrscr();
amount t;
cout<<"\t\MULTILEVELINTHERITANCE\n\n";
cout<<"Enter the book name:";
cin>>b1;
cout<<"Enter the author name:";
cin>>b2;
cout<<"Enter the book code:";
cin>>c1;
cout<<"Enter the number of days:";
cin>>d1;
t.getdata(b1,b2,c1);
t.getdays(d1);
t.calculate();
t.display();
getch();
}
Output:
MULTILEVEL INTHERITANCE
Enter the book name:cpp
Enter the book code:222
Enter the number of days:20
--------------------Book name:cpp
Book code:222
Fine amount is:10
Result:
Thus the program is executed to implement the concept of multilevel inheritance.
HIERARCHICAL INHERTIANCE
Aim:
To write a c++ program for student result using hierarchical inheritance.
Concept:
It is a derivation of several classes from a single base class. This is called hierarchical
inheritance.
Algorithm:
Step 1: start the program.
Step 2: Create the class as student.
Step 3: Create the function ‘get-data ()’ and pass the more than one argument.
Step 4: Create the function as ‘put-data ()’ and in function display the roll no and marks.
Step 5: Create the class as total and inherit the class student.
Step 6: Create the function as cal () and calculate the total.
Step 7: Create the class as result and inherit the class student.
Step 8: Create the function as display () and calculate the result using if condition.
Step 9: Create object and call the function using object.
Step 10: Stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class student
{
public:
int roll_no,m1,m2,m3;
char *name;
public:
void getdata()
{
cout<<"enter student name:";
cin>>name;
cout<<"enter student rollno:";
cin>>roll_no;
cout<<"enter marks\n MARK1=";
cin>>m1;
cout<<" MARK2=";
cin>>m2;
cout<<" MARK3=";
cin>>m3;
}
void putdata()
{
cout<<"\n\n******STUDEN T RESULTS******\n\n";
cout<<"NAME : "<<name<<"\n\nROLLNO:”;
cout<<roll_no<<"\n\nMARK1 ="<<m1<<"\n\nMARK2=”;
cout<<m2<<"\n\nMARK3 ="<<m3<<"\n\n";
}
};
class total:public student
{
public:
void cal()
{
int tot=m1+m2+m3;
float avg=(m1+m2+m3)/3;
cout<<"TOTAL MARKS="<<tot<<"\n\nAVERAGE="<<avg<<"\n\n";
}
};
class result:public student
{
public:
void display()
{
if((m1>50)||(m2>50)||(m3>50))
cout<<"RESULT=PASS"<<endl;
else
cout<<"RESULT=FAIL"<<endl;
}
};
void main()
{
total t;
result r;
clrscr();
cout<<"\n******HIERARCHICAL INHERITANCE**************\n\n\n";
t.getdata();
t.putdata();
t.cal();
r.display();
getch();
}
OUTPUT:
**************HIERARCHICAL INHERITANCE**************
enter student name:kumar
enter student rollno:133
enter marks
MARK1=89
MARK2=98
MARK3=78
******STUDENT RESULTS******
NAME : kumar
ROLLNO: 133
MARK1 =89
MARK2 =98
MARK3 =78
TOTAL MARKS=265
AVERAGE=88
RESULT=PASS
Result:
Thus the program is created to implement the concept of hierarchial inheritance.
MULTIPATH INHERITANCE:
Aim:
To develop the c++ program to implement the concept of multipath inheritance.
Concept:
Derivation of a class from another derived classes, which are derived from the same
base classes, is called multipath inheritance.
Algorithm:
Step 1: Start the program..
Step 2: Declare the class ‘person’ and declare the member variables and functions.
Step 3: inherit the properties of class ‘person’ to account and admin to avoid ambiguity
virtual is used.
Step 4: inherit the properties of class ‘account and admin’ to class ‘master’.
Step 5: create the object ‘m’ master that access the properties of
classes’person,account,admin’.
Step 6: stop the program
Program:
#include<iostream.h>
#include<conio.h>
#include<process.h>
class person
{
protected:
char *name;
int code;
public:
void getdata()
{
cout<<"Get the Name & Code"<<endl;
cout<<"\n enter the name & code";
cin>>name>>code;
}
};
class account:virtual public person
{
protected:
int pay;
public:
void getpay()
{
cout<<"Get the Payment"<<endl;
cout<<"\n enter the payment";
cin>>pay;
}
};
class admin:virtual public person
{
protected:
int exp;
public:
void getexp()
{
cout<<"Get the Experience"<<endl;
cout<<"\n enter the experience";
cin>>exp;
}
};
class master:public account,public admin
{
public:
void getvalue()
{
getdata();
getpay();
getexp();
}
void update()
{
int c;
cout<<"\n Do you want to update\n 1.code & name\n 2.payment\n
3.experience"<<endl;
cout<<"\n enter your choice";
cin>>c;
switch(c)
{
case 1:
getdata();
break;
case 2:
getpay();
break;
case 3:
getexp();
break;
default:
cout<<"\n invalid choice";
}
}
void putdata()
{
clrscr();
cout<<"\n details";
cout<<"\n Name:"<<name<<endl;
cout<<"\n Code:"<<code<<endl;
cout<<"\n Payment:"<<pay<<endl;
cout<<"\n Experience:"<<exp<<endl;
cout<<"\n press any key 2 continue";
getch();
}
};
void main()
{
clrscr();
int ch;
master m;
cout<<"\t\t\t\tMULTIPATH INHERITANCE\t\t\t\t"<<endl;
cout<<"\t\t\t\t*********************\t\t\t\t"<<endl;
while(1)
{
cout<<"\n MENU \n1.create\n2.update\n3.display\n4.exit";
cout<<"\n enter your choice";
cin>>ch;
switch(ch)
{
case 1:
m.getvalue();
break;
case 2:
m.update();
break;
case 3:
m.putdata();
break;
case 4:
exit(0);
default:
cout<<"\n invalid choice";
}
}
getch();
}
Output:
MULTIPATH INHERITANCE
*********************
MENU
1.create
2.update
3.display
4.exit
enter your choice 1
Get the Name & Code Value
_______________________
Enter the Name & Code
Rajeswari
101
Get the Payment Value
_____________________
Enter the payment
10000
Get the Experience Value
________________________
Enter the experience
5
MENU
1.create
2.update
3.display
4.exit
enter your choice 2
Do you want to update
1.code & name
2.payment
3.experience
Enter your choice
2
Get the Payment Value
_____________________
Enter the payment
15000
MENU
1.create
2.update
3.display
4.exit
enter your choice 3
details
Name:Rajeswari
Code:101
Payment:15000
Experience:5
press any key 2 continue
MENU
1.create
2.update
3.display
4.exit
enter your choice 4
Result:
Thus the program is executed to implement the concept of multipath inheritance.
VIRTUAL FUNCTION
Aim:
To write a c++ program implements the concept of virtual functions.
Concept:
The virtual function support dynamic binding. The function overriding is achieved by
virtual function. The virtual keyword is used to declare a virtual function in a base class. A
virtual function need not to be redefined in the derived class.
Algorithm:
Step1: start the program.
Step 2: declare the base class ‘shape’
Step 3: define the virtual function ‘draw’ in the base class.
Step 4: declare the derived class ‘circle’ that is derived from ‘shape’.
Step 5: define the function ‘draw’ that is same as virtual function.
Step 6: create the pointer object ‘*ptr’ and object‘s’ for base class
Step 7: create the object ‘c ‘for class ‘circle’;
Step 8: access the function ‘draw’ through pointer object ‘ptr’ by assigning the address of
the objects.
Step 9: stop the program.
Program:
#include<iostream.h>
#include<conio.h>
class shape
{
public:
virtual void draw()
{
cout<<"shape is drawn\n";
}
};
class circle:public shape
{
public:
void draw()
{
cout<<"circle is drawn\n";
}
};
void main()
{
shape *ptrs,s;
circle c;
clrscr();
cout<<"
VIRTUAL FUNCTION
cout<<"
----------------
\n";
\n";
ptrs=&s;
cout<<"the base class function is executed\n";
ptrs->draw();
ptrs=&c;
cout<<"the derived class function is executed\n";
ptrs->draw();
getch();
}
Output:
VIRTUAL FUNCTION
---------------the base class function is executed
shape is drawn
the derived class function is executed
circle is drawn
Result:
The c++ program was executed that implements the concept of virtual function.
ITERATOR USING VECTOR
#include<iostream.h>
#include<conio.h>
#include<vector.h>
#include<iterator.h>
void main()
{
Vector <int> v1;
int sum=0;
clrscr();
v1.pushback(10);
v1.pushback(20);
v1.pushback(42);
v1.pushback(30);
vector <int> :: iterator first;
cout<<”element in the vector contains\n”;
for(first=v1.begin();first!=v1.end();first++)
{
cout<<*first<<”\t”;
sum+=*first; first++;
}
cout<<”the sum is :”<<sum;
getch();
}
OUTPUT:
Element in the vector contains :
10 20 42 30
the sum is :102
ITERATOR USING LIST
#include<iostream.h>
#include<conio.h>
#include<iterator.h>
#include<list.h>
void main()
{
list <int> l1;
sum=0;
clrscr();
l1.pushback(20);
l1.pushback(10);
l1.pushback(30);
l1.pushback(40);
list <int> :: iterator index = l1.begin();
cout<<”element in the list contains :\n”;
while(index != l1.end())
{
cout<<*index<<”\t”;
sum+=*index;
}
cout<<”sum is =”<<sum
getch();
}
OUTPUT:
Element in the list contains :
20 10 30 40
the sum is :100
ITERATOR USING MAP
#include<iostream.h>
#include<conio.h>
#include<map.h>
#include<iterator.h>
void main()
{
clrscr();
map <char,int> m1;
m1[ ‘a’ ]=1
m1[ ‘e’ ]=2;
m1[ ‘b’ ]=3;
m1[ ‘f’ ]=4;
map <char,int> :: iterator index;
cout<<”element in the map contains :\n”;
for(index=m1.begin();index!=m1.end();index++)
{
cout<<index->first<<”\t”;
cout<<index->second<<endl;
}
getch();
}
OUTPUT:
Element in the map contains :
a 1
b
3
e 2
f
4
ITERATOR USING MULTI MAP
#include<iostream.h>
#include<conio.h>
#include<map.h>
#include<iterator.h>
Void main()
{
Clrscr();
Multimap <char,int> m1;
m1.insert(pair <char,int> ( ‘a’ ,1));
m1.insert(pair <char,int> ( ‘d’ ,2));
m1.insert(pair <char,int> ( ‘c’ ,3));
m1.insert(pair <char,int> ( ‘b’ ,4));
multimap <char,int> :: iterator index;
cout<<”element in the map contains :\n”;
for(index=m1.begin();index!=m1.end();index++)
{
cout<<index->first<<”\t”;
cout<<index->second<<endl;
}
getch();
}
OUTPUT:
Element in the multimap contains :
a 1
b
4
c
3
d
2