oops lab manual

78
EX NO : 1 SIMPLE CLASS DESIGN AND OBJECT CREATION DATE: AIM: To write the C++ program to illustrate the concept of class and object in c++. ALGORITHM: Step1: Start the program Step2: Define a class num. Step3: Define the member function read() to get the input. Step4: Define the member function max() to find the max number. Step5: Define the member function show() to display the values. Step6: Stop the program PROGRAM: #include<iostream.h> #include<conio.h> class num { private: int n1,n2; public: void read() { cout<<endl<<"Enter the two numbers"<<endl; cin>>n1>>n2; } int max() { if(n1>n2) return n1; else return n2; } void show() { cout<<"Max="<<max(); 1

Upload: kalpanasripathi

Post on 22-Jul-2016

17 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: OOPS Lab Manual

EX NO : 1 SIMPLE CLASS DESIGN AND OBJECT CREATIONDATE:

AIM:To write the C++ program to illustrate the concept of class and object in c++.

ALGORITHM:Step1: Start the programStep2: Define a class num.Step3: Define the member function read() to get the input.Step4: Define the member function max() to find the max number.Step5: Define the member function show() to display the values.Step6: Stop the program

PROGRAM:#include<iostream.h>#include<conio.h>class num{private:int n1,n2;public:void read(){cout<<endl<<"Enter the two numbers"<<endl;cin>>n1>>n2;}int max(){if(n1>n2)return n1;elsereturn n2;}void show(){cout<<"Max="<<max();}};void main(){clrscr();num obj1,obj2;obj1.read();obj1.show();

1

Page 2: OOPS Lab Manual

obj2.read();obj2.show();getch();}

OUTPUT:Enter the two numbers45 22Max=45Enter the two numbers56 78Max=78

RESULT:Thus the program for simple class design and object creation executed successfully using C++

language.

2

Page 3: OOPS Lab Manual

EX. NO: 2 STUDENT DETAILS USING CLASSES AND OBJECTDATE:

AIM: To write a C++ program to display the student details using classes and object as array.

ALGORITHM:

Step 1: Create a class as student.Step 2: Declare the data members rollno, name, mark1, mark2, mark3, total and average.Step 3: Declare the member functions as getdata() and displaydata().

3.1 getdata() method used to get the student details.3.2 displaydata() method used to display the student details.

Step 4: In the main, create an object array for the student class using the following syntax:

Classname objectname[size];Step 5: Get the number of students.Step 6: In getdata() method, get the student details using for loop.Step 7: In displaydata() method, display the student details using for loop.Step 8: Use the following syntax to call the member functions from the main

Objectname.methodname();

PROGRAM:

#include<iostream.h>#include<conio.h>#include<iomanip.h>class student { int rno; char name[20]; int m1,m2,m3,t; float avg; public: void getdata(); void displaydata(); }; void student :: getdata() { cout<<"Enter the roll no:"; cin>>rno; cout<<"Enter the name:"; cin>>name; cout<<"Enter the mark 1:"; cin>>m1; cout<<"Enter the mark 2:";

3

Page 4: OOPS Lab Manual

cin>>m2; cout<<"Enter the mark 3:"; cin>>m3; t=m1+m2+m3; avg=t/3; } void student :: displaydata() { cout<<setw(4)<<rno; cout<<setw(10)<<name; cout<<setw(6)<<m1<<setw(8)<<m2<<setw(8)<<m3; cout<<setw(6)<<t<<setw(7); cout<<avg<<endl; } void main() { student a[10]; clrscr(); int n; cout<<"Enter the range of the student\n"; cin>>n; for(int i=0;i<n;i++) { a[i].getdata(); } cout<<"\n\n"; cout<<"**************************************************"<<endl; cout<<"\t"<<" Student Details: "<<endl; cout<<"**************************************************"<<endl;

cout<<setw(5)<<"Rollno"<<setw(8)<<"Name"<<setw(8)<<"Mark1"<<setw(8)<<"Mark2"<<setw(8)<<"Mark3"<<setw(6)<<"Total"<<setw(6)<<"Avg"<<"\n"; for(int j=0;j<n;j++) { a[j].displaydata(); }

getch(); }

OUTPUT :Enter the range of the student 2Enter the roll no:1Enter the name:maryEnter the mark 1:89Enter the mark 2:89Enter the mark 3:78

4

Page 5: OOPS Lab Manual

Enter the roll no:2Enter the name:jimEnter the mark 1:67Enter the mark 2:78Enter the mark 3:89Enter the roll no:2Enter the name:jackEnter the mark 1:78Enter the mark 2:89Enter the mark 3:67

************************************************** Student Details:**************************************************Rollno Name Mark1 Mark2 Mark3 Total Avg 1 mary 89 89 78 256 85 2 jim 67 78 89 234 78 3 jack 78 89 67 234 78

RESULT:Thus the program was executed successfully.

5

Page 6: OOPS Lab Manual

EX:NO:3 POINTER TO DATA MEMBERDATE:

AIM:

To write a C++ program to find the area of rectangle using pointer to data member.

ALGORITHM:

Step 1: Create a class as rectangle.Step 2: Declare the data members a, b, *x,*y.Step 3: Declare the member functions as getdata() and area().

3.1 In the getdata() function, get the length and breadth of the rectangle and store their address to the pointer variable.

3.2 In the area() function, display the area of the rectangle.Step 4: In the main, create an object for the rectangle class using the following syntax:

Classname objectname;Step 5: Call the getdata() and area() function using the following syntax:

Objectname.methodname();

PROGRAM:

#include<iostream.h>#include<conio.h>class rectangle{ int a,b,*x,*y; public: void getdata(); void area();};void rectangle :: getdata() { cout<<"Enter the length of the rectangle:"; cin>>a; cout<<"Enter the breadth of the rectangle:"; cin>>b; x=&a; y=&b; }

void rectangle :: area() { cout<<"The area of the rectangle is:"<<*x**y; }

6

Page 7: OOPS Lab Manual

void main() { rectangle r; clrscr(); r.getdata(); r.area(); getch(); }

