IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++(二):继承和多态 -> 正文阅读

[C++知识库]C++(二):继承和多态

继承访问权限测试

  1. 设计类A具有public, protected, private等不同属性的成员函数或变量
class base {
public:
	int q=10;//外部只能访问到这一步
protected:
	int w = 11;//子类只能访问到基类的这一步
private:
	int e = 12;//无法被外部访问
};
  1. 实现类B通过public, protected, private等不同方式继承A,在类B的成员函数中测试访问A的成员函数或变量;

    1、使用public继承类base
    子函数使用public继承类A时,在子函数内部只能访问到protected部分,外部访问只能访问到public部分

class Pub_b :public base {
public:
	void Test() {
		q = 10;
		w = 10;
		//e = 10;//无法访问
	}

	int Pub_pub_b1 = 10;
protected:
	int Pub_pro_b2 = 11;
private:
	int Pub_pri_b3 = 12;
};

此时的输出:
在这里插入图片描述
既只能访问到public部分

2、使用protected继承类base

class Pro_B :protected base {
public:
	void Test() {
		q = 10;
		w = 10;
		//c = 10;//无法访问
	}

	int Pro_pub_b1 = 1;
protected:
	int Pro_pro_b2 = 2;
private:
	int Pro_pri_b3 = 3;
};

此时的输出结果:
在这里插入图片描述
既以protected方式继承的外部不能访问继承到的public部分成员,只能访问到自己定义的public成员

3、使用private继承类base

class Pri_B :private base {
public:
	void Test() {
		q = 10;
		w = 10;
		//c = 10;//无法访问
	}

	int Pri_pub_b1 = 4;
protected:
	int Pri_pro_b2 = 5;
private:
	int Pri_pri_b3 = 6;
};

此时的输出:
在这里插入图片描述

4、以private方式继承A,尝试把base中的部分public成员提升为public。
using的作用:在子类中引用基类的成员
newPri_B私有继承base类,所以base类中公有成员q不能被外部访问,但使用using后可以在main中访问到q

class newPri_B :private base {
public:
	void Test() {
		q = 100;
		w = 100;
		//c = 10;//无法访问
	}
	int pri_pub_b1 = 7;
	using base::q;
protected:
	int Pri_pro_b2 = 8;
private:
	int Pri_pri_b3 = 9;
};

输出:
在这里插入图片描述

友元类继承

1、设计类A含有私有变量a,在类A中友元给类C

class A {
public:
	int a = 10;
protected:
	int b = 1;
private:
	int c = 2;
	friend class C;//友元,使c可以访问到A的私有变量c的值
};

2、设计类B继承A,添加私有变量b;在类C中测试访问类B的成员变量a, b;

class B :public A {
private:
	int b=3;
};

class C {
public:
	void Test() {
		B b1;
		b1.a;
		//b1.b; 不可访问
		A a1;
		a1.c;//因为上面通过友元函数使得c的成员可以访问到A的私有变量
		
	}
	int a;
};

3、设计类D继承C,在D的成员函数中测试访问类A的成员变量a,类B的成员变量a, b。

class D :public C {
public:
	void Test() {
		A a1;
		a1.a;
		//a1.c; 不可访问
		//友元函数:朋友可以访问,朋友的儿子不能访问
		B b2;
		b2.a;
		//b2.b;
	}
};

多态的综合性运用

一般多态性函数:输入输出参数完全一样,在父类中添加virtual

class A {

public:
	int a = 1;
	virtual void func1();
	void func2();
protected:
	int b = 2;
private:
	int c = 3;

}Base;

特殊多态性函数:输入或输出参数在子类中是父类的指针或基类的引用,在子类中对于的是子类的指针或子类的引用

class A {

public:
	int a = 1;
	virtual void func1();
	void func2();
protected:
	int b = 2;
private:
	int c = 3;

}Base;
void A :: func1() {
	cout << "这是基类,这是func1" << endl;
}

void A::func2() {
	cout << "这是基类,这是func2" << endl;
}

class B : public A {
	void func1();
	void func2();
}Son;

void B::func1() {
	cout << "这是子类,这是func1" << endl;
}

void B::func2() {
	cout << "这是子类,这是func2" << endl;
}
int main(int argc, char* argv[]) {
	A* a;
	if (_getch() == 'c') {
		a = &Son;
	}
	else a = &Base;
	a->func1();
	a->func2();
}

此时的输出结果:
当输入为c时
在这里插入图片描述
当输入不为c时
在这里插入图片描述

析构函数的多态性

