C/C++教程

【C++】面向对象(多态)

本文主要是介绍【C++】面向对象(多态),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

C++面向对象编程 多态

  • 1.理解多态
    • (一)多态的概念
    • (二)虚函数概念
    • (三)多态的意义
  • 2.编程加深对多态的理解
    • (一)一般多态性
    • (二)特殊多态性函数
    • (三)析构函数的多态性
    • (四)多继承
    • (五)多继承实例
      • CShape.h
      • CShape.cpp
      • Main.cpp

1.理解多态

(一)多态的概念

1、根据实际的对象类型决定函数调用的具体目标;
2、同样的调用语句在实际运行时有多种不同的表现形态体现,这就是多态,同一语句具有多种形态。函数的重载、运算符的重载都是多态现象。
3.在C++中,多态性表现形式之一是:具有不同功能的函数可以用同一个函数名,这样就可以实现用一个函数名调用不同内容的函数。
4.从系统实现的角度来看,多态性分为两类:静多态性和动多态性,静多态性是通过函数重载实现的,动多态性是通过虚函数实现的。

(二)虚函数概念

C++中的虚函数就是用来解决动态多态问题的,所谓虚函数,就是在基类声明函数是虚拟的,并不是实际存在的,然后在派生类中才正式定义此函数,在程序运行期间,用指针指向某一派生类对象,这样就能调用指针指向的派生类对象中的函数,而不对调用其他派生类中的函数。

(三)多态的意义

1、在程序运行中展现出动态的特性;
2、函数重写必须多态实现,否则没有任何意义;
3、多态是面向对象组件化程序设计的基础特性。

2.编程加深对多态的理解

(一)一般多态性

新建CAnimal类,Move()是其内部虚函数,新建CCat类、CEagle类、COwl类继承CAnimal类,并重写其内部的函数Move()的实现方法以实现多态的效果。
代码:

#include <iostream>  
using namespace std;
class CAnimal
{
public:
    CAnimal() {
        m_nLegs = 8;
    }
    CAnimal(int nLeg) {
        m_nLegs = nLeg;
    }
	 void Move() {
		cout << "animal has many legs" << endl;
	};
protected:
	int m_nLegs;
};

class CCat : virtual public CAnimal {
public:
    CCat() {
        m_nLegs = 4;
    }
    CCat(int nLegs) {
        m_nLegs = nLegs;
    }
	void Move() {
		cout << "cat has four legs" << endl;
	};
};

class CEagle :virtual public CAnimal {
public:
    CEagle() {
        m_nLegs = 2;
    }
    CEagle(int nLegs) {
        m_nLegs = nLegs;
    }
	void Move() {
		cout << "eagle has two legs" << endl;
	};
};

class COwl : public CCat, public CEagle
{
public:
    //无参构造函数
    COwl() {
        m_nLegs = 2;
    }
    COwl(int nLegs) {
        m_nLegs = nLegs;
    }
    void Move() {
        cout << "owl has two legs" << endl;
    }
};

void TestAnimal() {
	CAnimal* Animal[4];
	Animal[0] = new CAnimal;
	Animal[1] = new CCat;
	Animal[2] = new CEagle;
    Animal[3] = new COwl;
	for(int i = 0; i < 4; i++)
	{
		Animal[i]->Move();
	}
}
int main()
{
	TestAnimal();
	return 0;
}

实验截图:
在这里插入图片描述
可以看到输出全部一样。

(二)特殊多态性函数

输入或输出参数在子类中是父类的指针或基类的引用,在子类中对于的是子类的指针或子类的引用
代码:

 void callMove(CAnimal* a) {
    a->Move();
}

void TestAnimal()
{
    CAnimal animal;
    CCat cat;
    CEagle eagle;
    COwl owl;
    callMove(&animal);
    callMove(&cat);
    callMove(&eagle);
    callMove(&owl);
}

int main()
{
	TestAnimal();
	return 0;
}

运行截图:
在这里插入图片描述
可以看到Move函数输出的都是一样的。
为了让每个子类能够输出自己类中重写的函数Move,则我们只需要在每个子类的父类Move 函数前加上virtual。即让父类的Move函数变为虚函数,告诉编译器它是虚函数,要进行晚绑定。
更改后的运行截图:
在这里插入图片描述