OUTPUT:Enter the length of the rectangle:12Enter the breadth of the rectangle:15The area of the rectangle is:180

RESULT:Thus the program was executed successfully.

7

Page 8: OOPS Lab Manual

EX NO :4 CLASS DESIGN IN C++ USING NAMESPACEDATE:

AIM: To write a C++ program to implement the concept of namespace.

ALGORITHM:Step 1: Start the program.Step 2: Include all the header files.Step 3: Create a class namespace one, assign values for a.Step 4: Create a class namespace two, assign values for a.Step 5: In the main function, class namespace two is called.Step 6: Print the value.Step 7: End the program.

PROGRAM:#include<iostream.h>namespace one{int a=100;}namespace two{float a=98.456;}void main(){using namespace two;cout<<a<<end<<(a/2)<<endl;}

OUTPUT98.45649.228

RESULT:Thus the program for namespace was executed successfully in C++ language.

8

Page 9: OOPS Lab Manual

EX NO: 5 FUNCTION OVERLOADING – PROGRAM 1DATE:

AIM: To write a C++ program to implement the concept of function overloading.

ALGORITHM:Step 1: Start the program.Step 2: Function add is created with different arguments such as one, two, three.Step 3: In first function it performs operation with single variable.Step 4: In second function it performs the operation of two variables.Step 5: In third function it performs the operation of three variables.Step 6: In main function the output of the three functions are displayed.Step 7: End the program.

PROGRAM:#include<iostream.h>#include<conio.h>void add(int a){a=a+10;cout<<"a:"<<a<<endl;}void add(int a,int b){int c;c=a+b;cout<<"c:"<<c<<endl;}void add(int a,int b,int c){int d;d=a+b+c;cout<<"d:"<<d<<endl;}void main(){clrscr();add(10);add(10,20);add(10,20,30);getch();}

9

Page 10: OOPS Lab Manual

OUTPUTa:20c:30d:60

RESULT:Thus the program for function overloading using C++ concept was implemented.

10

Page 11: OOPS Lab Manual

EX. NO:6 FUNCTION OVERLOADING – PROGRAM 2DATE:

AIM: To write a C++ program to find the volume of cube, rectangle and cylinder using function overloading.

ALGORITHM:

Step 1: Create a class as shape.Step 2: Declare the data members as a, l, b, h, and r.Step 3: Declare the member function as volume with different arguments.Step 4: Volume function calculates the volume of cube, cylinder and rectangle based on the parameter passed to it.Step 5: In the main, create the object for the shape class.Step 6: Call the function using objectname.functionname();

PROGRAM:

#include<iostream.h>#include<conio.h>class shape { int a,l,b,h; float r; public: void volume(int); void volume(int,int,int); void volume(float,int); }; void shape :: volume(int a) { cout<<"Volume of the cube is:"<<a*a*a; } void shape :: volume(int l,int b,int h) { cout<<"Volume of the rectangle is :"<<l*b*h; } void shape :: volume(float r,int h) { cout<<"Volume of the cylinder is:"<<0.33*3.14*r*r*h; } void main() { shape s;

11

Page 12: OOPS Lab Manual

int a1,l1,b1,h1,h2; float r1; clrscr(); cout<<"CUBE:"<<endl; cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl; cout<<"Enter the value of a:"; cin>>a1; s.volume(a1); cout<<endl<<endl<<"RECTANGLE:"<<endl; cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl; cout<<"Enter the value of length, breadth and height:"; cin>>l1>>b1>>h1; s.volume(l1,b1,h1); cout<<endl<<endl<<"CYLINDER"<<endl; cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl; cout<<"Enter the radius and height:"; cin>>r1>>h2; s.volume(r1,h2); getch(); }

OUTPUT:CUBE:~~~~~~~~~~~~~~~~~~~~Enter the value of a:7Volume of the cube is:343

RECTANGLE:~~~~~~~~~~~~~~~~~~~~Enter the value of length, breadth and height:645Volume of the rectangle is :120

CYLINDER~~~~~~~~~~~~~~~~~~~~Enter the radius and height:5.43Volume of the cylinder is:90.646779

RESULT:Thus the program was executed successfully.

12

Page 13: OOPS Lab Manual

EX. NO: 7 CALCULATION OF BANK INTEREST USING DEFAULT ARGUMENT DATE:

AIM: To calculate bank interest using the concept of default arguments

ALGORITHM:Step 1 : Start the process.Step 2 : Create a class with sufficient data members and two member functions.Step 3 : The default values are specified in the classStep 4: The two member functions are defined outside the class.Step 5: The object is created for the class and the member function is called from the main function.

Step 6: The function assigns a default values to the parameter which does not have a matching argument and function call.Step 7: If the values for the parameter are specified then the default value is not taken.Step 8: The bank interest is calculated.Step 9 : Display the result.Step 10 :Stop the process.

PROGRAM:#include<iostream.h>#include<conio.h>class arg{

float sum,amount,p,n,r;public:

float value(float p,int n,float r=0.15);void print(char ch='*', int len=13);

};float arg::value(float p,int n, float r){

int year=1;float sum=p;while(year<=n)

{sum=sum*(1+r);year=year+1;

}return(sum);

}void arg:: print(char ch, int len){

for(int i=1;i<=len;i++)cout<<ch;cout<<"\n";

13

Page 14: OOPS Lab Manual

}void main(){

arg a1;float tot, res;clrscr();a1.print();tot=a1.value(5000,5);cout<<tot<<endl;a1.print();cout<<endl;res=a1.value(5000,1,0.20);cout<<res;getch();

}

OUTPUT:****************************10056.786133****************************6000

RESULT:Thus the program for calculation of simple interest using default arguments was executed.

14

Page 15: OOPS Lab Manual

EX.NO:8 CONSTRUCTOR OVERLOADINGDATE:

AIM:

To write a C++ program to display the account number and balance using constructor overloading.

ALGORITHM:Step 1: Create a class as Account.Step 2: Declare the data members as accountid and balance.Step 3: Declare the member functions as

