Upload
thalapathy-raja
View
9
Download
0
Tags:
Embed Size (px)
Citation preview
5/23/2018 Www.vidyarthiplus.com VP Attachment
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.
5/23/2018 Www.vidyarthiplus.com VP Attachment
2/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
LIST OF EXPERIMENTS FOR OBJECT ORIENTED
PROGRAMMING LABCS2312
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
5/23/2018 Www.vidyarthiplus.com VP Attachment
3/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
LIST OF EXPERIMENTS FOR OBJECT ORIENTED
PROGRAMMING LABCS2312
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 .
5/23/2018 Www.vidyarthiplus.com VP Attachment
4/78
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 ofcube.
6. Invoke volume function of Rectangle by passing length, breadth and height tofind volume of rectangle.
7. Print the volume of cube, cylinder and rectangle8. Stop the program.
Program
/* Function overloading*/
#include
#include
int volume(int);
double volume(double,int);
long volume(long,int,int);
int main()
{
5/23/2018 Www.vidyarthiplus.com VP Attachment
5/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
int n,r,b,h1;
double h;
long l;
clrscr();
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
6/78
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.
5/23/2018 Www.vidyarthiplus.com VP Attachment
7/78
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 program2. Declare no of years deposited as default argument3. 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
#include
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();
5/23/2018 Www.vidyarthiplus.com VP Attachment
8/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
printline();
amount=value(5000.0,10);
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
9/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
year=year+1;
}
return (sum);
}
void printline(char ch,int len)
{
for (int i=1;i
5/23/2018 Www.vidyarthiplus.com VP Attachment
10/78
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 program2. Declare a class named as item with two data members.3. Define member function getdata() to assign value for data member4. 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
#include
class item
{
int number;
float cost;
public :
void getdata(int a,float b);
void putdata(void)
{
5/23/2018 Www.vidyarthiplus.com VP Attachment
11/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
12/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
cin>>b;
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
13/78
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
#include
#include
int count=0;
class string
{
5/23/2018 Www.vidyarthiplus.com VP Attachment
14/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
char *name;
int length;
public:
string()
{
length=0;
count++;
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
15/78
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
5/23/2018 Www.vidyarthiplus.com VP Attachment
16/78
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
5/23/2018 Www.vidyarthiplus.com VP Attachment
17/78
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.
5/23/2018 Www.vidyarthiplus.com VP Attachment
18/78
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 columns4. Define input () member function to get input of first and second matrix5. Define compute () member function to calculate addition of two matrix6. 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
#include
#include
void input(int);
void display();
void compute();
class matrix
5/23/2018 Www.vidyarthiplus.com VP Attachment
19/78
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
5/23/2018 Www.vidyarthiplus.com VP Attachment
20/78
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
5/23/2018 Www.vidyarthiplus.com VP Attachment
21/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
{
for(int j=0;j
5/23/2018 Www.vidyarthiplus.com VP Attachment
22/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
23/78
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.
5/23/2018 Www.vidyarthiplus.com VP Attachment
24/78
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 program2. Declare a class constructor,copy constructor.3. Define member function increment () and display() member function4. 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
#include
class fibonacci
{
private:
unsigned long int f0,f1,fib;
public:
fibonacci()
{
5/23/2018 Www.vidyarthiplus.com VP Attachment
25/78
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
5/23/2018 Www.vidyarthiplus.com VP Attachment
26/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
{
clrscr();
fibonacci number;
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
27/78
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 program2. Declare a class and declare private data member3. 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
#include
class friendf
{
private:
int x;
public:
inline void getdata();
friend void display(friendf abc)
{
5/23/2018 Www.vidyarthiplus.com VP Attachment
28/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
29/78
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 program2. Declare a class as palyer and declare private data member3. 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
#include
class player
{
char name[10];
int age,rank,run;
public:
void input()
{
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
30/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
cin>>name;
coutage;
coutrun;
}
void dispdetails()
{
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
31/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
p.dispdetails();
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
32/78
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
5/23/2018 Www.vidyarthiplus.com VP Attachment
33/78
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.
5/23/2018 Www.vidyarthiplus.com VP Attachment
34/78
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 program2. Declare a class as complex with real and imaginary part as data member s3. Define constructor overloading to assign different value for complex data
member
4. Define member function getdata() to get the value of complex no5. Define operator function +() to perform complex addition6. 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
#include
class complex
{
double real,imag;
public :
complex (int r=0,int i=0)
{
5/23/2018 Www.vidyarthiplus.com VP Attachment
35/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
real=r;
imag=i;
}
complex (double r,double i)
{
real=r;
imag=i;
}
void getdata()
{
coutreal>>imag;
}
complex operator +(complex c2)
{
complex temp;
temp.real=real+c2.real;
temp.imag=imag+c2.imag;
return(temp);
}
void display()
{
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
36/78
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
5/23/2018 Www.vidyarthiplus.com VP Attachment
37/78
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 program2. Declare a class as complex with real and imaginary part as data member s3. Define constructor overloading to assign different value for complex data
member
4. Define member function getdata() to get the value of complex no5. Define operator function -() to perform complex subtraction6. 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 programPROGRAM:
/*complex operation using operator overloading and type conversion */
#include
#include
class complex
{
int real,imag;
public :
complex (int r=0,int i=0)
{
real=r;
imag=i;
5/23/2018 Www.vidyarthiplus.com VP Attachment
38/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
}
void getdata()
{
coutreal>>imag;
}
complex operator -(complex c2)
{
complex temp;
temp.real=real-c2.real;
temp.imag=imag-c2.imag;return(temp);
}
void display()
{
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
39/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
40/78
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 program2. 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 definegetmarks() member function,putmarks()() member function
4. Declare a class as result which is public inherited from test and definedisplay() member function
5. In main function create object for test and invoke getno(), putno(),getmarks(),putmarks(),display()member function through object
6. Stop the programPROGRAM:
/*Program using Multilevel inheritence */
#include
class student
{
protected:
int rollno;
public:
void getno(int);
void putno(void);
};
5/23/2018 Www.vidyarthiplus.com VP Attachment
41/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
void student:: getno(int a)
{
rollno=a;
}void student ::putno()
{
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
42/78
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
5/23/2018 Www.vidyarthiplus.com VP Attachment
43/78
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.
5/23/2018 Www.vidyarthiplus.com VP Attachment
44/78
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 program2. 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 definegetmarks() 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 anddefine 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 programPROGRAM
/*Program using Hybrid inheritence */
#include
#include
class student
{protected:
int rollno;
public:
void getno(int);
5/23/2018 Www.vidyarthiplus.com VP Attachment
45/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
void putno(void);
};
void student:: getno(int a)
{
rollno=a;
}
void student ::putno()
{
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
46/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
void test::putmarks()
{
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
47/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
};
void result ::display(void)
{
total=sub1+sub2+score;
putno();
putmarks();
putscore();
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
48/78
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.
5/23/2018 Www.vidyarthiplus.com VP Attachment
49/78
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 program2. 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() memberfunction 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 variable7. Invoke show function of base class using base pointer variable8. Assign base ptr to derived class object.9. Invoke display function of base class using base pointer variable10.Invoke show function of derived class using base pointer variable11.Stop the program
PROGRAM
/*Program using Run time polymorphism */
#include
#include
class base
{
public:
void display()
{
5/23/2018 Www.vidyarthiplus.com VP Attachment
50/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
51/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
base *bptr;
clrscr();
coutshow();
coutshow(); getch();return 0;
}
Sample output
bptr points to base
display base class
SHOW BASEdisplay base class
show derived
Result:
Thus above program executed and output verified.
5/23/2018 Www.vidyarthiplus.com VP Attachment
52/78
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 program2. 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
#include
template
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
5/23/2018 Www.vidyarthiplus.com VP Attachment
53/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
54/78
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 program2. 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
#include
#include
#include
void divide(int x,int y,int z)
{
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
55/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
}
}
int main()
{try
{
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
56/78
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 program2. Get the name or telephone number3. 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 result5. Stop the program
Program:
/*STANDARD TEMPLATE LIBRARY*/
#include
#include
#include
typedef map phonemap;
int main()
{
string name;
int number;
phonemap phone;
cout>number;
phone[name]=number;
5/23/2018 Www.vidyarthiplus.com VP Attachment
57/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
}
phone["jacob"]=4567;
phone.insert(pair("boss",6666));
int n=phone.size();
cout
5/23/2018 Www.vidyarthiplus.com VP Attachment
58/78
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.
5/23/2018 Www.vidyarthiplus.com VP Attachment
59/78
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 program2. Declare class and static main function as public3. Get the input from the user4. Using mathematical function find the square root of the given number5. Display the result6. 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);
}
}
5/23/2018 Www.vidyarthiplus.com VP Attachment
60/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
Sample Output
Y=2.236067;
Result
Thus above program executed and output verified.
5/23/2018 Www.vidyarthiplus.com VP Attachment
61/78
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 program2. 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
5/23/2018 Www.vidyarthiplus.com VP Attachment
62/78
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.
5/23/2018 Www.vidyarthiplus.com VP Attachment
63/78
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 program2. 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 function5. 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
5/23/2018 Www.vidyarthiplus.com VP Attachment
64/78
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.
5/23/2018 Www.vidyarthiplus.com VP Attachment
65/78
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 program2. Create the interface name as area and declare variables and methods in interface3. 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 andcircle
5. Create object for interface6. 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)
{
5/23/2018 Www.vidyarthiplus.com VP Attachment
66/78
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 Outputarea of rectangle:=200
area of circle:=62.800003
Result
Thus above program executed and output verified.
5/23/2018 Www.vidyarthiplus.com VP Attachment
67/78
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 program2. Create the interface name as sports area and declare variables and methods in interface3. 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 memberfunction getmarks() to set value of different subjects and putmarks() to retrieve value of
different subject5. 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 callsetno(),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);
5/23/2018 Www.vidyarthiplus.com VP Attachment
68/78
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);
5/23/2018 Www.vidyarthiplus.com VP Attachment
69/78
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.
5/23/2018 Www.vidyarthiplus.com VP Attachment
70/78
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 program2. 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 programPROGRAM:
/* 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");
} }
5/23/2018 Www.vidyarthiplus.com VP Attachment
71/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
Sample Output
division by zero
after catch statement
ResultThus above program executed and output verified.
5/23/2018 Www.vidyarthiplus.com VP Attachment
72/78
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 program2. Create object for buffered reader class3. 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"));
}
}
5/23/2018 Www.vidyarthiplus.com VP Attachment
73/78
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.
5/23/2018 Www.vidyarthiplus.com VP Attachment
74/78
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 program2. Create object for print writer class3. 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);
}
}
5/23/2018 Www.vidyarthiplus.com VP Attachment
75/78
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.
5/23/2018 Www.vidyarthiplus.com VP Attachment
76/78
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 running2. Create class B which extends thread and initializes running3. Create class C which extends thread and initializes running.4. After finishes job terminates job A5. After finishes job terminates job B6. After finishes job terminates job7. Stop the program
PROGRAM
class a extend Threads
{
public void run()
{
for(int i=1;i
5/23/2018 Www.vidyarthiplus.com VP Attachment
77/78
WWW.VIDYARTHIPLUS.COM
WWW.VIDYARTHIPLUS.COM MAGNA
class c extend Threads
{
public void run()
{
for(int k=1;k
5/23/2018 Www.vidyarthiplus.com VP Attachment
78/78
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.