虚析构函数可以防止内存泄漏

class A {

public:
	int a = 1;
	virtual void func1();
	void func2();
	virtual ~A();
protected:
	int b = 2;
private:
	int c = 3;

}Base;

多继承

使用虚继承的要求:当出现多重继承并且继承关系是一个环时使用虚继承

class A {

public:
	int a = 1;
	virtual void func1();
	void func2();
protected:
	int b = 2;
private:
	int c = 3;

}Base;
class B1 :public virtual A {
public:
	void Test() {
		a = 11;
		b = 12;
	}
	int b = 101;
};

class B2 :public virtual A {
public:
	void Test() {
		a = 11;
		b = 12;
	}
	int b = 1111;
};
class D :public B1, public B2 {
public:
	void Test() {
		B1_b = 12;
		B2_b = 15;
	}
	int d = 10;
};

设计矢量图,运用多继承设计组合图形,要求具备创建不同类型矢量图、选择图形、移动图形、用不同颜色显示图形(表示选中与否),用vector或数组管理图形。

#ifndef SHAPE_H
#define SHAPE_H
#include<string>
#include<math.h>
using namespace std;

class CPoint;
class CRect;
class Shape
{
public:
	Shape();
	Shape(const Shape& shape);
	virtual ~Shape();
	virtual double GetArea() const;
	virtual bool ptIn(const CPoint& pt) const;
	virtual bool InRect(const CRect& rc) const;
	virtual void Draw() const;
	virtual void DrawColor();
	virtual Shape* Clone() const;
	virtual Shape& Move(int nOffsetX, int nOffsetY);

protected:
	string m_sName;
};

class CPoint :public Shape {
public:
	int m_nPosX;
	int m_nPosY;
	CPoint() {
		m_nPosX = 0;
		m_nPosY = 0;
	}
	CPoint(int nPosX, int nPosY);
	CPoint(const CPoint& pt);
	virtual ~CPoint();
	double GetArea() const;
	bool ptIn(const CPoint& pt) const;
	bool InRect(const CRect& rc) const;
	void Draw() const;
	void DrawColor();
	CPoint* Clone() const;
	CPoint& Move(int nOffsetX, int nOffsetY);
};
class CTriangle :virtual public Shape {
public:
	CTriangle() {}
	CTriangle(const CPoint& pt1, const CPoint& pt2, const CPoint& pt3);
	CTriangle(const CTriangle& rc);
	CTriangle(const CPoint& pt);
	virtual ~CTriangle();
	double GetArea() const;
	bool ptIn(const CPoint& pt) const;
	bool InRect(const CRect& rc) const;
	void Draw() const;
	void DrawColor();
	Shape* Clone() const;
	Shape& Move(int nOffsetX, int nOffsetY);
	CPoint m_pts[3];
};

class CRect :virtual public Shape {
public:
	CRect() {}
	CRect(CPoint pt1, CPoint pt2);
	CRect(const CRect& rc);
	CRect(CPoint pt1);
	virtual ~CRect();
	double GetArea() const;
	bool ptIn(const CPoint& pt) const;
	bool InRect(const CRect& rc) const;
	void Draw() const;
	void DrawColor();
	Shape* Clone() const;
	Shape& Move(int nOffsetX, int nOffsetY);
	CPoint m_ptLT;
	CPoint m_ptBR;
};

class Comgraphics :public CRect, public CTriangle {
public:
	Comgraphics(const CRect& pt1);
	Comgraphics(const Comgraphics& rc);
	Comgraphics(const CPoint pt1);
	virtual ~Comgraphics();
	double GetArea() const;
	bool ptIn(const CPoint& pt) const;
	bool InRect(const CRect& rc) const;
	void Draw() const;
	void DrawColor();
	Shape* Clone() const;
	Shape& Move(int nOffsetX, int nOffsetY);
	CPoint m_pt1;
	CPoint m_pt2;

};
#endif

#include "Shape.h"
#include "graphics.h"
#include <iostream>
using namespace std;
//CShape
Shape::Shape()
{
}
Shape::Shape(const Shape& shape) {
	m_sName = shape.m_sName;
}
Shape::~Shape()
{
}
double Shape::GetArea() const {
	return 0;
}
bool Shape::ptIn(const CPoint& pt) const {
	return false;
}
bool Shape::InRect(const CRect& rc) const {
	return false;
}
void Shape::Draw() const
{
}
void Shape::DrawColor()
{
}
Shape* Shape::Clone() const {
	return new Shape(*this);
}
Shape& Shape::Move(int nOffsetX, int nOffsetY) {
	return *this;
}