void Account() implies default constructorvoid Account(float) implies one argument constructorvoid Account(int,float) implies two argument constructorvoid moneytransfer(Account,float)void display()

Step 4: In main, create the object for Account class.Step 5: While creating the object without argument, the default constructor is called.Step 6: While creating the object with one argument, constructor with one argument is

called and value is assigned to the accoundid.Step 7: Call the display() function to display the account details.Step 8: While creating the object with two arguments, constructor with two arguments is

called and value is assigned to the accoundid and balance.Step 9: Call the display() function to display the account details.Step 10: Call the money transfer() function to transfer the money and display the balance

after money transfer.

PROGRAM:#include<iostream.h>#include<conio.h>

class Account{

int accid;float balance;

public:Account();Account(float );Account(int ,float);void moneytransfer(Account&,float);void display();

};

Account::Account(){

15

Page 16: OOPS Lab Manual

accid=1;balance=1000.0;

}

Account::Account(float y){

accid=2;balance=y;

}

Account::Account(int x,float z){

accid=x;balance=z;

}

void Account::display(){

cout<<"\t"<<accid;cout<<"\t\t"<<balance<<"\n";

}

void Account::moneytransfer(Account &ac1,float amount){

ac1.balance=ac1.balance+amount;balance=balance-amount;

}

void main(){

clrscr();

float a;cout<<"\nEnter the amount to be transfer:";cin>>a;

Account ac1;Account ac2(25000);Account ac3(3,45000);

cout<<"\n\nBefore Money Transfer:\n";cout<<"\tAccountID\tAmount\n";ac1.display();ac2.display();ac3.display();ac3.moneytransfer(ac1,a);

16

Page 17: OOPS Lab Manual

cout<<"\n\n After Money Transfer:\n";cout<<"\tAccountID\tAmount\n";ac1.display();ac2.display();ac3.display();

}

OUTPUT:Enter the amount to be transfer:500

Before Money Transfer: AccountID Amount 1 1000 2 25000 3 45000

After Money Transfer: AccountID Amount 1 1500 2 25000 3 44500

RESULT:Thus the program was executed successfully.

17

Page 18: OOPS Lab Manual

EX.NO: 9 OPERATOR OVERLOADINGDATE:

AIM: To write a C++ program to implement the overloading of - and + operator.

ALGORITHM:Step 1: Start the program.Step 2: Declare all the header files.Step 3: Create a class called sample and declare the variables.Step 4: Constructor is created and the variables are assigned there.Step 5: The operators '+' and '-' are overloaded as per the syntax and the result is made to display.Step 6: An object is created for the class in the main function.Step7: The functions are accessed using the object.Step8 : End the program.

PROGRAM:#include<iostream.h>#include<conio.h>class sample{ int x,y; public: sample() { } sample(int a,int b) { x=a; y=b; } sample operator +(sample s) { s.x=x+s.y; s.y=x+s.y; return(s); } sample operator -(sample s) { s.x=x-s.y; s.y=x-s.y; return(s); } void display() { cout<<"x="<<x<<endl;

18

Page 19: OOPS Lab Manual

cout<<"y="<<y<<endl; }};void main(){ clrscr(); sample s1(10,10); sample s2(20,20); sample s3,s4; s3=s1+s2; s4=s1-s2; s3.display(); s4.display(); getch();}

OUTPUT:x=30y=30x=-10y=-10

RESULT:Thus the program for operator overloading was executed.

19

Page 20: OOPS Lab Manual

EX.NO.10 UNARY OPERATOR OVERLOADINGDATE:

AIM: To write a C++ program for unary operator overloading.

ALGORITHM:Step 1: Declare the class as count.Step 2: Declare the data member as c.Step 3: Declare the member function as

count( )void operator ++( )int getcount( )

Step 4: count() function is used to initialize the “c” value to zero.Step 5: getcount() function returns the value of c.Step 6: operator ++() function is used to increment the value.Step 7: In the main, create the object for the class count.Step 8: Unary operator function is called by using the syntax operator objectname.Step 9: Display the value of c using getcount() function.

PROGRAM:#include<iostream.h>#include<conio.h>class count{

int c;public:

count(){c=0;}void operator ++(){c=c+1;}int getcount(){return(c);}

};

void main(){

clrscr();count c1,c2;

20

Page 21: OOPS Lab Manual

cout<<"\nThe value of c in c1="<<c1.getcount();cout<<"\nThe value of c in c2="<<c2.getcount();++c1;++c2;++c2;cout<<"\nThe value of c in c1="<<c1.getcount();cout<<"\nThe value of c in c2="<<c2.getcount();

}

OUTPUT:The value of c in c1=0The value of c in c2=0The value of c in c1=1The value of c in c2=2

RESULT:Thus the program was executed successfully.

21

Page 22: OOPS Lab Manual

EX.NO.11 BINARY OPERATOR OVERLOADING

DATE:

AIM:To write a C++ program to add two complex numbers using binary operator overloading.

ALGORITHM:Step 1: Create a class as complex.Step 2: Declare the data members as real and img.Step 3: Declare the member functions as

complex()complex operator +(complex c2)void read()void print()

Step 4: Constructor is used to initialize the data members to zero.Step 5: read() method is used to get the complex numbers.Step 6: print() method is used to display the complex numbers.Step 7: operator overloading function is used to perform add operation on complex

numbers.Step 8: In main, create the object for class complex.Step 9: Call the binary operator overloading function using the syntax objectname operator objectname Step10: Add the two complex numbers and display the result.

PROGRAM:#include<iostream.h>#include<conio.h>

class complex{ int real,img; public: complex() {

real=0;img=0;

}

complex operator +(complex c2) {

complex c;c.real=real+c2.real;c.img=img+c2.img;return c;

}

void read()

22

Page 23: OOPS Lab Manual

{cout<<"\n ENTER THE REAL AND THE IMAGINARY PART:";cin>>real>>img;

}

void print() {

cout<<real<<"+"<<img<<"i"<<"\n"; }};

