C + + toward Ocean--44 (Project One, point-circle-cylinder class family design, derived classes)

Source: Internet
Author: User
Tags gety

*/* Copyright ( c) 2016, School of Computer and Control engineering, Yantai University * All rights reserved. * File name: Text.cpp * Changxuan * Public Number: Worldhello * Completion Date: May 8, 2016 * version number: V1.0 * Problem Description: Derivation of Class * Program input: NO * program output: See operating results */#include <iostream                    Implementation of the >//Point class using Namespace Std;class point{public:point (double x=0,double y=0);                  constructor void SetPoint (double,double);              Sets the coordinate value double GetX () const {return x;}              Read x-coordinate double GetY () const {return y;} Read y-coordinate void show ();p rotected://protected Member double x,y;};/    The/point constructor point::P oint (double a,double b) {x=a; Y=b;}    Set x and Y coordinate values void Point::setpoint (double a,double b) {x=a; Y=b;} void Point::show () {cout<< "(" <<x<< "," <<y<< ")" <<ENDL;}    int main () {point P (2.4,5.1);    P.show ();    P.setpoint (1.8,3.2);    P.show (); return 0;} 



Operation Result:



#include <iostream>//The implementation of the circular class using namespace Std;class point{public:point (double x=0,double y                    =0);                  constructor void SetPoint (double,double);   Sets the coordinate value double GetX () const {return x;   Read x-coordinate} double GetY () const {return y; Read y-coordinate} void Show ();p rotected://protected Member double x,y;};/    The/point constructor point::P oint (double a,double b) {x=a; Y=b;}    Set x and Y coordinate values void Point::setpoint (double a,double b) {x=a; Y=b;} void Point::show () {cout<< "[" <<x<< "," <<y<< "]" &LT;&LT;ENDL;}  Class Circle:public Point//circle is the common derived class of the point class {public:circle (double x=0,double y=0,double r=0);                  constructor void Setradius (double);               Set the RADIUS value double Getradius () const;                   Read RADIUS value double area () const; Calculate Circle area Void Show ();p rotected:double radius;};/ /define constructors for center coordinates and RADIUS initialization circle::circle (double A,doubLe b,double R):P Oint (A, B), radius (r) {}//set radius value void Circle::setradius (double R) {radius=r;} Read RADIUS value double Circle::getradius () const{return radius; Calculate Circle Area Double Circle::area () const{return 3.14159*radius*radius;} Output Circle information void Circle::show () {cout<< "center= (" <<x<< "," <<y<< "), r=" <<radius< < ", area=" <<area () <<endl;}    int main () {Circle C (3.1,5,6.7);    cout<< "Original circle:\n";    C.show ();    C.setpoint (3,3);    cout<< "New circle:\n";    C.show (); return 0;}

Operation Result:



#include <iostream>//cylinder class implementation using namespace Std;class Point{public:point (double x=0,double y=0)                    ;                  constructor void SetPoint (double,double);   Sets the coordinate value double GetX () const {return x;   Read x-coordinate} double GetY () const {return y; Read y-coordinate} void Show ();p rotected://protected Member double x,y;};/    The/point constructor point::P oint (double a,double b) {x=a; Y=b;}    Set x and Y coordinate values void Point::setpoint (double a,double b) {x=a; Y=b;} void Point::show () {cout<< "[" <<x<< "," <<y<< "]" &LT;&LT;ENDL;}  Class Circle:public Point//circle is the common derived class of the point class {public:circle (double x=0,double y=0,double r=0);                  constructor void Setradius (double);               Set the RADIUS value double Getradius () const;                   Read RADIUS value double area () const; Calculate Circle area Void Show ();p rotected:double radius;};/ /define constructors for center coordinates and RADIUS initialization circle::circle (double a,doubleB,double r):P Oint (A, B), radius (r) {}//set radius value void Circle::setradius (double R) {radius=r;} Read RADIUS value double Circle::getradius () const{return radius; Calculate Circle Area Double Circle::area () const{return 3.14159*radius*radius;} Output Circle information void Circle::show () {cout<< "center=[" <<x<< "," <<y<< "], r=" <<radius< < ", area=" <<area () <<endl;} Class Cylinder:public Circle{public:cylinder (double x=0,double y=0,double r=0,double h=0);//constructor void SetHeight (Do                      uble);                    Set cylinder high double getheight () const;                         Read cylinder high double area () const;                       Calculates the cylindrical surface area double volume () const;                               Calculates the cylinder volume void show ();p rotected:double height; Cylindrical high};//definition constructor Cylinder::cylinder (double a,double b,double r,double h): Circle (a,b,r), height (h) {}//set cylinder high void Cylinder::setheight (Double h) {height=h;} Read cylinder high double cylinder::getheight () const{return height;Calculate cylindrical surface Area double Cylinder::area () const{return 2*circle::area () +2*3.14159*radius*height; Calculates the cylinder volume double cylinder::volume () const{return Circle::area () *height; void Cylinder::show () {cout<< "center=[" <<x<< "," <<y<< "], r=" <<radius<< ", H= "<
Operation Result:


C + + toward Ocean--44 (Project One, point-circle-cylinder class family design, derived classes)

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.