(三)析构函数的多态性

代码:

#include <iostream>
using namespace std;

class Base {
public:
    Base() { cout << "Base constructor... \n"; }
    ~Base() { cout << "Base destructor... \n"; }
    virtual void fun() const { cout << "Base functoin...\n"; }
};

class Derived :public Base {
public:
    Derived() {
        p = new int(0);
        cout << "Derived Constructor...\n";
    }
    ~Derived() {
        cout << "Derived destructor...\n";
        delete p;
    }
    void fun() const { cout << "Derived function...\n"; }
private:
    int* p;
};

int main() {
    Base* pd = new Derived;
    pd->fun();
    delete pd;
    return 0;
}

运行截图:
在这里插入图片描述
可以看到当指向基类的指针被释放时,派生类的析构函数其实没有被调用,造成在派生类中申请的空间没有被释放,从而导致内存泄漏。
为了防止在实际应用中造成这种情况,我们只需要将上述代码中的 ~Base() { cout << "Mammal destructor... \n";前面添上virtual,即通过将基类的析构函数声明为虚析构函数,成功的通过基类指针调用了派生类的析构函数,完成了内存的释放。

更改后的运行截图:
在这里插入图片描述

(四)多继承

c++不仅可以单继承,也可以多继承。一个类有多个基类,那么这种继承关系就叫做多继承。
多继承的声明:
class 派生类名:访问控制 基类名1, 访问控制 基类名2, …
{
成员列表
}

使用多继承会带来哪些问题
多继承比单继承复杂,也更容易出现问题。
多继承的两个主要问题是:
①从两个不同的基类,继承同名方法
②从多个基类间接继承同一个类的多个实例

class A
{
public:
	int a;
};

class B1 : public A 
{
public:
	int b1;
};

class B2 : public A
{
public:
	int b2;
};

class C :public B1, public B2
{
public:
	void fun()
	{
		int  i = a; 
	}
};

在这里插入图片描述
可以看到若B1、B2不虚继承会报错,因为多继承原因,C同时继承B1,B2,而B1、B2的基类又相同,则程序不知道是a是B1的还是B2的,此时只需要让B1,B2都虚继承A即可解决问题。

(五)多继承实例

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

CShape.h

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

class CPoint;
class CRect;
class CShape
{
public:
	CShape();
	CShape(const CShape& shape);
	virtual ~CShape();
	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 CShape* Clone() const;
	virtual CShape& Move(int nOffsetX, int nOffsetY);

protected:
	string m_sName;
};

class CPoint :public CShape {
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 CShape {
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();
	CShape* Clone() const;
	CShape& Move(int nOffsetX, int nOffsetY);
	CPoint m_pts[3];
};

class CRect :virtual public CShape {
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();
	CShape* Clone() const;
	CShape& 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();
	CShape* Clone() const;
	CShape& Move(int nOffsetX, int nOffsetY);
	CPoint m_pt1;
	CPoint m_pt2;

};
#endif

CShape.cpp

#include "CShape.h"
#include "graphics.h"
#include <iostream>
using namespace std;
//CShape
CShape::CShape()
{
}
CShape::CShape(const CShape& shape) {
	m_sName = shape.m_sName;
}
CShape::~CShape()
{
}
double CShape::GetArea() const {
	return 0;
}
bool CShape::ptIn(const CPoint& pt) const {
	return false;
}
bool CShape::InRect(const CRect& rc) const {
	return false;
}
void CShape::Draw() const
{
}
void CShape::DrawColor()
{
}
CShape* CShape::Clone() const {
	return new CShape(*this);
}
CShape& CShape::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;
}

CShape& 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);
}
CShape* 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);
}
CShape* CRect::Clone() const {
	return new CRect(*this);
}
CShape& 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);
}
CShape* Comgraphics::Clone() const {
	return new Comgraphics(*(this));
}

CShape& 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;
}

Main.cpp

#include<vector>
#include "graphics.h"
#include<iostream>
#include "CShape.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;
}

运行截图:
在这里插入图片描述

这篇关于【C++】面向对象(多态)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!