//CPoint
CPoint::CPoint(int nPosX, int nPosY) {
	m_nPosX = nPosX;
	m_nPosY = nPosY;
}
CPoint::CPoint(const CPoint& pt) {
	m_nPosX = pt.m_nPosX;
	m_nPosY = pt.m_nPosY;
}
CPoint::~CPoint() {
	//cout << "CPoint::~CPoint()\n";
}
double CPoint::GetArea() const {
	return 0;
}
bool CPoint::ptIn(const CPoint& pt) const {
	return false;
}
bool CPoint::InRect(const CRect& rc) const {
	return rc.ptIn(*this);
}
void CPoint::Draw() const {
	circle(m_nPosX, m_nPosY, 2);
}
void CPoint::DrawColor()
{
}
CPoint* CPoint::Clone() const {
	return new CPoint(*this);
}
CPoint& CPoint::Move(int nOffsetX, int nOffsetY) {
	m_nPosX += nOffsetX;
	m_nPosY += nOffsetY;
	return *this;
}

//CTriangle
CTriangle::CTriangle(const CTriangle& tri) {
	for (int i = 0; i < 3; i++) {
		m_pts[i] = tri.m_pts[i];
	}
}
CTriangle::~CTriangle() {
	//cout << "CTriangle::~CTriangle()\n";
}
CTriangle::CTriangle(const CPoint& pt1, const CPoint& pt2, const CPoint& pt3) {
	m_pts[0] = pt1;
	m_pts[1] = pt2;
	m_pts[2] = pt3;
}
CTriangle::CTriangle(const CPoint& pt)
{
	CPoint* pt1 = new CPoint(pt.m_nPosX + 100, pt.m_nPosY + 90);
	CPoint* pt2 = new CPoint(pt.m_nPosX, pt.m_nPosY + 90);
	m_pts[0] = pt;
	m_pts[1] = *pt1;
	m_pts[2] = *pt2;
}

Shape& CTriangle::Move(int nOffsetX, int nOffsetY) {
	for (int i = 0; i < 3; i++) {
		m_pts[i].Move(nOffsetX, nOffsetY);
	}
	return *this;
}
double CTriangle::GetArea() const {
	int x1, y1, x2, y2, x3, y3;
	x1 = m_pts[0].m_nPosX;
	y1 = m_pts[0].m_nPosY;
	x2 = m_pts[1].m_nPosX;
	y2 = m_pts[1].m_nPosY;
	x3 = m_pts[2].m_nPosX;
	y3 = m_pts[2].m_nPosY;

	double bottomLine = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
	double verticalLine1 = abs((y1 - y2) * x3 - (x1 - x2) * y3 + (x1 - x2) * y2 - (y1 - y2) * x2);
	double verticalLine2 = sqrt(pow(y1 - y2, 2) + pow(x1 - x2, 2));
	double verticalLine = verticalLine1 / verticalLine2;

	return (verticalLine * bottomLine) / 2.0;
}
bool CTriangle::ptIn(const CPoint& pt) const {
	CTriangle c1 = CTriangle(m_pts[0], m_pts[1], pt);
	CTriangle c2 = CTriangle(m_pts[1], m_pts[2], pt);
	CTriangle c3 = CTriangle(m_pts[2], m_pts[0], pt);

	double totalArea = c1.GetArea() + c2.GetArea() + c3.GetArea();

	if (totalArea == this->GetArea())
		return true;
	else
		return false;
}
bool CTriangle::InRect(const CRect& rc) const {
	return rc.ptIn(m_pts[0]) && rc.ptIn(m_pts[1]) && rc.ptIn(m_pts[2]);
}
void CTriangle::Draw() const {
	int poly[8] = { m_pts[0].m_nPosX ,m_pts[0].m_nPosY,m_pts[1].m_nPosX,m_pts[1].m_nPosY,
				m_pts[2].m_nPosX,m_pts[2].m_nPosY, m_pts[0].m_nPosX ,m_pts[0].m_nPosY };
	setfillcolor(EGERGB(0xFF, 0xFF, 0xFF));
	fillpoly(4, poly);
}
void CTriangle::DrawColor() {
	int poly[8] = { m_pts[0].m_nPosX ,m_pts[0].m_nPosY,m_pts[1].m_nPosX,m_pts[1].m_nPosY,
				m_pts[2].m_nPosX,m_pts[2].m_nPosY, m_pts[0].m_nPosX ,m_pts[0].m_nPosY };
	setfillcolor(EGERGB(0xFF, 0xA5, 0x00));
	fillpoly(4, poly);
}
Shape* CTriangle::Clone() const {
	return new CTriangle(*this);
}