void main(){ clrscr(); complex c1,c2,c3; c1.read(); c2.read(); c3=c1+c2; cout<<"\n\n"; cout<<"The Complex Number of C1: "; c1.print(); cout<<"\n\nThe Complex Number of C2: "; c2.print(); cout<<"\n\n THE ADDITION IS:"; c3.print(); getch();}

OUTPUT: ENTER THE REAL AND THE IMAGINARY PART:6 8

ENTER THE REAL AND THE IMAGINARY PART:3 4

THE COMPLEX NUMBER OF C1: 6+8i

THE COMPLEX NUMBER OF C2: 3+4i

THE ADDITION IS:9+12i

RESULT:Thus the program was executed successfully.

23

Page 24: OOPS Lab Manual

EX NO :12 CLASS DESIGN IN C++ USING NAMESPACEDATE:

AIM: To write a C++ program to implement the concept of namespace.

ALGORITHM:Step 1: Start the program.Step 2: Include all the header files.Step 3: Create a class namespace one, assign values for a.Step 4: Create a class namespace two, assign values for a.Step 5: In the main function, class namespace two is called.Step 6: Print the value.Step 7: End the program.

PROGRAM:#include<iostream.h>namespace one{int a=100;}namespace two{float a=98.456;}void main(0{using namespace two;cout<<a<<end<<(a/2)<<endl;}

OUTPUT98.45649.228

RESULT:Thus the program for namespace was executed successfully in C++ language.

24

Page 25: OOPS Lab Manual

EX.NO:13 FRIEND FUNCTIONDATE:

AIM To write a C++ program to implement the concept of friend function.

ALGORITHM:Step 1: Start the program.Step 2: Include all the header files.Step 3: A class called sample is created and the variables declared.Step 4: A function called set value is created to set the values of the declared variables.Step 5: A friend function is created to the class.Step 6: Then the function is defined out of the class.Step 7: Object is created in the main class and the function is called through it.Step 8: The output is displayed.Step 9: End of the program.

PROGRAM:#include<iostream.h>#include<conio.h>class sample{int a;int b;public:void setvalue(){a=25;b=40;}friend float mean(sample s)};float mean(sample s){return float(s.a+s.b)/2;}int main(){clrscr();sample x;x.setvalue();cout<<"mean value="<<mean(x)<<"\n";return(0);getch();}

25

Page 26: OOPS Lab Manual

OUTPUTmean value=32.5

RESULT:Thus the program for the concept of friend function using C++ was implemented.

26

Page 27: OOPS Lab Manual

EX NO:14 SINGLE INHERITANCEDATE:

AIM:

To write a program to find out the payroll system using single inheritance.

ALGORITHM:Step 1: Start the program.Step 2: Declare the base class emp.Step 3: Define and declare the function get() to get the employee details.Step 4: Declare the derived class salary.Step 5: Declare and define the function get1() to get the salary details.Step 6: Define the function calculate() to find the net pay.Step 7: Define the function display().Step 8: Create the derived class object.Step 9: Read the number of employees.Step 10: Call the function get(),get1() and calculate() to each employees.Step 11: Call the display().Step 12: Stop the program.

PROGRAM:

#include<iostream.h>#include<conio.h> class emp{   public:     int eno;     char name[20],des[20];     void get()     {              cout<<"Enter the employee number:";              cin>>eno;              cout<<"Enter the employee name:";              cin>>name;              cout<<"Enter the designation:";              cin>>des;     }}; class salary:public emp{     float bp,hra,da,pf,np;

27

Page 28: OOPS Lab Manual

   public:     void get1()     {                           cout<<"Enter the basic pay:";              cin>>bp;              cout<<"Enter the Humen Resource Allowance:";              cin>>hra;              cout<<"Enter the Dearness Allowance :";              cin>>da;              cout<<"Enter the Profitablity Fund:";              cin>>pf;     }     void calculate()     {              np=bp+hra+da-pf;     }     void display()     {              cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\t"<<np<<"\n";     }}; void main(){    int i,n;    char ch;    salary s[10];    clrscr();    cout<<"Enter the number of employee:";    cin>>n;    for(i=0;i<n;i++)    {              s[i].get();              s[i].get1();              s[i].calculate();    }    cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";    for(i=0;i<n;i++)    {              s[i].display();    }    getch();}

OUTPUT:

28

Page 29: OOPS Lab Manual

Enter the number of employee:02Enter the employee number:034Enter the employee name:kanEnter the designation:managerEnter the basic pay:1200Enter the Humen Resource Allowance:123Enter the Dearness Allowance :34Enter the Profitablity Fund:455Enter the employee number:035Enter the employee name:maniEnter the designation:asstEnter the basic pay:1100Enter the Humen Resource Allowance:123Enter the Dearness Allowance :34Enter the Profitablity Fund:455

e_no e_name des bp hra da pf np28 kan manager 1200 123 34 455 90229 mani asst 1100 123 34 455 802

RESULT:Thus the program for payroll system using single inheritance was executed successfully using

C++ language.EX NO:15 MULTIPLE INHERITANCE

29

Page 30: OOPS Lab Manual

DATE:

AIM: To write a C++ program to implement the concept of multiple inheritance.

ALGORITHM:Step 1: Start the programStep 2: Include all the header files.Step 3: Create a class named vehicle and declare all the variables in protected mode.Step 4: The name and the cost of the vehicle is taken in through a seperate function and the inputs are made to display in a seperate function.Step 5: Then a class called vehicle1 is created and all the variables are declared in the protected mode.Step 6: The name and cost of the second vehicle is taken in through a seperate function and the inputs are made to display in the seperate function.Step 7: Then the derived class difference is created by inheriting from the above mentioned classes.Step 8: The difference of the cost is calculated in this class and made to display.Step 9: An object is created in the main class so as to access the functions.Step 10:End the program.

PROGRAM:#include<iostream.h>#include<conio.h>class vehicle{protected:char name[20];float a;public:void getname(){cout<<"\nenter the name of the vehicle:";cin>>name;}void putname(){cout<<"\nthe name of the vehicle is:"<<name;}void getcost(){cout<<"\nenter the cost of the vehicle:";cin>>a;}void putcost(){cout<<"\nthe cost of the vehicle is:"<<a;

