Assign a subclass object to the parent class reference

Source: Internet
Author: User

# Include <iostream>
Using namespace STD;
Class Point
{
Public:
Point (float = 0, float = 0 );
Void setpoint (float, float );
Float getx () const {return X ;}
Float Gety () const {return y ;}
Friend ostream & operator <(ostream &, const point &);
Protected:
Float X, Y;
};

Point: Point (float a, float B)
{X = A; y = B ;}
Void point: setpoint (float a, float B)
{X = A; y = B ;}
Ostream & operator <(ostream & output, const point & P)
{Output <"[" <p. x <"," <p. Y <"]" <Endl;
Return output;
}

Class circle: Public point
{Public:
Circle (float x = 0, float y = 0, float r = 0 );
Void setradius (float );
Float getradius () const;
Float area () const;
Friend ostream & operator <(ostream &, const circle &);
Protected:
Float radius;
};

Circle: Circle (float a, float B, float R): Point (a, B), radius (r ){}

Void circle: setradius (float R)
{Radius = r ;}

Float circle: getradius () const {return radius ;}

Float circle: Area () const
{Return 3.14159 * radius ;}

Ostream & operator <(ostream & output, const circle & C)
{Output <"center = [" <C. x <"," <C. Y <"], r =" <C. radius <", Area =" <C. area () <Endl;
Return output;
}

Class cylinder: public circle
{Public:
Cylinder (float x = 0, float y = 0, float r = 0, float h = 0 );
Void setheight (float );
Float getheight () const;
Float area () const;
Float volume () const;
Friend ostream & operator <(ostream &, const cylinder &);
Protected:
Float height;
};

Cylinder: cylinder (float a, float B, float R, float H)
: Circle (a, B, R), height (H ){}

Void cylinder: setheight (float h) {Height = H ;}

Float cylinder: getheight () const {return height ;}

Float cylinder: Area () const
{Return 2 * circle: Area () + 2*3.14159 * radius * height ;}

Float cylinder: volume () const
{Return circle: Area () * height ;}

Ostream & operator <(ostream & output, const cylinder & CY)
{Output <"center = [" <cy. x <"," <cy. Y <"], r =" <cy. radius <", H =" <cy. height
<"\ Narea =" <Cy. Area () <", volume =" <Cy. Volume () <Endl;
Return output;
}

int main ()
{cylinder cy1 (3.5, 6.4, 5.2, 10);
cout <"\ noriginal cylinder: \ Nx = " point & Pref = cy1;
cout <" \ npref as a point: " circle & CREF = cy1;
cout <"\ ncref as a circle:" return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.