//CRect
CRect::CRect(CPoint pt1, CPoint pt2) {
	m_ptLT = CPoint(min(pt1.m_nPosX, pt2.m_nPosX), min(pt1.m_nPosY, pt2.m_nPosY));
	m_ptBR = CPoint(max(pt1.m_nPosX, pt2.m_nPosX), max(pt1.m_nPosY, pt2.m_nPosY));
}
CRect::CRect(const CRect& rc) {
	m_ptLT = rc.m_ptLT;
	m_ptBR = rc.m_ptBR;
}
CRect::CRect(CPoint pt1)
{
	m_ptLT = CPoint(pt1.m_nPosX, pt1.m_nPosY);
	m_ptBR = CPoint(pt1.m_nPosX + 100, pt1.m_nPosY + 100);
}
CRect::~CRect() {
	// cout << "CRect::CRect()\n";
}
double CRect::GetArea() const {
	return (m_ptBR.m_nPosX - m_ptLT.m_nPosX) * (m_ptBR.m_nPosY - m_ptLT.m_nPosY);
}
bool CRect::ptIn(const CPoint& pt) const {
	return (pt.m_nPosX >= m_ptLT.m_nPosX && pt.m_nPosX <= m_ptBR.m_nPosX) &&
		(pt.m_nPosY >= m_ptLT.m_nPosY && pt.m_nPosY <= m_ptBR.m_nPosY);
}
bool CRect::InRect(const CRect& rc) const {
	return rc.ptIn(m_ptLT) && rc.ptIn(m_ptBR);
}
void CRect::Draw() const {
	// 存储n个顶点的x,y坐标
	int pts[10] = { m_ptLT.m_nPosX,m_ptLT.m_nPosY,m_ptBR.m_nPosX,m_ptLT.m_nPosY,
	m_ptBR.m_nPosX,m_ptBR.m_nPosY,m_ptLT.m_nPosX,m_ptBR.m_nPosY,m_ptLT.m_nPosX,m_ptLT.m_nPosY };
	// 绘制n个顶点的多边形,第一个参数必须要传入n+1,pts最后一个顶点坐标和第一个相同
	//drawpoly(5, pts);
	setfillcolor(EGERGB(0xFF, 0xFF, 0xFF));
	fillpoly(5, pts);
}
void CRect::DrawColor() {
	int pts[10] = { m_ptLT.m_nPosX,m_ptLT.m_nPosY,m_ptBR.m_nPosX,m_ptLT.m_nPosY,
	m_ptBR.m_nPosX,m_ptBR.m_nPosY,m_ptLT.m_nPosX,m_ptBR.m_nPosY,m_ptLT.m_nPosX,m_ptLT.m_nPosY };
	// 绘制n个顶点的多边形,第一个参数必须要传入n+1,pts最后一个顶点坐标和第一个相同
	setfillcolor(EGERGB(0xFF, 0xA5, 0x00));
	fillpoly(5, pts);
}
Shape* CRect::Clone() const {
	return new CRect(*this);
}
Shape& CRect::Move(int nOffsetX, int nOffsetY) {
	m_ptLT.Move(nOffsetX, nOffsetY);
	m_ptBR.Move(nOffsetX, nOffsetY);
	return *this;

}
//Comgraphics
Comgraphics::Comgraphics(const CRect& pt1) {

	m_pt1.m_nPosX = pt1.m_ptBR.m_nPosX;
	m_pt1.m_nPosY = pt1.m_ptLT.m_nPosY + (pt1.m_ptBR.m_nPosY - pt1.m_ptLT.m_nPosY) / 2;
	m_pt2.m_nPosX = pt1.m_ptLT.m_nPosX + (pt1.m_ptBR.m_nPosX - pt1.m_ptLT.m_nPosX) / 2;
	m_pt2.m_nPosY = pt1.m_ptBR.m_nPosY;
	m_ptLT = pt1.m_ptLT;
	m_ptBR = pt1.m_ptBR;

}
Comgraphics::Comgraphics(const Comgraphics& rc) {
	m_pt1 = rc.m_pt1;
	m_pt2 = rc.m_pt2;
	m_ptBR = rc.m_ptBR;
	m_ptLT = rc.m_ptLT;
}
Comgraphics::Comgraphics(const CPoint pt1) {
	m_ptLT = CPoint(pt1.m_nPosX, pt1.m_nPosY);
	m_ptBR = CPoint(pt1.m_nPosX + 60, pt1.m_nPosY + 80);
}
Comgraphics::~Comgraphics() {
	cout << "Comgraphics::~Comgraphics()" << endl;

}
double Comgraphics::GetArea()  const {
	return 0.0;
}
bool Comgraphics::ptIn(const CPoint& pt) const {
	return (pt.m_nPosX >= m_ptLT.m_nPosX && pt.m_nPosX <= m_ptBR.m_nPosX) &&
		(pt.m_nPosY >= m_ptLT.m_nPosY && pt.m_nPosY <= m_ptBR.m_nPosY);
}
bool Comgraphics::InRect(const CRect& rc) const const {
	return rc.ptIn(m_ptLT) && rc.ptIn(m_ptBR);
}
void Comgraphics::Draw() const {
	// 存储n个顶点的x,y坐标
	int pts[10] = { m_ptLT.m_nPosX,m_ptLT.m_nPosY,m_ptBR.m_nPosX,m_ptLT.m_nPosY,
	m_ptBR.m_nPosX,m_ptBR.m_nPosY,m_ptLT.m_nPosX,m_ptBR.m_nPosY,m_ptLT.m_nPosX,m_ptLT.m_nPosY };
	// 绘制n个顶点的多边形,第一个参数必须要传入n+1,pts最后一个顶点坐标和第一个相同
	//drawpoly(5, pts);
	setfillcolor(GREEN);
	fillpoly(5, pts);
	line(m_pt1.m_nPosX, m_pt1.m_nPosY, m_pt2.m_nPosX, m_pt2.m_nPosY);
	line(m_ptLT.m_nPosX, m_ptLT.m_nPosY, m_pt2.m_nPosX, m_pt2.m_nPosY);
	line(m_pt1.m_nPosX, m_pt1.m_nPosY, m_ptLT.m_nPosX, m_ptLT.m_nPosY);
}
void Comgraphics::DrawColor() {
	// 存储n个顶点的x,y坐标
	int pts[10] = { m_ptLT.m_nPosX,m_ptLT.m_nPosY,m_ptBR.m_nPosX,m_ptLT.m_nPosY,
	m_ptBR.m_nPosX,m_ptBR.m_nPosY,m_ptLT.m_nPosX,m_ptBR.m_nPosY,m_ptLT.m_nPosX,m_ptLT.m_nPosY };
	// 绘制n个顶点的多边形,第一个参数必须要传入n+1,pts最后一个顶点坐标和第一个相同
	setfillcolor(YELLOW);
	fillpoly(5, pts);
	line(m_pt1.m_nPosX, m_pt1.m_nPosY, m_pt2.m_nPosX, m_pt2.m_nPosY);
	line(m_ptLT.m_nPosX, m_ptLT.m_nPosY, m_pt2.m_nPosX, m_pt2.m_nPosY);
	line(m_pt1.m_nPosX, m_pt1.m_nPosY, m_ptLT.m_nPosX, m_ptLT.m_nPosY);
}
Shape* Comgraphics::Clone() const {
	return new Comgraphics(*(this));
}

