Virtual Base Classes..
Virtual Base Classes:
Program: Write a program to demonstrate the use of Virtual Base Class
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void get_number(int a)
{
rollno = a;
}
void put_number(void)
{
cout<<"Roll
No:"<<rollno<<endl;
}
};
class test:virtual public student
{
protected:
float part1,part2;
public:
void get_marks(float x, float y)
{
part1=x; part2=y;
}
void put_marks(void)
{
cout<<"Marks
Obtained:"<<"\n"
<<"Part1="<<part1<<"\n"
<<"Part2="<<part2<<"\n";
}
};
class sports:public virtual student
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"Sports
Score:"<<score<<endl;
}
};
class result:public test, public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_number();
put_marks();
put_score();
cout<<"Total Score:
"<<total<<endl;
}
int main()
{
result s;
s.get_number(21);
s.get_marks(30.5,45.5);
s.get_score(8.0);
s.display();
getch();
return 0;
}
Output:
Program: Write a program
to demonstrate the use of Hierarchical Inheritance.
#include<iostream.h>
#include<conio.h>
class students
{
int rollno;
char sname[20];
public:
void getdata();
void putdata();
};
class engineering :
public students
{
protected:
char year[10];
char sem[10];
public:
void gdata();
void pdata();
};
class mech : public
engineering
{
char stream[10];
int subjects;
public:
void idata();
void odata();
};
class civil : public
engineering
{
char stream[10];
int subjects;
public:
void input();
void output();
};
void students::getdata()
{
cout<<" Enter
Roll No:";
cin>>rollno;
cout<<"Enter
Name:";
cin>>sname;
}
void students::putdata()
{
cout<<"Roll
No:"<<rollno<<endl;
cout<<"Name:"<<sname<<endl;
}
void
engineering::gdata()
{
getdata();
cout<<"Enter
year:";
cin>>year;
cout<<"Enter
semister:";
cin>>sem;
}
void
engineering::pdata()
{
putdata();
cout<<"Year:"<<year<<endl;
cout<<"Semister:"<<sem<<endl;
}
void mech::idata()
{
gdata();
cout<<"Enter
stream:";
cin>>stream;
cout<<"Enter
no. of subjects:";
cin>>subjects;
}
void mech::odata()
{
pdata();
cout<<"Stream:"<<stream<<endl;
cout<<"Subjects:"<<subjects<<endl;
}
void civil::input()
{
gdata();
cout<<"Enter
stream:";
cin>>stream;
cout<<"Enter
no. of subjects:";
cin>>subjects;
}
void civil::output()
{
pdata();
cout<<"Stream:"<<stream<<endl;
cout<<"Subjects:"<<subjects<<endl;
}
void main()
{
mech m;
civil c;
c.input();
cout<<"-------------"<<endl;
c.output();
cout<<"-------------"<<endl;
m.idata();
cout<<"-------------"<<endl;
m.odata();
getch();
}
Output:
Comments
Post a Comment