Professional Documents
Culture Documents
Oops Lab ECE: 38.WAP To Implement Use of Multiple Inheritance
Oops Lab ECE: 38.WAP To Implement Use of Multiple Inheritance
PROGRAM NO-38
38.WAP to implement use of multiple inheritance
#include<iostream.h>
#include<conio.h>
class base1
{
protected:
int x;
public:
void readx()
{
cout<<"\n Enter the value of x = ";
cin>>x;
}
void showx()
{
cout<<"\n x= "<<x;
}
};
class base2
{
protected:
int y;
public:
void ready()
{
cout<<" Enter the value of y = ";
cin>>y;
}
void showy()
{
cout<<"\n y= "<<y;
}
};
class der:public base1,public base2
{
private:
int z;
public:
void add()
{
z=x+y;
}
void showz()
Sonu 14BEC1124
OOPS LAB ECE
{
cout<<"\n z= "<<z;
}
};
void main()
{
clrscr();
der d;
cout<<"\n ************Name = Ankit dogra************";
cout<<" \n ************UID= 14BEC1125************\n";
cout<<"\n* ************Execution Started**********\n";
d.readx();
d.ready();
d.add();
d.showx();
d.showy();
d.showz();
getch();
}
OUTPUT
Sonu 14BEC1124
OOPS LAB ECE
PROGRAM NO -39
39.WAP to read the derive class data members such as name,rollno,sex,age,marks and
display it on the screen. The program should follow single inheritance.
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno,marks,age;
char name[20],sex[8];
};
class data : public student
{
public:
void getdata()
{
cout<<"Enter the student name:";
cin>>name;
cout<<"Enter the roll number:";
cin>>rollno;
cout<<"Enter the sex of the student:";
cin>>sex;
cout<<"Enter the age of the student:";
cin>>age;
cout<<"Enter the marks of the student:";
cin>>marks;
}
void show()
{
cout<<"\n NAME: "<<name;
cout<<"\n ROLLNUMBER: "<<rollno;
cout<<"\n SEX: "<<sex;
cout<<"\n AGE: "<<age;
cout<<"\n MARKS: "<<marks;
}
};
int main()
{
clrscr();
cout<<"\n Name= Ankit dogra\n";
Sonu 14BEC1124
OOPS LAB ECE
Sonu 14BEC1124
OOPS LAB ECE
Program No-40
40.WAP having three classes person, student and exam. T he person class is
the base class, student class is derived from Person and Exam class is derived
from Student.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student // base class
{
private:
int rl;
char nm[20];
public:
void read();
void display();
};
class marks : public student // derive from student
{
protected:
int s1;
int s2;
int s3;
public:
void getmarks();
void putmarks();
};
class result : public marks // derived from marks
{
private:
int t;
float p;
char div[10];
public:
void process();
void printresult();
};
void student::read()
{
cout<<"Enter NAME: ";
Sonu 14BEC1124
OOPS LAB ECE
cin>>nm;
cout<<"Enter ROLL NUMBER: ";
cin>>rl;
}
void student:: display()
{
cout <<"Roll NO:"<<rl<<endl;
cout<<"NAME: "<<nm<<endl;
}
void marks ::getmarks()
{
cout<<"ENTER MARKS OF FIRST SUBJECTS: ";
cin>>s1;
cout<<"ENTER MARKS OF SECOND SUBJECTS: ";
cin>>s2;
cout<<"ENTER MARKS OF THIRD SUBJECTS: ";
cin>>s3;
}
void marks ::putmarks()
{
cout<<"Subject 1:"<<s1<<endl;
cout<<"Subject 2:"<<s2<<endl;
cout<<"Subject 3:"<<s3<<endl;
}
void result::process()
{
t= s1+s2+s3;
p = t/3.0;
p>=60?strcpy(div,"FIRST"):p>=50?strcpy(div,"SECOND"): strcpy(div,"THIRD");
}
void result::printresult()
{
cout<<"TOTAL = "<<t<<endl;
cout<<"PERCENTAGE= "<<p<<endl;
cout<<"DIVISION= "<<div<<endl;
}
void main()
{
result x;
clrscr();
cout<<"\n Name= Ankit dogra\n";
cout<<" UID= 14BEC1125\n";
x.read();
Sonu 14BEC1124
OOPS LAB ECE
x.getmarks();
x.process();
x.display();
x.putmarks();
x.printresult();
getch();
}
OUTPUT
Description: Three classes person, student and exam. The person class is the base class, student class is
derived from Person and Exam class is derived from Student.
Sonu 14BEC1124
OOPS LAB ECE
Program No-41
41.WAP THAT SHOW ORDER OF EXECUTION OF BASE AND
DERIVE CLASS CONSTRUCTORS AND DISTRUCTORS USING
INHERITANCE.
#include<iostream.h>
#include<conio.h>
class A1
protected:
char name[15];
int age;
A1()
cout<<"\nName:";
cin>>name;
cout<<"age:";
cin>>age;
~A1()
cout<<"\nName:"<<name;
cout<<"\nage:"<<age;
};
class A2:public A1
Sonu 14BEC1124
OOPS LAB ECE
{
protected:
float height;
float weight;
A2()
OUTPUT
Sonu 14BEC1124
OOPS LAB ECE
Program No – 42
42.WAP to display the concept of virtual function.
#include<iostream.h>
#include<conio.h>
class base
public:
virtual
void display()
};
public:
void display()
};
Sonu 14BEC1124
OOPS LAB ECE
public:
void display()
};
void main()
clrscr();
cout<<”******Execution started*******”;
base *p;
der1 d1;
der2 d2;
p=&d1;
p->display();
p=&d2;
p->display();
getch();
Sonu 14BEC1124
OOPS LAB ECE
OUTPUT
Sonu 14BEC1124
OOPS LAB ECE
Program No -43
43. WAP to implement concept of virtual function of base class shape,derive
classes are rectangle and triangle.
#include<iostream.h>
#include<conio.h>
class shape
protected:
int a,b;
public:
void read()
cin>>a;
cin>>b;
};
public:
void cal_area()
Sonu 14BEC1124
OOPS LAB ECE
int area;
area=a*b;
};
public:
void cal_area()
int area;
area=(a*b)/2;
};
void main()
clrscr();
cout<<”******Execution Started******\n”;
shape *p;
rect r;
r.read();
Sonu 14BEC1124
OOPS LAB ECE
p=&r;
p->cal_area();
tri t;
t.read();
p=&t;
p->cal_area();
getch();
Output
Description: implement concept of virtual function in base classis shape and derived classes
are rectangle and triangle
Sonu 14BEC1124
OOPS LAB ECE
Program No-44
44.WAP to implement concept of pure virtual function.
#include<iostream.h>
#include<conio.h>
class shape
protected:
int a,b;
public:
void read()
cin>>a>>b;
};
public:
void cal_area()
int area=a*b;
};
Sonu 14BEC1124
OOPS LAB ECE
class triangle:public shape
public:
void cal_area()
int area=a*b/2;
};
int main()
clrscr();
cout<<”UID-14BEC1125"<<endl;
shape *p[2];
rectangle r1;
r1.read();
triangle t1;
t1.read();
p[0]=&r1;
p[1]=&t1;
for(int i=0;i<2;i++)
p[i]->cal_area();
getch();
Sonu 14BEC1124
OOPS LAB ECE
return 0;
OUTPUT
Sonu 14BEC1124
OOPS LAB ECE
Program No-45
45.WAP to demonstrate the use new and delete operators.
#include<iostream.h>
#include<conio.h>
class rectangle
{
private:
int l,b;
public:
rectangle()
{
cout<<"Constructor with no parameter invoked"<<endl;
}
void read()
{
cout<<"Enter length and breadth="<<endl;
cin>>l>>b;
}
void area()
{
cout<<"Area of rectangle="<<l*b<<endl;
}
~rectangle()
{
cout<<"Destructor invoked"<<endl;
}
};//end of class
int main()
Sonu 14BEC1124
OOPS LAB ECE
{
clrscr();
cout<<"Name= Ankit dogra"<<endl;
cout<<"UID=14BEC1125"<<endl;
rectangle*ptr;
ptr=new rectangle;
ptr->read();
ptr->area();
cout<<"Destroying object"<<endl;
delete ptr;
cout<<"End of program"<<endl;
getch();
return 0;
}
OUTPUT
Sonu 14BEC1124
OOPS LAB ECE
Program No-47
47.WAP to demonstrate the use of template function in template class.
#include<iostream.h>
#include<conio.h>
template<class T>
T min(T a,T b)
return(a<b)?a:b;
template<class T>
return(a<b)?(a<c?a:c):(b<c?b:c);
void main()
clrscr();
cout<<"Name-sonu kumar"<<endl;
cout<<” UID-14BEC1124"<<endl;
getch();
Sonu 14BEC1124
OOPS LAB ECE
return 0;
OUTPUT:
Sonu 14BEC1124
OOPS LAB ECE
Program No-48
48.WAP to create a file”student.txt” and store students information into it using
insertion operator(<<).
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
int main()
clrscr();
int r_no;
char name[20];
float marks;
ofstream out_file("student.txt");
//if(!out_file)
//{
//exit(-1);
//}
cin>>r_no;
cin>>name;
Sonu 14BEC1124
OOPS LAB ECE
cin>>marks;
out_file<<r_no<<endl;
out_file<<name<<endl;
out_file<<marks<<endl;
getch();
return 0;
Output:
Sonu 14BEC1124
OOPS LAB ECE
Program No-49
49. WAP to read the contents of the file student.txt using extraction operator.
#include<fstream.h>
#include<conio.h>
#include<process.h>
int main()
clrscr();
cout<<”Name-sonu kumar”;
cout<<”UID-14BEC1124”;
int r_no;
char name[20];
float marks;
ifstream inp_file("student.txt");
if(!inp_file)
exit(-1);
inp_file>>r_no;
inp_file>>name;
inp_file>>marks;
Sonu 14BEC1124
OOPS LAB ECE
cout<<"r_no="<<r_no;
cout<<"name="<<name;
cout<<"marks="<<marks;
getch();
return 0;
OUTPUT:
Sonu 14BEC1124
OOPS LAB ECE
Program No-50
50.WAP to implement the concept of file to pointer
#include<fstream.h>
#include<conio.h>
int main()
clrscr();
cout<<”UID-14BEC1124”
ofstream out;
char data[25];
out.open("text",ios::out);
cin.getline(data,25);
out<<data;
out.close();
out.open("text",ios::app);
cin.getline(data,25);
out<<data;
out.close();
ifstream in;
in.open("text",ios::in);
Sonu 14BEC1124
OOPS LAB ECE
while(in.eof()==0)
in>>data;
cout<<data;
getch();
return 0;
OUTPUT:
Sonu 14BEC1124
OOPS LAB ECE
Program No-51
51.WAP to implement concept of static data members.
#include<iostream.h>
#include<conio.h>
class student
private:
int rollno,marks;
public:
void read()
cin>>rollno;
cin>>marks;
void cal()
tot_marks+=marks;
double avg=double(tot_marks)/n1;
Sonu 14BEC1124
OOPS LAB ECE
cout<<"\n Average percentage of marks is = "<<avg;
};
int student::tot_marks;
int main()
clrscr();
student s[50];
int n;
cin>>n;
for(int i=0;i<n;i++)
s[i].read();
s[i].cal();
student::avg_per_marks(n);
getch();
return 0;
Sonu 14BEC1124
OOPS LAB ECE
OUTPUT:
Sonu 14BEC1124