30

Page 31: OOPS Lab Manual

}};class vehicle1{protected:char name1[20];float b;public:void getname1(){cout<<"\nenter the name of the vehicle1:";cin>>name1;}void putname1(){cout<<"\nthe name of the vehicle1 is:"<<name1;}void getvalue(){cout<<"\nenter the cost of the vehicle1:";cin>>b;}void putvalue(){cout<<"\nthe cost of the vehicle1 is:"<<b;}};class difference:public vehicle,public vehicle1{protected:float diff;public:void display(){diff=a-b;putname();putcost();putname1();putvalue();cout<<"\nthe difference is:"<<diff;}};void main(){clrscr();difference d;

31

Page 32: OOPS Lab Manual

d.getname();d.getcost();d.getname1();d.getvalue();d.display();getch();}

OUTPUTenter the name of the vehicle:car

enter the cost of the vehicle:100000

enter the name of the vehicle1:jeep

enter the cost of the vehicle1:45000

the name of the vehicle is:carthe cost of the vehicle is:100000the name of the vehicle1 is:jeepthe cost of the vehicle1 is:45000the difference is:55000

RESULT:Thus the program for multiple heritance was executed using C++ language.

EX NO:16 RUNTIME POLYMORPHISM

32

Page 33: OOPS Lab Manual

DATE:

AIM:To write the C++ program to implement the concept of Run time Polymorphism.

ALGORITHM:Step1: Start.Step2: Define a Template T.Step3: Define a sqr() using the template T.Step4: Get the integer value and call the sqr().Step5: Get the float value and call the sqr().Step6: Get the double value and call the sqr().Step7: Stop.

PROGRAM:#include<iostream.h>#include<conio.h>template<class T>T sqr(T & n){return(n*n);}void main(){int a;float b;double c;clrscr();cout<<"\n\n Enter an Integer : ";cin>>a;cout<<"\n Square of a = "<<sqr(a)<<endl;cout<<"\n\n Enter a Float Value : ";cin>>b;cout<<"\n Square of b = "<<sqr(b)<<endl;cout<<"\n\n Enter a Double Value : ";cin>>c;cout<<"\n Square of c = "<<sqr(c);getch();}

OUTPUT:Enter an Integer : 5 Square of a = 25

33

Page 34: OOPS Lab Manual

Enter a Float Value : 3.3 Square of b = 10.89

Enter a Double Value : 5.25 Square of c = 27.5625

RESULT:Thus the program for Run time polymorphism was implemented in C++ language.

EX.NO.17 A. BASIC TYPE INTO CLASS TYPE

34

Page 35: OOPS Lab Manual

DATE:

AIM:To write a C++ program to convert centimeter to meter using basic to class type.

ALGORITHM:Step 1: Create a class as length.Step 2: Declare the data member as m.Step 3: Create a default constructor to initialize the data member to zero.Step 4: Create a constructor with one argument and make the conversion from centimeter to meter.Step 5: In the main, create an object for class length.Step 6: Declare the float variable and get its value.Step 7: Assign the float variable to the object so that the conversion part is called.Step 8: Display the result using print() member function.

PROGRAM:

#include<iostream.h>#include<conio.h>

class length{ float m;public: length() { m=0.0; } length(float a) { m=a/100; } void print() { cout<<"\nTHE METER IS:"<<m; }};

void main(){ clrscr();

length l; cout<<"ENTER THE CENTIMETER:"; float k; cin>>k;

35

Page 36: OOPS Lab Manual

l=k; l.print(); getch();}

OUTPUT:ENTER THE CENTIMETER:700

THE METER IS:7

RESULT:Thus the program was executed successfully.

EX.NO.18 B.CLASS TYPE INTO BASIC TYPE.

36

Page 37: OOPS Lab Manual

DATE:

AIM:To write a C++ program to convert meter to centimeter using class to basic type.

ALGORITHM:Step 1: Create a class as length.Step 2: Declare the data member as m.Step 3: Declare the member function as

length()void read()operator float()

Step 3: Create a default constructor [i.e. length ()] to initialize the data member to zero.Step 4: read() method is used to get the meter value.Step 5: operator float() method is used to convert meter to centimeter.Step 6: In the main, create an object for the class length.Step 7: Call the read() method.Step 8: Assign the object to the variable so that the conversion part is called.Step 9: Display the result.

PROGRAM:

#include<iostream.h>#include<conio.h>

class length{

float m;public: length() { m=0.0; }

void getdata() {cout<<”Enter the meter:”;cin>>m; }

operator float( ) { float c;

c=m*100;return(c);

}};

void main()

37

Page 38: OOPS Lab Manual

{ clrscr();

length l; l.getdata();

float f; f=l; cout<<”The Centimeter is”<<f; getch();}

OUTPUT:Enter the meter:7

The Centimeter is:700

RESULT:Thus the program was executed successfully.

EX.NO.19 C. CLASS TYPE INTO CLASS TYPE.

38

Page 39: OOPS Lab Manual

DATE:

AIM:To write a C++ program to convert degree to radian using class to class type.

ALGORITHM:Step 1: Create a class as degree.

1.1 Declare the data member as d.1.2 Initialize the d variable to zero using default constructor.1.3 read() method is used to get the degree value.1.4 getdegree() method is used to return the degree value.

Step 2: Create a class as radian.2.1 Declare the data member as r.2.2 Initialize the r variable to zero using default constructor.2.3 Specify the constructor with degree as argument to perform the conversion.2.4 print() method is used to display the radian value.

Step 3: In the main, create objects for the class degree and radian.Step 4: Assign the object of degree class to the object of radian class to call the

conversion part.Step 5: Display the value.

PROGRAM:

#include<iostream.h>#include<conio.h>

class degree{ float d; public: degree() {

d=0.0; } void read() {

cout<<"\n ENTER THE DEGREE:";cin>>d;

} float getdegree() {

return d; }};

