Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

OOPS LAB ECE

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

cout<<" UID= 14BEC1125\n";


cout<<"\n **********EXECUTION STARTED**********\n\n";
data x;
x.getdata();
x.show();
getch();
return 0;
}

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()

cout<<" We are in base class";

};

class der1:public base

public:

void display()

cout<<"\n We are in der1 class";

};

class der2:public base

Sonu 14BEC1124
OOPS LAB ECE
public:

void display()

cout<<"\n We are in der2 class";

};

void main()

clrscr();

cout<<” Name = Ankit dogra\n”;

cout<<” UID = 14BEC1125\n”:

cout<<”******Execution started*******”;

base *p;

der1 d1;

der2 d2;

p=&d1;

p->display();

p=&d2;

p->display();

getch();

Sonu 14BEC1124
OOPS LAB ECE

OUTPUT

Description: to implement the concept of virtual function

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;

virtual void cal_area()

cout<<" We can calculate the area of different shapes";

};

class rect:public shape

public:

void cal_area()

Sonu 14BEC1124
OOPS LAB ECE
int area;

area=a*b;

cout<<" Area of rectangle="<<area;

};

class tri:public shape

public:

void cal_area()

int area;

area=(a*b)/2;

cout<< " Area of triangle="<<area;

};

void main()

clrscr();

cout<<” Name = Ankit dogra\n”;

cout<<” UID = 14BEC1125”;

cout<<”******Execution Started******\n”;

shape *p;

rect r;

cout<<" Enter the value of length and breath=";

r.read();

Sonu 14BEC1124
OOPS LAB ECE
p=&r;

p->cal_area();

tri t;

cout<<"\n Enter the value of base and perpendicular=";

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;

virtual void cal_area()=0;

};

class rectangle:public shape

public:

void cal_area()

int area=a*b;

cout<<"\n Area of rectangle="<<area;

};

Sonu 14BEC1124
OOPS LAB ECE
class triangle:public shape

public:

void cal_area()

int area=a*b/2;

cout<<"\n Area of triangle="<<area;

};

int main()

clrscr();

cout<<"Name- Ankit dogra"<<endl;

cout<<”UID-14BEC1125"<<endl;

shape *p[2];

rectangle r1;

cout<<"\n Enter length and breadth of rectangle=";

r1.read();

triangle t1;

cout<<"\n Enter base and perpendicular of triangle=";

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

Description : To implement the concept of pure virtual fuction.

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

Description: To implement the use of new and delete operators.

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>

T min(T a,T b,T c)

return(a<b)?(a<c?a:c):(b<c?b:c);

void main()

clrscr();

cout<<"Name-sonu kumar"<<endl;

cout<<” UID-14BEC1124"<<endl;

cout<<"Min of two int values="<<min(10,20);

cout<<"Min of two float values="<<min(2.75,2.5);

cout<<"Min of three int values="<<min(15,5,20);

cout<<"Min of three float values="<<min(3.1,4.1,5.1);

cout<<"Min of three char values="<<min('A','X','D');

getch();

Sonu 14BEC1124
OOPS LAB ECE
return 0;

OUTPUT:

Description: To implement the use of template function in template class.

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();

cout<<"\n Name – sonu kumar"<<endl;

cout<<"\n UID -14BEC1124"<<endl;

int r_no;

char name[20];

float marks;

ofstream out_file("student.txt");

//if(!out_file)

//{

//cerr<<"file cannot open correctly";

//exit(-1);

//}

cout<<"\n Enter student's details\n";

cout<<"\n Enter r_no of student\n";

cin>>r_no;

cout<<"\n Enter name of student\n";

cin>>name;

cout<<"\n Enter marks of student\n";

Sonu 14BEC1124
OOPS LAB ECE
cin>>marks;

cout<<"\n Writing student details into file:";

out_file<<r_no<<endl;

out_file<<name<<endl;

out_file<<marks<<endl;

getch();

return 0;

Output:

Description :implementing concept of file.

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)

cerr<<"File cannot open correctly";

exit(-1);

cout<<"Reading student details from a file\n";

inp_file>>r_no;

inp_file>>name;

inp_file>>marks;

cout<<"Content of files are=";

Sonu 14BEC1124
OOPS LAB ECE
cout<<"r_no="<<r_no;

cout<<"name="<<name;

cout<<"marks="<<marks;

getch();

return 0;

OUTPUT:

Description: implementing reading data into a file.

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<<”Name- sonu kumar”;

cout<<”UID-14BEC1124”

ofstream out;

char data[25];

out.open("text",ios::out);

cout<<"\n Enter text"<<endl;

cin.getline(data,25);

out<<data;

out.close();

out.open("text",ios::app);

cout<<"\n Again Enter text"<<endl;

cin.getline(data,25);

out<<data;

out.close();

ifstream in;

in.open("text",ios::in);

cout<<endl<<"contents of the file \n";

Sonu 14BEC1124
OOPS LAB ECE
while(in.eof()==0)

in>>data;

cout<<data;

getch();

return 0;

OUTPUT:

Description: implementing concept of file to pointer.

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;

static int tot_marks;

public:

void read()

cout<<"\n Enter roll no. = ";

cin>>rollno;

cout<<" Enter marks = ";

cin>>marks;

void cal()

tot_marks+=marks;

static void avg_per_marks (int n1)

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();

cout<<"\n\t\t Name- sonu kumar "<<endl;

cout<<"\t\t UID-14BEC1124 "<<endl;

student s[50];

int n;

cout<<"\n----| Enter no. of students = ";

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:

Description: implementing concept of static data member.

Sonu 14BEC1124

You might also like