[go: up one dir, main page]

0% found this document useful (0 votes)
85 views78 pages

VP Attachment

The document is a lab manual for an Object Oriented Programming course. It contains a list of 25 experiments to be completed in C++ and Java programming languages. The experiments cover topics like function overloading, constructors, destructors, operator overloading, inheritance, polymorphism, exceptions, templates and more. For each experiment, a sample code is provided along with the aim, algorithm and sample output.

Uploaded by

Thalapathy Raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views78 pages

VP Attachment

The document is a lab manual for an Object Oriented Programming course. It contains a list of 25 experiments to be completed in C++ and Java programming languages. The experiments cover topics like function overloading, constructors, destructors, operator overloading, inheritance, polymorphism, exceptions, templates and more. For each experiment, a sample code is provided along with the aim, algorithm and sample output.

Uploaded by

Thalapathy Raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

WWW.VIDYARTHIPLUS.

COM
WWW.VIDYARTHIPLUS.COM MAGNA


MAGNA COLLEGE OF ENGINEERING
(Affiliated to Anna University)
JDN Educational Trust
RED HILLS-THIRUVALLUR HIGH ROAD, MAGARAL, CHENNAI-55

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERIN

SUBJECT CODE :CS2312
SUBJECT NAME :OBJECT ORIENTED PROGRAMMING
LABORATORY
DEPARTMENT : ELECTRICAL AND ELECTRONICS
ENGINEERING
SEMESTER : V

Prepared By,
Ms.N.Anbarasi
Lecturer,
Department of Computer Science and Engineering,
Magna College of Engineering,
Chennai.
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA



LIST OF EXPERIMENTS FOR OBJECT ORIENTED
PROGRAMMING LAB CS2312


Sl.No. Name of the Experiment Page No.
C++ PROGRAM
1 Function overloading
2 User defined function with default arguments
3 Program using simple class object creation and invoking class
4 program using constructor and destructor
5 Dynamic initialization of constructor
6
copy constructor

7 Using Friend Function accessing private data member
8 Display Players detail Using Friend Function
9
Complex operation using operator overloading

10
complex operation using operator overloading and type conversion

11 Multilevel inheritance
12 Hybrid inheritance
13 Runtime polymorphism
14 Function template
15
Exception handling

16 Standard template library


WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA



LIST OF EXPERIMENTS FOR OBJECT ORIENTED
PROGRAMMING LAB CS2312

Sl.No. Name of the Experiment Page No.
JAVA PROGRAM
17 Simple java program.
18 Finding area of a rectangle.
19 Creating package and importing package.
20 Finding area of rectangle and circle using interface.
21 Students details using multiple inheritance through interface.
22
Exception handling.

23 Reading string from console using buffered reader class.
24
Program using print writer class to handle console output.

25
Multithreading .










WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:1 Function overloading
DATE:

AIM:
To write a c++ program to calculate volume of cube,cylinder,and rectangle using
function overloading.
ALGORITHM:
1. Start the program.
2. Declare the prototypes for volume function to find volume of
cube,cylinder,rectangle.
3. Get the input values such as side, length, breadth, height, and radius.
4. Invoke volume function of cylinder by passing radius and height to find
volume of cylinder.
5. Invoke volume function of cube by passing side of cube to find volume of
cube.
6. Invoke volume function of Rectangle by passing length, breadth and height to
find volume of rectangle.
7. Print the volume of cube, cylinder and rectangle
8. Stop the program.
Program
/* Function overloading*/
#include<iostream.h>
#include<conio.h>
int volume(int);
double volume(double,int);
long volume(long,int,int);
int main()
{
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

int n,r,b,h1;
double h;
long l;
clrscr();
cout<<"enter the side of cube\n";
cout<<"volume of cube"<<"\n";
cin >>n;
cout<<volume(n);
cout<<"enter the radius and height of cylinder\n";
cout<<"volume of cylinder"<<"\n";
cin >>r>>h;
cout<<volume(h,r);
cout<<"enter the length,breadth and height of rectangle\n";
cout<<"volume of rectangle"<<"\n";
cin >>l>>b>>h1;
cout<<volume(l,b,h1);
getch();
return 0;
}
int volume(int s)
{
return(s*s*s);
}
double volume(double r,int h)
{
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

return (3.14519*r*r*h);
}
long volume (long l,int b,int h)
{
return (l*b*h);
}
Sample Output:
Enter the side of cube: 6
Volume of Cube: 216
Enter the radius and height of cylinder: 5 50
Volume of Cylinder: 39314.875
Enter the length , breadth and height of Rectangle : 5 10 20
Volume of Cylinder: 1000
Result
Thus above program executed and output verified.








WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:2 User defined function with default arguments
DATE:
Aim:
To write a C++ program to get net amount depending up on no of years deposited using
default arguments.
Algorithm:
1. Start the program
2. Declare no of years deposited as default argument
3. Accept the input which are required to calculate net amount after a particular year.
4. Call the total function to calculate the net amount if all the required argument are
not passed than default argument value will be substituted
5. Print the net amount.
6. Stop the program

