There are classes A, B, C. Class A is base class. Class B is derived class from
ID: 3582296 • Letter: T
Question
There are classes A, B, C. Class A is base class. Class B is derived class from class A with private derivation mode. Class C is derived class from class B with public derivation mode.
Class A has members in following sections -- private: int a; protected: float ap; public: void printa().
Class B has members in following sections -- private: char b; protected: float bp; public: void getb()
Class C has the members in following sections -- private: int c; protected: int cp; public: int assign(int k)
- Represent diagrammatically what sections and in what classes will be extended as a result of inheritance.
- Indicate what statements produce an error in the following code. Explain, why?
main()
{ A a; B b; C c;
c.ap = 10.6;
c.printa();
b.printa();
c.getb();
}
Explanation / Answer
I have represented the member derivation using structure of the derived classes.
class A
{
private: //Private members can't be inherited any derived class.
int a;
protected:
float ap;
public:
void printa();
}
class B: private A //In private inheritance, members/functions of base class will be inherited as private members/functions of derived class
{
private:
char b;
float ap;
void printa();
protected:
float bp;
public:
void getb();
}
class C: public A //In public inheritance, members/functions of base class will be inherited as it is except private members/functions of base class
{
private:
int c;
protected:
float ap;
float cp;
public:
void printa();
int assign(int k);
}
Following lines will throw error
int main()
{
A a; B b; C c;
c.ap = 10.6; //Error, accessing derived protected member outside the class
c.printa();
b.printa(); // Error, printa is a private function, it can't be accessed outside the class
c.getb(); //Error, getb function is not part of Class C
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.