class radian{

39

Page 40: OOPS Lab Manual

float r; public: radian() {

r=0.0; } radian(degree d) {

r=d.getdegree()*3.14/180; } void print() {

cout<<"\nTHE RADIAN IS:"<<r; }};

void main(){ clrscr(); radian rr; degree dd; dd.read(); rr=dd; rr.print();

getch();}

OUTPUT:ENTER THE DEGREE:67

THE RADIAN IS:1.168778

RESULT:Thus the program was executed successfully.

40

Page 41: OOPS Lab Manual

EX.NO.20 FUNCTION TEMPLATEDATE:

AIM:To write a C++ program to swap two variables using function template.

ALGORITHM:Step 1: Declare the template to perform swap operation.Step 2: In the main, get the values for integer data type 2.1 Call the swap function.

2.2 Display the swapped values.Step 3: Get the values for float data type 3.1 Call the swap function.

3.2 Display the swapped values.Step 4: Get the values for character data type 4.1 Call the swap function.

4.2 Display the swapped values.

PROGRAM:

#include<iostream.h>#include<conio.h>

template<class T>void swap(T &x,T &y){ T t; t=x;

x=y;y=t;

}

void main(){ clrscr();

int a,b; cout<<"\nENTER THE INTEGER VALUES:\n"; cin>>a>>b; swap(a,b); cout<<"\nTHE SWAPPED VALUES ARE:"<<a<<"\t"<<b;

float c,d; cout<<"\n\nENTER THE FLOAT VALUES:\n"; cin>>c>>d; swap(c,d); cout<<"\n THE SWAPPED VALUES ARE:"<<c<<"\t"<<d;

41

Page 42: OOPS Lab Manual

char ch1,ch2; cout<<"\n\nENTER THE CHARACTER VALUES:\n"; cin>>ch1>>ch2; swap(ch1,ch2); cout<<"\n THE SWAPPED VALUES ARE:"<<ch1<<"\t"<<ch2; getch();}

OUTPUT:ENTER THE INTEGER VALUES: 67 12

THE SWAPPED VALUES ARE: 12 67

ENTER THE FLOAT VALUES: 8.9 1.2

THE SWAPPED VALUES ARE: 1.2 8.9

ENTER THE CHARACTER VALUES: r a

THE SWAPPED VALUES ARE: a r

RESULT:Thus the program was executed successfully.

EX.NO.21 SINGLE INHERITANCE

42

Page 43: OOPS Lab Manual

DATE:

AIM:To write a C++ program to display student details using single inheritance.

ALGORITHM:Step 1: Create a base class as student.1.1 Declare the data members as rollno and name[25].1.2 Declare the member functions as

void get()void put()

1.3 get() function is used to get the rollno and name.1.4 put function is used to display the rollno and name.

Step 2: Create a derived class as person.2.1 Declare the data members as dept[10] and college[10].2.2 Declare the member functions as

void get1()void put1()

2.3 get1() function is used to get the dept and college.2.4 put1() function is used to display the dept and college.

Step 3: In the main, create the object for the derived class person.Step 4.Call the functions.Step 5: Display the results.

PROGRAM:

#include<iostream.h>#include<conio.h>

class student{protected:

int roll;public:

void get(int r){roll=r;}void put(){cout<<"\nRoll :"<<roll;}

};

class test:virtual public student{protected:

43

Page 44: OOPS Lab Manual

int mark1,mark2;public:

void get1(int y,int z){mark1=y;mark2=z;}void put1(){cout<<"\nMark1 :"<<mark1;cout<<"\nMark2 :"<<mark2;}

};void main(){

clrscr();test R;R.get(101);R.get1(97,99);cout<<"\nSTUDENT DETAIL:\n";R.put();R.put1();getch();

}

OUTPUT:

STUDENT DETAIL:

Roll:101Mark1:97Mark2:99

RESULT:Thus the program was executed successfully.

EX.NO.22 VIRTUAL BASE CLASS

44

Page 45: OOPS Lab Manual

DATE:

AIM:To write a C++ program to display student details using virtual base class.

ALGORITHM:

Step 1: Create a base class as student.

1.1 Declare the data members as rollno.

1.2 Declare the member functions as

void get(int r)

void put()

1.3 get(int r) function is used to assign the r value to rollno.

1.4 put() function is used to display the rollno.

Step 2: Create a derived class test with virtual keyword. (Base class – student)

2.1 Declare the data members mark1 and mark2.

2.2 Declare the member functions as

void get1(int y,int z)

void put1()

2.3 get1(int y,int z) function is used to assign the value of y and z to mark1 and

mark2..

2.4 put1() function is used to display the department and college name.

Step 3: Create a derived class as sport with virtual keyword. (Base class – student)

3.1 Declare the data member score.

3.2 Declare the member functions as

void get2(int s)

void put2()

3.3 get2(int s) function is used to assign the value of s to score.

3.4 put2() function is used to display the value of score.

Step 4: Create a derived class as result. (Base classes – test and sport)

3.1 Declare the data member total.

3.2 Declare the member function as

void put3()

3.3 put3() function is used to display the total value.

45

Page 46: OOPS Lab Manual

Step 5: In the main, create the object for the derived class result.

Step 6.Call the functions.

Step 7: Display the results.

PROGRAM:

#include<iostream.h>#include<conio.h>

class student{protected:

int roll;public:

void get(int r){roll=r;}void put(){cout<<"\nRoll:"<<roll;}

};

class test:virtual public student{protected:

int mark1,mark2;public:

void get1(int y,int z){mark1=y;mark2=z;}

void put1(){

cout<<"\nMark1:"<<mark1;cout<<"\nMark2:"<<mark2;}

};

class sport:virtual public student{protected:

int score;

46

Page 47: OOPS Lab Manual

public:void get2(int s){score=s;}void put2(){cout<<"\nScore:"<<score;}

};

class result:public test,public sport{

int total;public:

void put3(){total=mark1+mark2+score;put();put1();put2();cout<<"\nTotal:"<<total;}

};