/* User defined function with default arguments*/
Program
#include<iostream.h>
#include<conio.h>
float value(float p,int n,float r=0.15);
void printline(char ch='*',int len =40);
int main()
{
float amount,prin,r;
int n;
clrscr();
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

printline();
amount=value(5000.0,10);
cout<<"\nfinal amount with default arguments= "<<amount <<"\n";
cout<<"enter the principal amount\n";
cin>>prin;
cout<<"enter the rate of interest\n";
cin>>r;
cout<<"enter the no of years to be deposited\n";
cin>>n;
amount=value(prin,r,n);
printline('=');
cout<<"final amount= "<<amount<<"\n";
printline('*');
getch( );
return(0);
}
float value(float p,int n,float r)
{
int year=1;
float sum=p;
while(year<=n) {
sum=sum*(1+r);
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

year=year+1;
}
return (sum);
}
void printline(char ch,int len)
{
for (int i=1;i<=len;i++)
cout<<ch;
cout<<"\n";
}
Sample Output:
***************************************************
final amount with default arguments = 20227.789062
enter the principal amount:1000
enter the rate of interest :5
enter the no of years to be deposited:5
##################################################
final amount with default arguments :7776000
Result
Thus above program executed and output verified.


WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:3 Program using simple class object creation and invoking class
DATE:
AIM:
To write a c++ program to create inventory application using class concept.
ALGORITHM:
1. Start the program
2. Declare a class named as item with two data members.
3. Define member function getdata() to assign value for data member
4. Define member function putdata() to retrieve value of data member.
5. In main function create object for class item.
6. Call getdata() function to assign value.
7. Call putdata() function to retrieve value of member function.
8. Stop the program.
/* program using simple class object creation and invoking class */
Program
#include<iostream.h>
#include<conio.h>
class item
{
int number;
float cost;
public :
void getdata(int a,float b);
void putdata(void)
{
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

cout<<"number :"<<number<<"\n";
cout<<"cost "<<cost<<"\n";
}
};
void item ::getdata(int a,float b)
{
number=a;
cost=b;
}
int main()
{
int a;
float b;
item x;
clrscr();
cout<<"\nobject x"<<"\n";
x.getdata(100,29.9);
x.putdata();
item y;
cout<<"enter item no :\n";
cin >>a;
cout<<"enter item cost :\n";
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

cin>>b;
cout<<"\nobject y "<<"\n";
y.getdata(a,b);
y.putdata();
getch();
return 0;
}
Sample Output
Object X
Number:100
Cost :29.9
enter item no :40
enter item cost:60
Object y
Number:40
Cost :60
Result
Thus above program executed and output verified.




WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:4 program using constructor and destructor
DATE:
AIM:
To write a c++ program to copy one string to another string and concatenate one
string with another string using dynamic memory allocation(constructor).
ALGORITHM:
1. Start the program.
2. Declare a class string , define constructor which assign value of data member
at run time.
3. Define display () member function to display string.
4. Define destructor to release memory usage by object after its usage.
5. Define join member function to concatenate two string.
6. In main function create object for string which invoke constructor.
7. Using object invoke join function and dynamic constructor.
8. Display the resultant string.
9. Invoke destructor.
10. Stop the program.

/* program using constructor and destructor*/
Program
#include<iostream.h>
#include<string.h>
#include<conio.h>
int count=0;
class string
{
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

char *name;
int length;
public:
string()
{
length=0;
count++;
cout<<"\nno of objects created\n"<<count;
name=new char[length+1];
}
string(char *s)
{
length=strlen(s);
name=new char[length+1];
strcpy(name,s);
count++;
cout<<"\nno of objects created\n"<<count;
}
void display(void)
{
cout<<"\nname"<<"\t"<<name<<"\n";
}
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

void join(string &a,string &b)
{
length=a.length+b.length;
delete name;
name=new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);
}
~string()
{
cout<<"no of objects destroyed\n"<<count;
count--;
}
};
int main()
{
clrscr();
char *first="Josphine";
string name1(first);
string name2("Louis");
string name3("Langrange"),s1,s2;
getch();
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

s1.join(name1,name2);
s2.join(s1,name3);
name1.display();
getch();
name2.display();
getch();
name3.display();
getch();
s1.display();
getch();
s2.display();
getch();
return 0;
}
Sample Output:
no of objects created :1
no of objects created:2
no of objects created:3
no of objects created:4
no of objects created:5
name : Josphine
name : Louis
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

name : Langrange
name : Josphine Louis
name : Josphine Louis Langrange
no of objects destroyed:5
no of objects destroyed:4
no of objects destroyed:3
no of objects destroyed:2
no of objects destroyed:1

Result
Thus above program executed and output verified.










WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:5 Dynamic initialization of constructor
DATE:
AIM:
To write a c++ program to perform matrix addition.
ALGORITHM:
1. Start the program.
2. Declare a class matrix , define constructor which assign value of data member
at run time.
3. Prompt the user to enter no of rows and columns
4. Define input () member function to get input of first and second matrix
5. Define compute () member function to calculate addition of two matrix
6. Define destructor to release memory usage by object after its usage.
7. Define display () member function to display the resultant matrix.
8. In main function create object for matrix which invoke constructor.
9. Invoke input(),compute(),display() member function.
10. Invoke destructor.
11. Stop the program.
12.
/*Dynamic initialization of constructor*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
void input(int);
void display();
void compute();
class matrix
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

{
int **a,**b,**c;
int row,col,i,j,k;
public:
matrix(int r=0,int cl=0)
{
row=r;
col=cl;
a=new int *;
b=new int *;
c=new int *;

}
void input(int f)
{

for(j=0;j<row;j++)
for(k=0;k<col;k++)
{
cout<<endl<<endl<<"enter the value ["<<j<<"]["<<k<<"]\t";
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

if (f==1)
cin>>a[j][k];
if (f==2)
cin>>b[j][k];
}
}
void compute (matrix &m)
{
for(int j=0;j<m.row;j++)
{
for(int k=0;k<m.col;k++)
{
m.c[j][k]=m.a[j][k]+m.b[j][k];

cout <<m.c[j][k]<<"\t";
}
cout<<"\n";
}
}
void operator =(matrix &m2)
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

{
for(int j=0;j<m2.row;j++)
for(int k=0;k<m2.col;k++)
c[j][k]=m2.c[j][k];
}

void display(matrix &m)
{
cout<<endl<<endl<<"\t resultant matrix"<<endl;
for(j=0;j<m.row;j++)
{
for (k=0;k<m.col;k++)
{
cout<<"\t"<<m.c[j][k];
}
cout<<"\n";
}
}
~matrix()
{
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

cout<<"\nthe object has been destroyed";
}
};
void main()
{
int c,r;
clrscr();
cout<<"enter no of rows";
cin>>r;
cout<<"enter no of column ";
cin>>c;
matrix m(r,c);
cout<<"enter the element of first matrix";
m.input(1);
cout<<"enter the element of second matrix";
m.input(2);
matrix m2(r,c);
m2.compute(m);
matrix mr(m2);
mr=m2;
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

mr.display(m);
getch();
}
Sample Output
enter no of rows:2
enter no column:2
enter the element of first matrix
enter the value a[0][0]:1
enter the value a[0][1]:2
enter the value a[1][0]:3
enter the value a[1][1]:4
enter the element of second matrix
enter the value b[0][0]:1
enter the value b[0][1]:2
enter the value b[1][0]:3
enter the value a[1][1]:4
resultant matrix
2 4
6 8
the object has been destroyed
the object has been destroyed
the object has been destroyed
Result
Thus above program executed and output verified.
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:6 copy constructor
DATE:
AIM:
To write a c++ program to generate Fibonacci series using coppy constructor
concept.
ALGORITHM:
1. Start the program
2. Declare a class constructor,copy constructor.
3. Define member function increment () and display() member function
4. In main function create object for class item.
5. Assign the object to another object by invoking copy constructor.
6. Call increment() and display() member function to display fibbonacci series.
7. Stop the program

PROGRAM
/*copy constructor*/
#include<iostream.h>
#include<conio.h>
class fibonacci
{
private:
unsigned long int f0,f1,fib;
public:
fibonacci()
{
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

f0=-1;
f1=1;
fib=f0+f1;
}
fibonacci(fibonacci &ptr)
{
f0=ptr.f0;
f1=ptr.f1;
fib=ptr.fib;
}
void increment()
{
f0=f1;
f1=fib;
fib=f0+f1;
}
void display()
{
cout<<"\n"<<fib<<"\n";
}
};
void main()
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

{
clrscr();
fibonacci number;
cout<<Fibonacci series;
for(int i=0;i<15;i++)
{
number.display();
number.increment();
}
getch();
}
Sample output
Fibonnacci series
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Result
Thus above program executed and output verified.





WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA


EX.NO:7 Using Friend Function accessing private data member
DATE:
AIM:
To write a c++ program to display private data member using friend function
ALGORITHM:
1. Start the program
2. Declare a class and declare private data member
3. Define getdata() member function to assign value to private data member.
4. Define friend function diplay() to display private data member.
5. In main function create object for class item.
6. Invoke friend function to display private data member()
7. Stop the program
PROGRAM
/*using Friend Function accessing private data member */
#include<iostream.h>
#include<conio.h>
class friendf
{
private:
int x;
public:
inline void getdata();
friend void display(friendf abc)
{
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

cout<<"Entered number is="<<abc.x;
cout<<endl;
}
};
inline void friendf :: getdata()
{
cout<<"Enter a value for x";
cin>>x;
}
void main()
{
friendf obj;
obj.getdata();
cout<<"Accessing the private data for non member fn using friend
fn"<<"\n";
display(obj);getch();
}
Sample output
Entered number is= 5,Accessing the private data for non member fn
using friend fn
Entered number is= 5
Result
Thus above program executed and output verified.
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:8 Display Players detail Using Friend Function
DATE:
AIM:
To write a c++ program to display player details using friend function
ALGORITHM:
1. Start the program
2. Declare a class as palyer and declare private data member
3. Define input () member function to assign value to private data member.
4. Define member function diplay() to display private data member.
5. Define friend function listrank() to display rank of player depending on
scores.
6. Display player rank.
7. Stop the program
PROGRAM
/*using Friend Function accessing private data member */
#include<iostream.h>
#include<conio.h>
class player
{
char name[10];
int age,rank,run;
public:
void input()
{
cout<<"\nEnter the player name";
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

cin>>name;
cout<<"\nEnter the player age";
cin>>age;
cout<<"\nEnter the score of the player";
cin>>run;
}
void dispdetails()
{
cout<<"\n DETAILS OF THE PLAYER";
cout<<"\n Player name is"<<name<<"\n age is"<<age<<"\n score is"<<run;
}
friend void listrank(player p);
};
void listrank(player p)
{
if(p.run>=300)
{
p.dispdetails();
cout<<"\n Player's rank \t\t FIRST RANK";
}
else if(p.run>=200&&p.run<300)
{
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

p.dispdetails();
cout<<"\nPlayer's rank is\t\t SECOND RANK";
}
else if(p.run>=100&&p.run<200)
{
p.dispdetails();
cout<<"\nPlayer's rank is \t\t THIRD RANK";
}
else if(p.run<100)
{
p.dispdetails();
cout<<"Player's rank is \t\t not eligible";
}
}
void main()
{
int i;
clrscr();
player p[5];
for (i=0;i<5;i++)
{
p[i].input();
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

listrank(p[i]);
}
getch();
}
Sample output
Enter the player name:a
Enter the player age:25
Enter the score of the player500
DETAILS OF THE PLAYER
player name:a
player age:25
score of the player50
Player's rank is FIRST Rank
Enter the player name:b
Enter the player age:26
Enter the score of the player 60
DETAILS OF THE PLAYER
player name:b
player age:26
score of the player 60
Player's rank is NOT ELIGIBLE

Enter the player name:c
Enter the player age:24
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

Enter the score of the player 70
DETAILS OF THE PLAYER
player name:c
player age:24
score of the player 70
Player's rank is NOT ELIGIBLE
Enter the player name:d
Enter the player age:25
Enter the score of the player 35
DETAILS OF THE PLAYER
player name:d
player age:25
score of the player 35
Player's rank is NOT ELIGIBLE
Enter the player name:e
Enter the player age:25
Enter the score of the player 30
DETAILS OF THE PLAYER
player name:e
player age:25
score of the player 30
Player's rank is NOT ELIGIBLE
Result
Thus above program executed and output verified.

WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:9 Complex operation using operator overloading
DATE:
AIM:
To write a c++ program to perform complex no addition using operator
overloading.
ALGORITHM:
1. Start the program
2. Declare a class as complex with real and imaginary part as data member s
3. Define constructor overloading to assign different value for complex data
member
4. Define member function getdata() to get the value of complex no
5. Define operator function +() to perform complex addition
6. Define member function Display() to display complex number.
7. In main function create object,invoke constructor,member function and
operator function through object
8. Stop the program

/*complex operation using operator overloading */
Program
#include<iostream.h>
#include<conio.h>
class complex
{
double real,imag;
public :
complex (int r=0,int i=0)
{
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

real=r;
imag=i;
}
complex (double r,double i)
{
real=r;
imag=i;
}
void getdata()
{
cout<<"enter the real part and imaginary part\n";
cin >>real>>imag;
}
complex operator +(complex c2)
{
complex temp;
temp.real=real+c2.real;
temp.imag=imag+c2.imag;
return(temp);
}
void display()
{
cout<<"complex no is "<<real<<"+i"<<imag;
}
};
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

void main()
{
clrscr();
complex c1,c2,c3,c4(4,5),c5(3.5,7.8),c6;
c1.getdata();
c2.getdata();
c3=c1+c2;
c6=c4+c5;
cout <<"\n addition of two no's c1 &c2 \n";
c3.display();
cout<<"\n addition of two floating complex no c4 & c5 is\t ";
c6.display();
getch();
}

Sample output
enter the real part and imaginary part 1 2
enter the real part and imaginary part 3 4
addition of two no's c1 &c2 THE COMPLEX NO IS 4+I6
addition of two floating complex no c4 & c5 is THE COMPLEX NO IS
7.5+12.8
Result
Thus above program executed and output verified.
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:10 complex operation using operator overloading and type conversion
DATE:
AIM:
To write a c++ program to perform complex no subtraction using operator
overloading.and type conversion.
ALGORITHM:
1. Start the program
2. Declare a class as complex with real and imaginary part as data member s
3. Define constructor overloading to assign different value for complex data
member
4. Define member function getdata() to get the value of complex no
5. Define operator function -() to perform complex subtraction
6. Define conversion function double() to perform complex conversion.
7. Define member function Display() to display complex number.
8. In main function create object,invoke constructor,member function and
operator function and conversion function through object
9. Stop the program

PROGRAM:
/*complex operation using operator overloading and type conversion */
#include<iostream.h>
#include<conio.h>
class complex
{
int real,imag;
public :
complex (int r=0,int i=0)
{
real=r;
imag=i;
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

}

void getdata()
{
cout<<"enter the real part and imaginary part\n";
cin >>real>>imag;
}
complex operator -(complex c2)
{
complex temp;
temp.real=real-c2.real;
temp.imag=imag-c2.imag;
return(temp);
}
void display()
{
cout<<"complex no is "<<real<<"+i"<<imag;
}
operator double()
{
return(real+imag);
}
};

void main()
{
double d;
clrscr();
complex c1,c2,c3,c4(4,5),c5(3.5,4.789),c6(4.8,5.789);
c1.getdata();
c2.getdata();
c3=c1-c2;
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

cout<<"\n difference of two complex number\n";
c3.display();
//c6=c4+c5;
cout <<"\n integer to complex \n";
c4.display();
cout<<"\n double to complex is\n ";
c5.display();
cout<<"\ncomplex to double\n";
d=c6;
cout <<"\n"<<d;
getch();
}
Sample output
enter the real part and imaginary part 1 2
enter the real part and imaginary part 3 4
difference of two no's c1 &c2 -2+-i2
integer to complex
THE COMPLEX NO IS 4+I5
Double to complex
complex no3+I 4
complex to double
9
Result
Thus above program executed and output verified.

WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:11 Multilevel inheritence
DATE:
AIM:
To write a c++ program to find student score using multilevel inheritance
ALGORITHM:
1. Start the program
2. Declare a class as student and define getno() member function,putno()()
member function
3. Declare a class as test which is public inherited from students and define
getmarks() member function,putmarks()() member function
4. Declare a class as result which is public inherited from test and define
display() member function
5. In main function create object for test and invoke getno(), putno(),
getmarks(),putmarks(),display()member function through object
6. Stop the program
PROGRAM:
/*Program using Multilevel inheritence */
#include<iostream.h>
class student
{
protected:
int rollno;

public:
void getno(int);
void putno(void);
};

WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

void student:: getno(int a)
{
rollno=a;
}
void student ::putno()
{
cout<<"rollno: "<<rollno<<"\n";
}
class test:public student
{
protected:
float sub1;
float sub2;
public:
void getmarks(float,float);
void putmarks(void);
};
void test ::getmarks(float x,float y)
{
sub1=x;
sub2=y;
}
void test::putmarks()
{
cout<<"marks of subject one "<<sub1<<"\n";
cout<<"marks of subject two"<<sub2<<"\n";
}

WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

class result : public test
{
float total;
public:
void display(void);
};
void result ::display(void)
{
total=sub1+sub2;
putno();
putmarks();cout<<"total="<<total<<"\n";
}
int main()
{
float x,y,z;
result student1;
cout<<"enter student no &marks of subject";
cin>>x>>y>>z;
student1.getno(x);
student1.getmarks(y,z);
student1.display();
return(0);
}





WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA



Sample output
enter student no &marks of subject
1111345
59
76
rollno: 1111345
marks of subject one : 59
marks of subject two:76
total : 135

Result
Thus above program executed and output verified.








WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:12 Hybrid inheritance
DATE:
AIM:
To write a c++ program to find student score using Hybrid inheritance
ALGORITHM:
1. Start the program
2. Declare a class as student and define getno() member function,putno()()
member function
3. Declare a class as test which is public inherited from students and define
getmarks() member function,putmarks()() member function
4. Declare a class as sports and define getscore() member function,putscore()
member function
5. Declare a class as result which is public inherited from test and sports and
define display() member function
6. In main function create object for test and invoke getno(), putno(),
getmarks(),putmarks(),getscore(),putscore(),display()member function
through object
7. Stop the program
PROGRAM
/*Program using Hybrid inheritence */
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void getno(int);
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

void putno(void);
};

void student:: getno(int a)
{
rollno=a;
}
void student ::putno()
{
cout<<"rollno: "<<rollno<<"\n";
}
class test:public student
{
protected:
float sub1;
float sub2;
public:
void getmarks(float,float);
void putmarks(void);
};
void test ::getmarks(float x,float y)
{
sub1=x;
sub2=y;
}
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

void test::putmarks()
{
cout<<"marks of subject one "<<sub1<<"\n";
cout<<"marks of subject two"<<sub2<<"\n";
}
class sports
{
protected:
float score;
public:
void getscore(float s)
{
score=s;
}
void putscore(void)
{
cout<<"sportweight"<<score;
}
};

class result : public test,public sports
{
float total;
public:
void display(void);
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

};
void result ::display(void)
{
total=sub1+sub2+score;
putno();
putmarks();
putscore();
cout<<"total="<<total<<"\n";
}
int main()
{
float x,y,z;
clrscr();
result student1;
cout<<"enter student no &marks of subject";
cin>>x>>y>>z;
student1.getno(x);
student1.getmarks(y,z);
student1.getscore(40);
student1.display();
getch();
return(0);
}


WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

Sample output
enter student no &marks of subject
1111345
59
76
rollno: 1111345
marks of subject one : 59
marks of subject two:76
total : 175

Result
Thus above program executed and output verified.









WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:13 Run time polymorphism
DATE:
AIM:
To write a c++ program to implement run time polymorphism through virtual
function.
ALGORITHM:
1. Start the program
2. Declare a base and define display() member function to display base class
content. define show() member function to show base class content.
3. Declare a derived class inherit from base and define display() member
function to display derived class content. define show() member function to
derived class content.
4. In main function create object for base and derived class.
5. Assign base ptr to base class object.
6. Invoke display function of base class using base pointer variable
7. Invoke show function of base class using base pointer variable
8. Assign base ptr to derived class object.
9. Invoke display function of base class using base pointer variable
10. Invoke show function of derived class using base pointer variable
11. Stop the program
PROGRAM
/*Program using Run time polymorphism */
#include<iostream.h>
#include <conio.h>
class base
{
public:
void display()
{
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

cout<<"display base class\n";
}
virtual void show()
{
cout<<"SHOW BASE\n";
}
};

class derived :public base
{
public:
void display()
{
cout<<"display derived class\n";
}

void show()
{
cout<<" show derived\n";
}
};
int main()
{

base b;
derived d;
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

base *bptr;
clrscr();
cout<<"\n bptr points to base\n";
bptr=&b;
bptr->display();
bptr->show();
cout<<"\n\n bptr points to derived\n";
bptr=&d;
bptr->display();
bptr->show(); getch();
return 0;
}
Sample output
bptr points to base
display base class
SHOW BASE
display base class
show derived

Result:
Thus above program executed and output verified.





WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:14 Function Template
DATE:
AIM:
To write a c++ program to swap two numbers using function template
ALGORITHM:
1. Start the program
2. Create template class to define how to swap two numbers.
3. Declare two variables of int type and two variables of float type.
4. Call template function to swap two int variable and two float variable.
5. Print the result.
6. Stop the program
PROGRAM:
/*Program using Function Template */
#include<iostream.h>
#include<conio.h>
template <class t>
void swap(t &x,t &y)
{
t temp;
temp=x;
x=y;
y=temp;
}
void fun (int m,int n,float a,float b)
{
cout<<"m and n befor swap:\t\t"<<m<<"\t\t"<<n<<"\n";
swap(m,n);
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

cout<<"m and n after swap:\t\t"<<m<<"\t\t"<<n<<"\n";
cout<<"a and b befor swap:\t\t"<<a<<"\t\t"<<b<<"\n";
swap(a,b);
cout<<"a and b after swap:\t\t"<<a<<"\t\t"<<b<<"\n";
}
int main ()
{
int m,n;
float a,b;
clrscr();
cout<<"enter integer value for m and n ";
cin >> m>>n;
cout<<"enter float value for a and b ";
cin >> a>>b;
fun(m,n,a,b);
getch();
return 0;
}
Sample output
enter integer value for m and n :1 2
enter float value for a and b:1.5 4.7
m and n befor swap 1 2
m and n after swap 2 1
a and b befor swap 1.5 4.7
a and b after swap 4.7 1.5
Result:
Thus above program executed and output verified.
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA


EX.NO:15 Exception handling
DATE:
AIM:
To write a c++ program to handle exception during run time.
ALGORITHM:
1. Start the program
2. Create template class to define how to swap two numbers.
3. Declare two variables of int type and two variables of float type.
4. Call template function to swap two int variable and two float variable.
5. Print the result.
6. Stop the program
PROGRAM:
#include<iostream.h>
#include<process.h>
#include<conio.h>
#include<math.h>
void divide(int x,int y,int z)
{
cout<<"\nwe are inside the function\n";
if((x-y)!=0)
{
int r=z/(x-y);
cout<<"result ="<<r<<"\n";
}
else
{
throw(x-y);
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

}
}
int main()
{
try
{
cout<<"we are inside the try block\n";
divide(10,20,30);
divide(10,10,20);
}
catch (int i)
{
cout<<"caught the exception\n";
}
return 0;
}
Sample output
we are inside the try block
we are inside the function
result :3

caught the exception
Result:
Thus above program executed and output verified.





WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:16 Standard template library
DATE:

AIM:
T O write c++ program to search telephone number using map standard template library.
ALGORITHM:
1. Start the program
2. Get the name or telephone number
3. Depending up on the input given if name is given then phone no is retrieved from phone
directory and name is retrieved if number is given using map standard template library
4. Display the result
5. Stop the program
Program:
/*STANDARD TEMPLATE LIBRARY*/
#include<iostream.h>
#include<map.h>
#include<string.h>
typedef map<string,int> phonemap;
int main()
{
string name;
int number;
phonemap phone;
cout<<"enter three sets of name and no\n";
for(int i =0;i<=3;i++)
{
cin >>name;
cin>>number;
phone[name]=number;

WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

}
phone["jacob"]=4567;
phone.insert(pair<string,int>("boss",6666));
int n=phone.size();
cout<<"\nsize of map" <<n<<"\n\n";
cout<<"list of telephone no \n";
phonemap::iterator p;
for(p=phone.begin();p!=phone.end();p++)
{
cout<<(*p).first<<" "<<(*p).second<<"\n";
}
cout<<"\n";
cout<<"enter name";
cin>>name;
number=phone[name];
cout<<"no:"<<number<<"\n";
return 0;
}













WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

Sample output:
enter three sets of name and no
hema
123
Meera
456
Akila
789
size of map 3
list of telephone no
123
456
789
enter name
akila
no: 789
Result:
Thus above program executed and output verified.













WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:17 SIMPLE JAVA PROGRAM
DATE:
AIM:
T O write Simple java program to find square root of two numbers.
ALGORITHM:

1. Start the program
2. Declare class and static main function as public
3. Get the input from the user
4. Using mathematical function find the square root of the given number
5. Display the result
6. Stop the program.
/* simple java program*/
PROGRAM
import java.lang.Math;
class squareroot
{
public static void main(String args[])
{
double x=5;
double y;
y=Math.sqrt(x);
System.out.println("y ="+y);
}
}


WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA



Sample Output
Y=2.236067;
Result
Thus above program executed and output verified.















WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:18 FINDING AREA OF RECTANGLE
DATE:
AIM:
T O write simple java program using class to find area of a triangle
ALGORITHM:
1. Start the program
2. Define class rectangle and define member function getdata() to assign length and width value.
3. Define member function rectarea() to find area of rectangle.
4. In main function create the object and through the object invoke getdata ()and rectarea()
member function.
5. Display the result.
6. Stop the program.
PROGRAM:
class rectangle
{
int length,width;
void getdata(int x,int y)
{
length=x;
width=y;
}
int rectarea1()
{
int area=length*width;
return(area);
}
}
class rectarea
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

{
public static void main(String args[])
{
int area1,area2;
rectangle rect1=new rectangle();
rectangle rect2= new rectangle();
rect1.length=15;
rect1.width=10;area1=rect1.length*rect1.width;
rect2.getdata(14,55);
area2=rect2.rectarea1();
System.out.println("area1"+area1);
System.out.println("area2"+area2);
}
}

Sample Output

Area1:=1150
Area2:=2770

Result
Thus above program executed and output verified.





WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:19 Creating package and importing package in current application
DATE:
AIM:
T o create a package in java and import the user defined package in a current application
ALGORITHM:
1. Start the program
2. Create the package as mypackage and define a class called balance which consist of two data
members account holder name and balance
3. Define member function show() to display balance of the account holder.
4. Import the package in new class called as test balance which consist of main function
5. Create object for balance and call the display function.
6. Display the result.
7. Stop the program


PROGRAM
/* package creation*/
package mypackage;
public class balance
{
String name;
double bal;
public balance(String n,double b)
{
name=n;
bal=b;
}
public void show()
{
if(bal<0)
System.out.println("..................");
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

System.out.println(name: +name+bal ": $"+bal);
System.out.println("..................");

}
}

/* importing package*/
import mypackage .*;
class testbalance
{
public static void main(String args[])
{
balance test =new balance ("hai",65.88);
test.show();
}
}

Sample Output

name: hai bal : 65.88


Result
Thus above program executed and output verified.








WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:20 FINDING AREA OF RECTANGLE AND CIRCLE USING INTERFACE
DATE:

AIM:
T o write a java program to find area of circle and rectangle using interface
ALGORITHM:
1. Start the program
2. Create the interface name as area and declare variables and methods in interface
3. Define class rectangle and the class circle which implements the interface area and define
member function compute() for each class to compute area of rectangle and circle
4. Create a class called interfacetest define main function and Create object for rectangle and
circle
5. Create object for interface
6. Assign rectangle object to interface object and call compute function of rectangle to find area of
rectangle
7. Assign circle object to interface object and call compute function of circle to find area of circle.
8. Display the result.
9. Stop the program.

/* program explaining interface concept*/
PROGRAM
interface area
{
final static float pi=3.14f;

float compute(float x,float y);

}
class rectangle implements area
{
public float compute (float x,float y)
{
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

return(x*y);
}
}

class circle implements area
{
public float compute(float x,float y)
{
return (pi*x*y);
}
}
class interfacetest
{
public static void main(String args[])
{
rectangle rect =new rectangle();
circle cir =new circle();
area area1;
area1 =rect;
System.out.println("area of rectangle="+area1.compute(10,20));
area1=cir;
System.out.println("area of circle= "+area1.compute(10,2));
}
}
Sample Output
area of rectangle:=200
area of circle:=62.800003
Result
Thus above program executed and output verified.
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:21 To create student details using multiple inheritance through interface
DATE:
AIM:
To create student details using multiple inheritance through interface
ALGORITHM:
1. Start the program
2. Create the interface name as sports area and declare variables and methods in interface
3. Define a class named as student and define member function getno() to assign value of roll
no and putno() to retrieve value of roll no.
4. Define a class named as test which inherit from student define data member and member
function getmarks() to set value of different subjects and putmarks() to retrieve value of
different subject
5. Define a class results which inherit from test and implements from sports and Define
putwt() and diplay () member function.
6. Define a class hybrid a define main function and Create object for result and call
setno(),getmarks(),display() member function
7. Display the result.
8. Stop the program.

