#includeusing namespace std; //class Circle; class Point { private: int x; int y; public: Point(int x = 0, int y = 0); Point(const Point& point); ~Point(); void setX(int x); int getX(void); void setY(int y); int getY(void); void show(void); friend class Circle;//我要访问点类,申请成为友元类 }; class Circle { private: static const double PI; double radius; Point center; public: Circle(int x = 0, int y = 0, double radius = 0); Circle(const Circle& circle); ~Circle(); //void show(void);//这个函数和复制点类的方法重复了 void setR(int radius); double getR(void); void setX(int x); int getX(void); void setY(int y); int getY(void); void show(void); }; int main() { Point p1; p1.setX(1); p1.setY(2); p1.show(); Circle c; c.setX(3); c.setY(4); c.setR(5); c.show(); return 0; } Point::Point(int x, int y) { this->x = x; this->y = y; } Point::Point(const Point& point) { this->x = point.x; this->y = point.y; } Point::~Point() { cout << "Point析构完成" << endl; } void Point::setX(int x) { this->x = x; } int Point::getX(void) { return this->x; } void Point::setY(int y) { this->y = y; } int Point::getY(void) { return this->y; } void Point::show(void) { cout << "(x,y)"<<"(" << this->y <<"," << this->y <<")" << endl; } const double Circle::PI = 3.141592654; Circle::Circle(int x, int y, double radius) : center(x, y) { this->radius = radius; } Circle::Circle(const Circle& circle) { this->radius = circle.radius; this->center = circle.center; } Circle::~Circle() { cout << "Circle析构完成" << endl; } void Circle::setR(int radius) { this->radius = radius; } double Circle::getR(void) { return this->radius; } void Circle::setX(int x) { this->center.x = x; } int Circle::getX(void) { return this->center.x; } void Circle::setY(int y) { this->center.y = y; } int Circle::getY(void) { return this->center.y; } void Circle::show(void) { cout << "(x,y)" << "(" << this->center.x << "," << this->center.y << ")" << endl; }
#includeusing namespace std; //class Circle; class Point { private: int x; int y; public: Point(int x = 0, int y = 0); Point(const Point& point); ~Point(); void setX(int x); int getX(void); int getX(void) const; void setY(int y); int getY(void); int getY(void) const; void show(void); }; class Circle:public Point { private: static const double PI; double radius; public: Circle(int x = 0, int y = 0, double radius = 0); Circle(const Circle& circle); ~Circle(); void setR(int radius); double getR(void); }; int main() { Point p1; p1.setX(1); p1.setY(2); p1.show(); Circle c; c.setX(3); c.setY(4); c.setR(5); c.show(); return 0; } Point::Point(int x, int y) { this->x = x; this->y = y; } Point::Point(const Point& point) { this->x = point.x; this->y = point.y; } Point::~Point() { cout << "Point析构完成" << endl; } void Point::setX(int x) { this->x = x; } int Point::getX(void) { return this->x; } int Point::getX(void) const{ return this->x; } void Point::setY(int y) { this->y = y; } int Point::getY(void) { return this->y; } int Point::getY (void) const { return this->y; } void Point::show(void) { cout << "(x,y)"<<"(" << this->y <<"," << this->y <<")" << endl; } const double Circle::PI = 3.141592654; Circle::Circle(int x, int y, double radius) : Point(x, y) { //派生类的构造函数使用初始化表调用基类的构造函数 this->radius = radius; } Circle::Circle(const Circle& circle):Point(circle.getX(), circle.getY()) { //派生类使用拷贝构造,基类该如何初始化 //由于circle是const,也就是常对象,而长对象只能调用常方法,所以我们要为circle.getX()写一个常方法 this->radius = circle.radius; } Circle::~Circle() { cout << "Circle析构完成" << endl; } void Circle::setR(int radius) { this->radius = radius; } double Circle::getR(void) { return this->radius; }
#include#include using namespace std; class Shape { private: char name[8]; char color[8]; public: Shape(const char* name = "none", const char* color = "none"); void SetName(char* name); void SetColor(char* color); void Draw(); }; class Circle :public Shape { private: static const double PI; double radius; public: Circle(const char* name = "none", const char* color = "none", double raidus = 0); double Area(); }; const double Circle::PI = 3.14; Circle::Circle(const char* name, const char* color, double raidus) :Shape(name, color) { this->radius = radius; } double Circle::Area() { return PI * radius * radius; } int main() { Shape s("shape ", "red"); Circle c("circle ", "blue", 10); s.Draw(); c.Draw(); return 0; } Shape::Shape(const char* name, const char* color) { strcpy_s(this->name, strlen(name) + 1, name); strcpy_s(this->color, strlen(color) + 1, color); } void Shape::SetName(char* name) { strcpy_s(this->name, strlen(name) + 1, name); } void Shape::SetColor(char* color) { strcpy_s(this->color, strlen(color) + 1, color); } void Shape::Draw() { cout << "Draw " << name << "with color " << color << endl; }