void main(){

clrscr();result R;R.get(101);R.get1(97,99);R.get2(5);cout<<"\nSTUDENT DETAIL:\n";R.put3();getch();

}

OUTPUT:STUDENT DETAIL:

Roll:101Mark1:97Mark2:99Score:5Total:201

47

Page 48: OOPS Lab Manual

RESULT:Thus the program was executed successfully.

EX.NO: 23 STRING CONCATENATION USIND DYNAMIC MEMORY ALLOCATIONDATE:

48

Page 49: OOPS Lab Manual

AIM: To implement the string concatenation function by using dynamic memory allocation

concept. ALGORITHM:Step 1: Start the programStep 2: Create class STRING with two constructors. The first is an empty constructor, which allows declaring an array of strings. The second constructor initializes the length of the strings, and allocates necessary space for the string to be stored and creates the string itself.Step 3: Create a member function to concatenate two strings.Step 4: Estimate the combined length of the strings to be joined and allocates memory for the combined string using new operator and then creates the same using the string functions strcpy() and strcat().Step 5: Display the concatenated string.Step 6: Stop the program PROGRAM:#include<iostream.h>#include<string.h>#include<conio.h>

class string{char *name;int length;public:string(){length=0;name=new char[length+1];}

string(char*s){length=strlen(s);name=new char[length+1];strcpy(name,s);}

void display(void){cout<<name<<"\n";}void join(string &a,string &b);};

49

Page 50: OOPS Lab Manual

void string::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);};

void main(){clrscr();string name1 ("Object ");string name2 ("Oriented ");string name3 ("Programming Lab");string s1,s2;s1.join(name1,name2);s2.join(s1,name3);name1.display();name2.display();name3.display();s1.display();s2.display();getch();}

OUTPUT:ObjectOrientedProgramming LabObject OrientedObject Oriented Programming Lab

RESULT: Thus the program for string concatenation using dynamic memory allocation was

implemented.EX.NO.24 HIERARCHAL INHERITANCE WITH VIRTUAL FUNCTION AND RTTIDATE:

50

Page 51: OOPS Lab Manual

AIM:

To write a C++ program to draw rectangle, square and circle using multiple inheritance with virtual

function.

ALGORITHM:Step 1: Create a class Point.1.1 Declare the data members x and y.1.2 Declare the member functions as

Point(int tempx,int tempy)int getx()int gety()

1.3 getx() function is used to return the value of x .1.4 gety() function is used to return the value of y.Step 2: Create a base class shape.2.1 Declare the necessary data members.2.2 Declare the member function as

virtual void draw()Step 3: Create a derived class square. (Base class – shape)

3.1 Create an abstract class.3.2 Get the sides of square.3.3 Draw the square using draw() function.

Step 4: Create a derived class rectangle. (Base class – shape)3.1 Create an abstract class.3.2 Get the length and breadth of rectangle.3.3 Draw the rectangle using draw() function.

Step 5: Create a derived class circle. (Base class – shape)3.1 Create an abstract class.3.2 Get the radius of circle.3.3 Draw the circle using draw() function.

Step 6: In the main,6.1 Create the objects for point class.6.2 Create a base pointer object for shape class.6.3 Call the draw() function to draw the square, rectangle and circle.

PROGRAM:

#include<iostream.h>#include<conio.h>#include<graphics.h>

class Point{

public:int x;int y;Point(){}

51

Page 52: OOPS Lab Manual

Point(int tempX, int tempY){

x=tempX;y=tempY;

}int GetX(){

return x;}

int GetY(){

return y;}friend ostream & operator<<(ostream & tempout, Point & tempPoint){

tempout<<"("<<tempPoint.GetX()<<","<<tempPoint.GetY()<<")";return tempout;

}};

class Shape{

Point Position;public:

Shape(){}virtual void draw(){

cout<<"shape is drawn";}

};

class Square : public Shape{

Point LeftBottom;int Length;

public:Square(){}Square(Point tLeftBottom, int tLength){

LeftBottom=tLeftBottom;Length=tLength;

}void draw(){

cout<<"Square is drwan at"<<LeftBottom<<"and with length as"<<Length<<"\n";

52

Page 53: OOPS Lab Manual

setcolor(14);rectangle(LeftBottom.GetX(),LeftBottom.GetY(),LeftBottom.GetX()

+Length,LeftBottom.GetY()+Length);}

};class Rectangles : public Shape{

Point LeftBottom, LeftTop, RightBottom, RightTop;public:

Rectangles(){}Rectangles(Point tLeftBottom, Point tLeftTop, Point tRightBottom, Point tRightTop){

LeftBottom=tLeftBottom;LeftTop=tLeftTop;RightBottom=tRightBottom;RightTop=tRightTop;

}void draw(){

cout<<"Rectangle is drwan at("<<LeftBottom<<","<<RightBottom<<")"<<"and"<<")"<<LeftTop<<","<<RightTop<<")\n";

setcolor(4);

rectangle(LeftBottom.GetX(),LeftBottom.GetY(),RightTop.GetX(),RightTop.GetY());}

};class Circle : public Shape{

Point Center;int Radius;

public:Circle(){}Circle(Point tCenter, int tRadius){

Center=tCenter;Radius=tRadius;

}void draw(){

cout<<"Circle is drawn at"<<" "<<Center<<" "<<"and the radius is"<<Radius<<"\n";setcolor(5);circle(Center.GetX(),Center.GetY(),Radius);

}};int main(){

53

Page 54: OOPS Lab Manual

clrscr();int gdriver = DETECT, gmode, errorcode;initgraph(&gdriver, &gmode, "D:/Tc/BGI");Point p1(100,200);Point p2(50,50);Point p3(100,300);Square sq(p1,50);Rectangles rect(p1,p2,p1,p2);Circle c(p3,50);Shape*s;s=&sq;s->draw();getch();s=&rect;s->draw();getch();s=&c; s->draw();getch();return 0;

}

OUTPUT:Square is drawn at (100,200) and with length as 50Rectangle is drawn at ((100,200), (100,200) ) and ( (50,50), (50,50) )Circle is drawn at (100,300) and the radius is 50

