C + + 11th week (Spring) Project 4-Class family design

Source: Internet
Author: User
Tags gety

Course Home:http://blog.csdn.net/sxhelijian/article/details/11890759, complete teaching program and resources Link


"Project 4-class family design" according to the following tips, from the base class design and test start, gradually completed the design of each class, to find out the surface area of the cylindrical cylinder, volume and output and completed the required calculation tasks:
(1) First set up a point (dot) class, including data member X, y (coordinate point), implement the required member functions, and design the main function complete test;
(2) using point as the base class, derive a circle (circle) class, add the data member R (RADIUS), and the area of the member function to achieve the other required member functions, design main function complete test;
(3) The Circle class is the direct base class, derives a cylinder (cylinder) class, then adds the data member H (high), and the member function area of the cylinder surface and the member function of the cylinder volume volume, implements the required member function, and designs the main function to test.
The code is required to design the "required member functions" in various categories, including constructors, destructors, changes to data members and public interfaces to get data members, overloaded operator "<<" functions for output, and so on.
(Tip: This task can be divided into three sub-tasks into several steps.) First, declare the base class, then declare the derived class, step through, and Debug. --Such a method is applicable to any project that does whatever.


Answers to the references:

(1) First set up a point (dot) class, including data member X, y (coordinate point), implement the required member functions, and design the main function complete test;

#include <iostream>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 friend Ostream & operator<< (ostream &,const Point &);//overloaded operator "<<" protected:// Protected member Double x,y;};/ /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;} Ostream & operator<< (ostream &output,const point &p) {output<< "[" <<p.x<< "," < <p.y<< "]" <<endl;return output;} int main () {point P (3.5,6.4); cout<< "x=" <<p.getx () << ", y=" <<p.gety () <<endl;  P.setpoint (8.5,6.8);                             cout<< "P:" <<p<<endl; return 0;}

(2) using point as the base class, derive a circle (circle) class, add the data member R (RADIUS), and the area of the member function to achieve the other required member functions, design main function complete test;

#include <iostream>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 friend Ostream & operator<< (ostream &,const Point &);//overloaded operator "<<" protected:// Protected member Double x,y;};/ /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;} Ostream & operator<< (ostream &output,const point &p) {output<< "[" <<p.x<< "," < <p.y<< "]" <<endl;return output;}  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 friend Ostream &operator<< (ostream &,const Circle &);//overloaded operator "<<" protected:Double radius;};/ /definition constructor, circle::circle (double a,double b,double R) for the center coordinate and radius initialization, radius (r) {}//set radius value void Circle::setradius:P oint (b) ( 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;} Overloaded operator "<<" to output circle information in a prescribed form ostream &operator<< (ostream &output,const circle &c) {output< < "center=[" <<c.x<< "," <<c.y<< "], r=" <<c.radius<< ", Area=" <<c.area () <<endl;return output;} int main () {Circle C (3.5,6.4,5.2);cout<< "Original circle:\nx=" <<c.getx () << ", y=" <<c.gety ()  << ", r=" <<c.getradius () << ", area=" <<c.area () <<endl;       C.setpoint (5,5);     cout<< "New circle:\n" <<c; return 0;}


(3) The Circle class is the direct base class, derives a cylinder (cylinder) class, then adds the data member H (high), and the member function area of the cylinder surface and the member function of the cylinder volume volume, implements the required member function, and designs the main function to test.

#include <iostream>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 friend Ostream & operator<< (ostream &,const Point &);//overloaded operator "<<" protected:// Protected member Double x,y;};/ /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;} Ostream & operator<< (ostream &output,const point &p) {output<< "[" <<p.x<< "," < <p.y<< "]" <<endl;return output;}  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 friend Ostream &operator<< (ostream &,const Circle &);//overloaded operator "<<" protected:Double radius;};/ /definition constructor, circle::circle (double a,double b,double R) for the center coordinate and radius initialization, radius (r) {}//set radius value void Circle::setradius:P oint (b) ( 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;} Overloaded operator "<<" to output circle information in a prescribed form 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 (double x=0,double y=0,double r=0,double h=0);//constructor void SetHeight (double                      );                    Set cylinder high double getheight () const;                         Read cylinder high double area () const;                       Calculates the cylindrical surface area double volume () const; Calculate the cylinder volume friend ostream& operator<< (ostream&,const cylinder&);//overloaded operator "<<" protected:double                               Height Cylindrical high};//defining constructors 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; Overloaded operator << 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=" <<cy1.getx () << ", y=" < <cy1.gety () << ", r=" <<cy1.getradius () << ", h=" <<cy1.getheight () << "\narea=" <     <cy1.area () << ", Volume=" <<cy1.volume () <<endl;cy1.setheight (15); Cy1.setradius (7.5);       Cy1.setpoint (5,5);        cout<< "\nnew cylinder:\n" <<cy1; return 0;}



=================== Helijian csdn Blog column ================= |== It Student Growth Guide Column column category folder (not regularly updated) ==| |== C + + Classroom Online Column The course teaching link (sub-course grade) ==| |== I wrote the book-"The reverse of the university-to the positive energy of IT students" ==| ===== for it rookie runway, and students enjoy a happy and passionate university =====



C + + 11th week (Spring) Project 4-Class family design

Related Article

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.