/* implementing multiple inheritance using interface*/
PROGRAM
class student
{
int rollno;
void getnumber(int n)
{
rollno=n;
}
void putnumber()
{
System.out.println("rollno "+rollno);
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

}

}
class test extends student
{
float part1,part2;
void getmarks(float m1,float m2)
{
part1=m1;
part2=m2;
}
void putmarks()
{
System.out.println("marks obtained");
System.out.println("part1 :"+part1);
System.out.println("part2 :"+part2);
}
}
interface sports
{
float sportwt=6.0f;
void putwt();
}
class results extends test implements sports
{
float total;
public void putwt()
{
System.out.println("Sports weight :"+ sportwt);
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

}
void display()
{
total=part1+part2+sportwt;
putnumber();
putmarks();
putwt();
System.out.println("Total score :="+total);
}
}
class hybrid
{
public static void main(String args[])
{
results student1 =new results();
student1.getnumber(1234);
student1.getmarks(66.9f,88.87f);
student1.display();
}
}
Sample Output
Roll no:1234
Marks obtained
Part 1: 66.9
Part 2: 68.87
Sports weight:6.0
Total score:161.77
Result
Thus above program executed and output verified.

WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:22 EXCEPTION HANDLING
DATE:
AIM:

To write java program to handle error or exception during runtime
ALGORITHM:
1. Start the program
2. Define function divide () which perform division operation.
3. Try to find with if there is any error occurs within divide function.
4. If find catch the exception and throw it to exception handler to take necessary action.
5. Stop the program

PROGRAM:
/* Exception handling*/
class exceptionhandling
{
public static void main(String args[])
{
int d,a;
try
{
d=0;
a=42/d;
System.out.println("this will not be printed");
}
catch(ArithmeticException e)
{
System.out.println("division by zero");
}
System.out.println("after catch statement");
} }
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA




Sample Output

division by zero

after catch statement


Result
Thus above program executed and output verified.

















WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:23 READING A STRING FROM CONSOLE USING BUFFERED READER CLASSS
DATE:
AIM:

To write java program to read a string from console using buffered reader class
ALGORITHM:
1. Start the program
2. Create object for buffered reader class
3. Through the object read or console the string until stop encountered.
4. Print the string.
5. Stop the program

PROGRAM:
/* READING A STRING FROM CONSOLE USING BUFFERED READER CLASSS*/
import java.io.*;
class breader
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter line of text");
System.out.println("enter stop to quit");
do
{
str=br.readLine();
System.out.println(str);
}
while(!str.equals("stop"));
}
}

WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA


Sample output

Enter line of text
enter stop to quit
hai
hai
hello
hello
bye
bye
stop


Result
Thus above program executed and output verified.















WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:24 program using print writer class to handle console output
DATE:
AIM:

To write java program to printwriter class to console output
ALGORITHM:
1. Start the program
2. Create object for print writer class
3. Through the object of printwriter console the output string.
4. Print the string.
5. Stop the program

PROGRAM:

/* program using print writer class to handle console output*/
import java.io.*;
class printwrite
{
public static void main(String args[]) throws IOException
{
PrintWriter pw =new PrintWriter(System.out,true);
int i=-7;
pw.println("this is a string");
pw.println(i);
double d=4.5e-8;
pw.println(d);
}
}

WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

Sample output
this is a string
-7
4.5e-8;
Result
Thus above program executed and output verified.






























WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

EX.NO:25 Program using Multi threading concept
DATE:
AIM:

To write java program to execute a multiple thread simultaneously
ALGORITHM:
1. Create class A which extends thread and initializes running
2. Create class B which extends thread and initializes running
3. Create class C which extends thread and initializes running.
4. After finishes job terminates job A
5. After finishes job terminates job B
6. After finishes job terminates job
7. Stop the program

PROGRAM

class a extend Threads
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("from thread A :i= "+i);
}
System.out.println("exit from A");
}
}
class b extend Threads
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println"from thread B :j= "+j);
}
System.out.println("exit from B");
}
}
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

class c extend Threads
{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println"from thread C :k= "+k);
}
System.out.println("exit from C");
}
}
class threadtest
{
public static void main(String args[])
{
new a().Start();
new b().Start();
new c().Start();
}
}
Sample output
from thread A :1
from thread A :2
from thread A :3
from thread B :1
from thread B :2
from thread C :1
from thread A :4
from thread B :3
from thread C :2
from thread C :3
from thread A :5
exit from A
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA

from thread B : 4
from thread B: 5
exit from B
from thread C :4
from thread C :5
exit from C
Result
Thus above program executed and output verified.

You might also like