为什么要用类的继承

#include <iostream>
using 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;
}

/*运行结果
(x,y)(2,2)
(x,y)(3,4)
Circle析构完成
Point析构完成
Point析构完成
*/

/*-----点类构造和析构-------*/
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::show(void) {
	cout << "圆心:(" << center.x << "," << center.y<<")" << endl;
	cout << "半径:" << radius << endl;
}
*/

/*-----圆类复制点类方法-------*/
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;
}
#include <iostream>
using 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.gex(),circle.gety()。另外这种关系用继承的方式不是一种好的方式。继承一般表达是is关系。比如,圆是图形,矩形是图形。可把图形作为基类,圆,矩形作为派生类。同样动物可以作为基类,猫 狗作为动物的派生类。因为圆包含圆心,作为内嵌对象更恰当。*/
    //由于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<iostream>
#include<cstring>
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值