RESULT:Thus the program was executed successfully.

EX NO:25 EXCEPTION HANDLINGDATE:

54

Page 55: OOPS Lab Manual

AIM:To write a C++ program to implement the concept of Exception handling.

ALGORITHM:Step1: Start.Step2: Create a class MyException with the divide() Step3: If x is 0 then the exception is thrown then the catch block displays the message “division by zero is not possible”Step4: Stop.

PROGRAM:#include<iostream.h>#include<conio.h>class MyException{int n;public:MyException(int a){n=a;}int Divide(int x){try{if(x==0)throw x;cout<<"This statement will not be executed\n";else{cout<<"Trying division suceede\n";cout<<n/x;}}catch(int){cout<<"Exception : Division by zero. Check the value";}return 0;}};void main(){MyException ex(10);ex.Divide(0);}

55

Page 56: OOPS Lab Manual

OUTPUT:This statement will not be executedException : Division by zero. Check the value

RESULT:Thus the program for divide by zero exception is performed using C++ language.EX NO:26 EXCEPTION HANDLINGDATE:

AIM:To perform a simple exception handling for Divide by zero Exception.

56

Page 57: OOPS Lab Manual

ALGORITHM:Step 1: Start the program.Step 2: Declare the variables a,b,c.Step 3: Read the values a,b,c,.Step 4: Inside the try block check the condition.a. if(a-b!=0) then calculate the value of d and display.b. otherwise throw the exception.Step 5: Catch the exception and display the appropriate message.Step 6: Stop the program.

PROGRAM:#include<iostream>using namespace std;int main(){int a,b,c;float d;cout<<"Enter the value of a:";cin>>a;cout<<"Enter the value of b:";cin>>b;cout<<"Enter the value of c:";cin>>c;try{if((a-b)!=0){d=c/(a-b);cout<<"Result is:"<<d;}else{throw(a-b);}}catch(int i){cout<<"Answer is infinite because a-b is:"<<i;}return 0; }OUTPUT:Enter the value for a: 20Enter the value for b: 20Enter the value for c: 40Answer is infinite because a-b is: 0

57

Page 58: OOPS Lab Manual

RESULT:Thus the program for divide by zero exception is performed using C++ language.

EX.NO.27 A. READ THE CONTENT FROM THE FILE.DATE:

AIM:To write a C++ program to read the contents from a file.

58

Page 59: OOPS Lab Manual

ALGORITHM:Step 1: Include the header file as fstream.hStep 2: Declare the variables as rollno, dept, name and year.Step 3: Declare the filename “stu.txt” using ifstream.Step 4: Read the values of rollno, dept, name and year from the file.Step 5: Display the values of rollno, dept, name and year.

PROGRAM:

#include<fstream.h>void main(){

char dept[20];char name[20];char year[10];int roll;ifstream inf("stu.txt");inf>>roll;inf>>dept;inf>>name;inf>>year;cout<<"\nRoll:"<<roll;cout<<"\nName:"<<name;cout<<"\nYear:"<<year;cout<<"\nDept:"<<dept;

}

OUTPUT:Roll: 101Name:RAMYear:IIDept:IT

RESULT:Thus the program was executed successfully.

EX.NO.28 B. WRITE INTO THE FILE.DATE:

AIM:

To write a C++ program to write the contents into a file.

59

Page 60: OOPS Lab Manual

ALGORITHM:Step 1: Include the header file as fstream.hStep 2: Declare the variables as rollno, dept, name and year.Step 3: Create a filename “stu.txt” using ofstream.Step 4: Read the values for rollno, dept, name and year.Step 5: Write the values of rollno, dept, name and year into the file.

PROGRAM:

#include<fstream.h>

void main(){

int roll;char dept[10];char name[45];char year[20];ofstream out("stu.txt");cout<<"enter the roll:";cin>>roll;cout<<"Enter the name:";cin>>name;cout<<"Enter the year:";cin>>year;cout<<"enter the dept:";cin>>dept;out<<roll;out<<name;out<<year;out<<dept;

}

OUTPUT:

STU.TXT101RAMIIIT

60

Page 61: OOPS Lab Manual

RESULT:Thus the program was executed successfully.

EX NO:29 PROGRAM DEVELOPMENT USING STLDATE:

AIM: To write a C++ program using STL.

61

Page 62: OOPS Lab Manual

ALGORITHM:Step 1: Start the program.Step 2: Include all the header files.Step 3: Include vector header file.Step 4: Create int type empty vector V of zero size.Step 5: In the next step, get the five values in to vector using member function push_back()Step 6: Insertion of function is called.Step 7: Deletion of function is called.Step 8: Output is displayed.Step 9: End the program.

PROGRAM:#include<iostream.h>#include<vector>using namespace std;void display(vector<int>&v){for(int i=0;i<v.size(0;i++){cout<<v[i]<<";}cout<<"\n';}int main(){vector<int>v;cout<<"initial size="<<v.size()<<"\n";int x;cout<<"enter five integer values:";for(int i=0;i<5;i++){cin>>x;v.push_back(x);}cout<<"size after adding 5 values:";cout<<v.size()<<"\n";cout<<"current contents:\n";display(v);v.push_back(6.6);cout<<"\nsize+"<<v.size()<<"\n";cout<<"contents now:\n";display(v);vector<int>::iterator itr=v.begin();itr=ite+3;v.insert(itr,1,9);cout<<"\n contents after inserting:\n";

62

Page 63: OOPS Lab Manual

display(v);v.erase(v.begin()+3,v.begin()+5);cout<<"\n contents after deletion:\n";display(v);cout<<"end\n";return(0);}

OUTPUTinitial size=0Enter five integer values: 1 2 3 4 5size after adding 5 values:5current contents:1 2 3 4 5size=6contents now:1 2 3 4 5 6contents after inserting:1 2 3 4 5 6contents after deletion:1 2 3 4 5 6END

RESULTThus the program for development using STL was executed in C++ language.

63