Home > Back-end >  C a friend function of the derived class can access private data members of the base class
C a friend function of the derived class can access private data members of the base class

Time:09-30

Class Shape//abstract base class
{
Public:
Virtual float area const () {return 0.0; }//virtual function
Const virtual float volume () {return 0.0; }//virtual function
Virtual void shapName (const)=0;//pure virtual function
};

The class Point: public Shape//some kind of public inheritance Shape
{
Public:
Point (float x=0, float y=0).

Void SetPoint (float a, float b);
Const float getx () {return x; }
Const float gety () {return y; }
Virtual void shapName () const {cout<& lt;" Point: "; }
Friend ostream& The operator & lt; & lt; (ostream& The output, const Point & amp; P);//the Point class a friend function
Protected:
Float x, y;
};

Point: : Point (float a, float b) : (a) x, y (b) {}

The void Point: : SetPoint (float a, float b)
{
X=a;
Y=b;
}

Ostream& The operator & lt; & lt; (ostream& The output, const Point & amp; P)
{
Output<& lt;" [" & lt; & lt; p.x & lt; & lt; ", "& lt; & lt; p.y & lt; & lt;"] ".
return output;
}

Class Circle: public Point//public inheritance Point class
{
Public:
Circle (float x=0, float y=0, float r=0).

Void setRadius (float r);
Const float getRadius () {return radius; }
Virtual float area () the const;
Virtual void shapName () const {cout<& lt;" Circle: "; }
Friend ostream& The operator & lt; & lt; (ostream& The output, const Circle& C);//Circle of friends function

Protected:
Float the radius;
};

Circle: : Circle (a, float float b, float r) :
Point (a, b), the radius (r) {}

Void Circle: : setRadius (float r)
{
The radius=r;
}

Float Circle: : area (const)
{
Return the radius radius, 3.14159 * *;
}

Ostream& The operator & lt; & lt; (ostream& The output, const Circle& C)
{
//output<& lt;" [" & lt; & lt; c.x & lt; & lt; ", "& lt; & lt; c.y & lt; & lt;"], r="& lt; & lt; C.r adius;//this two kinds of writing that right? A friend function of the derived class can access private data members of the base class
Output<& lt;" [" be sad etx chtistina georgina rossetti.british poetess & lt; & lt; () & lt; & lt; ", "be sad ety chtistina georgina rossetti.british poetess & lt; & lt; () & lt; & lt;"], r="& lt; & lt; C.r adius;
return output;
}

CodePudding user response:

Can't, because the public inheritance derived class cannot access private data base class
  • Related