Week 5-inheritance and derivation-Project 5-class family design, week 5-
/** Copyright (c) 2014, School of Computer Science, Yantai University * All rights reserved. * file name: test. cpp * Author: Liu Chang * Completion Date: July 15, May 19, 2015 * version No.: v1.0 ** Problem description: Follow the prompts below to start the design and test of the base class, gradually complete the design of each class, find the surface area and volume of the cylindrical cylinder, and output and complete the required computing tasks: (1) first create a Point class, contains data members x and y to implement the required member functions and design the main function to complete the test. (2) use the Point class to derive a Circle) class, add the data member r (RADIUS), and calculate the area of the member function area, implement other required member functions, design the main function to complete the test; (3) then a Cylinder (Cylinder) class is derived from the Circle class as the direct base class. Then, the data member h (high) is added, and the member function area of the cylindrical surface area is obtained and the Cylinder is obtained. Generate the member function volume to implement the required member function, and design the main function to complete the test. Programming is required to design various types of "required member functions ", including constructor, destructor, public interface for modifying data members and getting data members, and overload operator "<" for output. * Program input :... * Program output: coordinates of points; circle radius and area; code for the height and volume of the cylinder: # include <iostream> using namespace std; class Point {public: point (double x = 0, double y = 0); // constructor void setPoint (double, double); // set the coordinate value double getX () const {return x; // read x coordinate} double getY () const {return y; // read y coordinate} friend ostream & operator <(ostream &, const Point &); // overload operator "<" protected: // double x, y;}; Point: Point (double a, double B) {x =; y = B;} void Point: setPoint (double a, double B) {x = a; y = B;} ostream & operator <(ostream & output, const Point & p) {output <"(" <p. x <"," <p. y <")" <endl; return output;} class Circle: public Point // circle is a public 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 the radius value double area () const; // calculate the circular area friend ostream & operator <(ostream &, const Circle &); // reload the operator "<" protected: double radius ;}; Circle: Circle (double a, double B, double r): Point (a, B), radius (r) {} void Circle :: setRadius (double r) {radius = r;} double Circle: getRadius () const {return radius;} double Circle: area () const {return 3.14 * radius;} ostream & operator <(ostream & output, const Circle & c) {output <"Center = (" <c. x <"," <c. y <"), r =" <c. radius <endl; output <"Area =" <c. area () <endl; return output;} class Cylinder: public Circle {public: Cylinder (double x = 0, double y = 0, double r = 0, double h = 0); // constructor void setHeight (double); // set the height of a cylindrical double getHeight () const; // read the height of a cylindrical double area () const; // calculate the cylindrical surface area double volume () const; // calculate the cylindrical volume friend ostream & operator <(ostream &, const Cylinder &); // overload operator "<" protected: double height; // cylindrical height}; Cylinder: Cylinder (double a, double B, double r, double h ): circle (a, B, r), height (h) {} void Cylinder: setHeight (double h) {height = h;} double Cylinder: getHeight () const {return height;} double Cylinder: area () const {return 2 * Circle: area () + 2*3.14159 * radius * height;} double 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 <endl; output <"Area =" <cy. area () <", Volume =" <cy. volume () <endl; return output;} int main () {Cylinder cy1 (3.5, 6.4, 5.2, 10); cout <"Original cylinder: \ nx = "<cy1.getX () <", y = "<cy1.getY () <", r = "<cy1.getRadius () <", h = "<cy1.getHeight () <endl; cout <" Area = "<cy1.area () <", Volume = "<cy1.volume () <endl; cy1.setHeight (15); cy1.setRadius (7.5); cy1.setPoint (); cout <endl; cout <"New cylinder:" <cy1; return 0 ;}
Running result: