栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > C/C++/C#

为什么要用类的继承

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

为什么要用类的继承

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




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

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/303180.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号