Home > Back-end >  Inheritance and derived classes
Inheritance and derived classes

Time:04-13

Some kind of Point as the base class public derived class Circle Circle, and take the class Circle Circle is the base class public derived ball Sphere, the main (void) function to finish the test,
Point class structure elucidation:
Point class data members include:
(1) the private data members: X coordinates X type (double), Y Y type (double),
Point class member functions include:
(1) there are arguments constructor Point (double, double) and copy constructor Point (const Point & amp;) , including the Constructor parameters default value is 0, the output information "Point Constructor run", the copy Constructor output information "Point CopyConstructor run"
(2) the Destructor, the Destructor output information "Point Destructor run"
(3) members of the public function: void your setX (double) and double getX () const returns and sets the X coordinate
(4) members of the public function: void setY (double) and double getY () const return and set the Y coordinate
Const (5) public member functions to void the show () is used to display the coordinates of point information, display format is: (x, y)
6 public member functions to double area (const) is used to calculate points area, the area of the point of 0
# # # Circle class structure elucidation:

Public derived class Circle Circle to Point class Point as the base class, compared to some kind of Point, Circle class structure that is as follows:
Circle class data members include:
(1) the private data members: X coordinates X inherited from Point class, Y Y, inherited from the Point class
(2) the private data members: radius radius (double)
(3) static data constant PI (double), the value of 3.14159
Circle a class member function include:
1) define and constructor Circle (double, double, double) and copy constructor Circle (Circle & amp;) , including the Constructor parameters including the center coordinates and radius, call center Point to structure class Constructor, default value is 0, the parameters output information "Circle Constructor run", the copy Constructor output information "Circle CopyConstructor run"
(2) define the Destructor, the Destructor output information "Circle Destructor run"
(3) define the members of the public function: void setRadius (double) and double getRadius () const return and set the radius
(4) define public member functions to double circumference (const) is used to calculate the circumference of the circle
5. Overloaded double area (const) is used to calculate the area of a circle
6. Overloading void show () const is used to display round information, display format is:
(circle X coordinates, circle Y coordinates), Radius of the Radius=
Sphere class structure elucidation:
Public class derived ball Sphere to Circle Circle as the base class, Sphere structure of a class that is as follows:
Sphere class data members include:
Private data members: (1) inherited from the Circle class X coordinates X, Y coordinates Y, inherited from the Circle class radius radius, inherited from the Circle class
(2) the static data of constant PI, inherited from the Circle class
Sphere a class member function include:
1) define and constructor Sphere (double, double, double) and copy constructor Sphere (Sphere & amp;) , including the Constructor parameters including the center coordinates and radius, call center Point to structure class Constructor, default value is 0, the parameters output information "Sphere Constructor run", the copy Constructor output information "Sphere CopyConstructor run"
(2) define the Destructor, the Destructor output information "Sphere Destructor run"
(3) define the public member functions to double volume (const) used in calculating the ball size
(4) overloaded double area (const) is used to calculate the ball surface area of the
The referee sample testing program:
#include
using namespace std;
The class Point {
Protected:
double x;
double y;
Public:
Point (double xv=0, double yv=0).//a parameter structure
Point (const Point & amp; P);//copy structure
~ Point ();//destructors
Void the show () the const;//display Point information
Double area () the const;//area=0
Void your setX (double xv);//set the X coordinate
Void setY (double yv);//set Y
Double getX () const;//get X coordinate
Double getY () const;//get Y
};
//a parameter structure
Point: : Point (double xv, double yv) {
X=xv.
Y=yv;
Cout<& lt;" Point Constructor run "& lt; }
//copy structure
Point: : Point (const Point & amp; P) {
X=p.x;
Y=p.y;
Cout<& lt;" Point CopyConstructor run "& lt; }
//destructors
Point: : ~ Point () {
Cout<& lt;" Point Destructor run "& lt; }
//display Point
The void Point: : show () const {
Cout<& lt;" (" & lt; }
//set the X coordinate
Void Point: : your setX (double xv) {
X=xv.
}
//set Y
The void Point: : setY (double yv) {
Y=yv;
}
//area function
Double Point: : area () const {
return 0;
}
//get X coordinate
Double Point: : getX () const {
return x;
}
//get Y
Double Point: : getY () const {
return y;
} # include & lt; iostream>
using namespace std;
//the Point class
The class Point {
Protected:
double x;
double y;
Public:
Point (double xv=0, double yv=0).//a parameter structure
Point (const Point & amp; P);//copy structure
~ Point ();//destructors
Void the show () the const;//display Point information
Double area () the const;//area=0
Void your setX (double xv);//set the X coordinate
Void setY (double yv);//set Y
Double getX () const;//get X coordinate
Double getY () const;//get Y
};
//a parameter structure
Point: : Point (double xv, double yv) {
X=xv.
Y=yv;
Cout<& lt;" Point Constructor run "& lt; }
//copy structure
Point: : Point (const Point & amp; P) {
X=p.x;
Y=p.y;
Cout<& lt;" Point CopyConstructor run "& lt; }
//destructors
Point: : ~ Point () {
Cout<& lt;" Point Destructor run "& lt; }
//display Point
The void Point: : show () const {
Cout<& lt;" (" & lt; }
//set the X coordinate
Void Point: : your setX (double xv) {
X=xv.
}
//set Y
The void Point: : setY (double yv) {
Y=yv;
}
//area function
Double Point: : area () const {
return 0;
}
//get X coordinate
Double Point: : getX () const {
return x;
}
//get Y
Double Point: : getY () const {
return y;
}

Please fill in the answer here */*/

//the main function
Int main (void) {
Double r;
Cin> r;
Sphere s1 (1, 4-trichlorobenzene), s2 (s1);
S1. The show ();
Cout
  • Related