Shape& Comgraphics::Move(int nOffsetX, int nOffsetY) {
	m_ptLT.Move(nOffsetX, nOffsetY);
	m_ptBR.Move(nOffsetX, nOffsetY);
	m_pt1.Move(nOffsetX, nOffsetY);
	m_pt2.Move(nOffsetX, nOffsetY);
	return *this;
}

#include<vector>
#include "graphics.h"
#include<iostream>
#include "Shape.h"
#include<dos.h>
using namespace std;

int main()
{
	//图形画布基础设置
	initgraph(640, 480);
	setbkcolor(WHITE);
	delay_ms(0);
	setcolor(BLACK);
	setfont(20, 0, "楷体");
	setbkmode(TRANSPARENT);
	//enter+左击-->新建矩形");
	//enter+右击-->新建三角形");
	//enter+滚轮中间-->新建组合图形

	//ctrl+左击-->复制图形");
	//ctrl+右击-->粘贴图形");

	vector<CShape*>shapes;
	vector<CShape*>shapestmp;

	shapes.push_back(new CTriangle(CPoint(320, 320), CPoint(250, 340), CPoint(340, 450)));
	//shapes.push_back(new CTriangle(CPoint(10, 10), CPoint(150, 10), CPoint(150, 150)));
	shapes.push_back(new CRect(CPoint(200, 200), CPoint(300, 300)));
	shapes.push_back(new Comgraphics(CRect(CPoint(250, 50))));


	//移动
	bool move_flag = false;
	bool copy_flag = false;
	bool redraw = true;
	//鼠标点击时记录它的坐标
	int clickX, clickY;
	int copyX, copyY;
	int checkedid = -1;
	int copyid = -1;

	for (; is_run(); delay_fps(60)) {
		while (mousemsg()) {
			mouse_msg msg = getmouse();

			//判断鼠标的移动
			if (msg.is_move()) {
				if (checkedid != -1) {
					if (move_flag) {
						shapes[checkedid]->Move(msg.x - clickX, msg.y - clickY);
					}
				}
				clickX = msg.x;
				clickY = msg.y;
				redraw = true;
			}

			// 判断鼠标左键
			else if (msg.is_left()) {
				// 判断鼠标左键是否按下
				if (msg.is_down()) {
					clickX = msg.x;
					clickY = msg.y;

					CPoint pt = CPoint(clickX, clickY);
					int isIn = 0;
					for (int i = 0; i < shapes.size(); i++) {
						if (shapes[i]->ptIn(pt)) {
							isIn = 1;
							//如果鼠标在图形区域内就设置移动的flag为true
							move_flag = true;
							checkedid = i;
							redraw = true;
							break;
						}
					}
					if (isIn == 0)
						checkedid = -1;
				}
				else {
					move_flag = false;
				}
			}
		}
		// 重新绘图
		if (redraw) {
			redraw = false;
			cleardevice();
			for (int i = 0; i < shapes.size(); i++) {
				if (i == checkedid)
					shapes[i]->DrawColor();
				else
					shapes[i]->Draw();
			}
		}

		while (kbmsg()) {
			key_msg msgk = getkey();
			if (msgk.key == key_enter && msgk.msg == key_msg_down) {
				mouse_msg msgm = getmouse();
				if (msgm.is_left()) {
					// 判断鼠标左键是否按下
					if (msgm.is_down()) {
						shapes.push_back(new CRect(CPoint(msgm.x, msgm.y)));
						redraw = true;
					}
				}
				if (msgm.is_right()) {
					// 判断鼠标右键是否按下
					if (msgm.is_down()) {
						shapes.push_back(new CTriangle(CPoint(msgm.x, msgm.y)));
						redraw = true;
					}
				}
				if (msgm.is_mid()) {
					CRect r1 = CRect(CPoint(msgm.x, msgm.y));
					// 判断鼠标中键是否按下
					if (msgm.is_down()) {
						shapes.push_back(new Comgraphics(r1));
						redraw = true;
					}
				}
			}
			if (msgk.key == key_control && msgk.msg == key_msg_down) {
				mouse_msg msgm = getmouse();
				if (msgm.is_left()) {
					// 判断鼠标左键是否按下
					if (msgm.is_down()) {
						copyX = msgm.x;
						copyY = msgm.y;
						CPoint pt = CPoint(copyX, copyY);
						for (int i = 0; i < shapes.size(); i++) {
							if (shapes[i]->ptIn(pt)) {
								//如果鼠标在图形区域内就设置移动的flag为true
								copy_flag = true;
								copyid = i;
								break;
							}
						}
					}
				}
				if (msgm.is_right()) {
					// 判断鼠标右键是否按下
					if (msgm.is_down()) {
						if (copy_flag == true) {
							shapes.push_back(&(shapes[copyid]->Clone())->Move(msgm.x - copyX, msgm.y - copyY));
							redraw = true;
						}
					}
				}

			}
		}
	}
	closegraph();
	return 0;
}

出现的错误

1、内存越界
在这里插入图片描述
将类class A改为class base解决错误,应该是在另外一个cpp文件中也定义了A导致出错。

2、存在多个cpp文件时,会存在多个main函数,使得运行时会报错
解决:可以将没用到的cpp文件的main函数注释掉

总结

virtual:在类中加了Virtual关键字的函数就是虚拟函数.
虚函数是指一个类中你希望重载的成员函数 ,当你用一个基类指针或引用指向一个继承类对象的时候,调用一个虚函数时, 实际调用的是继承类的版本,它可以在运行时判断指针指向的对象,并自动调用相应的函数.
虚函数最关键的特点是“动态联编”,它可以在运行时判断指针指向的对象,并自动调用相应的函数。

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-10-27 12:39:41  更:2021-10-27 12:40:18 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 5:17:16-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码