Inheritance C++
Problem Statement : 1. Design a hierarchy of computer printers. Use multiple inheritance in your hierarchy (Say, Home Printer is Inherited from Xerox, Scan and Printer classes). Also use friend functions and classes in your program.
#include<iostream>
using namespace std;
class frnd;
class Xerox
{
int x_model;
friend class frnd;
public:
Xerox()
{
x_model = 100;
}
};
class Scan
{
int s_model;
public:
friend class frnd;
Scan()
{
s_model = 102;
}
};
class Printer
{
int p_model;
public:
Printer()
{
p_model = 103;
}
friend class frnd;
};
class HomePrinter : public Xerox, public Scan, public Printer
{
int h_model;
int access_code;
public:
HomePrinter()
{
h_model = 104;
access_code = 123456;
}
friend class frnd;
friend void display(HomePrinter );
};
class frnd
{
Xerox x;
Scan s;
Printer p;
HomePrinter h;
public:
void show()
{
cout<<"\nThe model number of Xerox is : "<<x.x_model<<endl;
cout<<"\nThe model number of Scan is : "<<s.s_model<<endl;
cout<<"\nThe model number of Printer is : "<<p.p_model<<endl;
cout<<"\nThe model number of HomePrinter is : "<<h.h_model<<endl;
}
};
void display(HomePrinter h1 ) //global function friend to class HomePrinter
{
cout<<"\nAccessing the access code via friend function : "<<h1.access_code<<endl;
}
int main()
{
frnd f;
f.show();
HomePrinter h1;
display(h1);
return 0;
}
#COFPROG
Sign up here with your email
ConversionConversion EmoticonEmoticon