继承与多态(C++第二次实验)
1、继承访问权限测试:
1.1 继承的概念
面向对象程序设计中最重要的一个概念是继承。继承允许我们依据另一个类来定义一个类,这使得创建和维护一个应用程序变得更容易。这样做,也达到了重用代码功能和提高执行效率的效果。
当创建一个类时,您不需要重新编写新的数据成员和成员函数,只需指定新建的类继承了一个已有的类的成员即可。这个已有的类称为基类,新建的类称为派生类。
继承代表了 is a 关系。例如,哺乳动物是动物,狗是哺乳动物,因此,狗是动物,等等。
1.2 设计类A具有public, protected, private等不同属性的成员函数或变量
代码:
class Person {
public:
string _name;
protected:
string _sex;
private:
int _age;
};
这里新建了一个名为Person的类,public、protected和private分别带有一个变量。
1.3 类B通过public, protected, private等不同方式继承A,在类B的成员函数中测试访问A的成员函数或变量
代码:
class Pub_Student : public Person { //public方式继承
public:
void Test() {
_name = "雷文晖";
_sex = "男";
}
string _pubCn;
protected:
string _pubSn;
private:
int _pubGrade;
};
class pro_Student : protected Person { //protected方式继承
public:
void Test() {
_name = "雷文晖";
_sex = "男";
}
string proCn;
protected:
string proSn;
private:
int proGrade;
};
class pri_Student : private Person { //private方式继承
public:
void Test() {
_name = "雷文晖";
_sex = "男";
}
string _priCn;
protected:
string _priSn;
private:
int _priGrade;
};
1.4 在类B中添加public, protected, private等不同属性的成员函数或变量,在外部测试访问B的各个成员函数或变量
class Pub_Student : public Person { //public方式继承
public:
void Test() {
_name = "雷文晖";
_sex = "男";
}
string _pubCn;
protected:
string _pubSn;
private:
int _pubGrade;
};
class pro_Student : protected Person { //protected方式继承
public:
void Test() {
_name = "雷文晖";
_sex = "男";
}
string _proCn;
protected:
string _proSn;
private:
int _proGrade;
};
class pri_Student : private Person { //private方式继承
public:
void Test() {
_name = "雷文晖";
_sex = "男";
}
string _priCn;
protected:
string _priSn;
private:
int _priGrade;
};
测试代码:
int main()
{
Person a;
cout<<a._name;
//cout << a._age; _age为private 不可访问
//cout << a._sex; _sex为protected 不可访问
PubStudent b1;
cout << b1._pubCn;
b1.Test();
cout << b1._name;
//cout << b1._sex; _sex为protected 不可访问
//cout << b1._age; _age为private 不可访问
//cout << b1._pubSn; _pubSn为protected 不可访问
//cout << b1._pubGrade; pubGrade为protected 不可访问
ProStudent b2;
cout << b2._proCn;
b2.Test();
//cout << b2._name; _name由public被降级为protected 不可访问
//cout << b2._sex; _sex为protected 不可访问
//cout << b2._age; _age为private 不可访问
//cout << b2._proSn; _pubSn为protected 不可访问
//cout << b2._proGrade; pubGrade为protected 不可访问
PriStudent b3;
cout << b3._priCn;
b3.Test();
//cout << b3._name; _name由public被降级为private 不可访问
//cout << b3._sex; _sex由protected被降级为private 不可访问
//cout << b3._age; _age为private 不可访问
//cout << b3._priSn; _pubSn为protected 不可访问
//cout << b3._priGrade; pubGrade为protected 不可访问
}
1.5 B以private方式继承A,尝试把A中的部分public成员提升为public
代码:
class PriStudent : private Person {
public:
void Test() {
_name = "雷文晖";
_sex = "男";
}
string _priCn;
using Person::_name;
protected:
string _priSn;
private:
int _priGrade;
};
只需要添加一句“using Person::_name”便可以把继承过来的基类public变量升级为public
如果想让这些继承而来的数据成员作为public或者protected成员,可以用using重新声明
using声明语句中名字的访问权限由该using声明语句之前的访问说明符决定
2、友元类继承测试
概念
友元关系不能继承。基类的友元对派生类的成员没有特殊访问权限。
如果基类被授予友元关系,则只有基类具有特殊访问权限,该基类的派生类不能访问授予友元关系的类。
友元关系是单向的,不具有交换性。友元关系也不具有传递性。
2.1 设计类A含有私有变量a,在类A中友元给类C
class Person {
private:
string _name;
friend class Teacher;
};
这里有一个类Person,友元给类Teacher
2.2 设计类B继承A,添加私有变量b;在类C中测试访问类B的成员变量a, b
class Student : public Person {
private:
int _grade;
};
class Teacher {
public:
void Test() {
Student b1;
b1._name;
//b1._grade;
}
};
2.3 设计类D继承C,在D的成员函数中测试访问类A的成员变量a,类B的成员变量a, b
class Mr_W :public Teacher {
public:
void Test() {
Person a1;
//a1._name; _name为private,不可访问
Student a2;
//a2._name; _name为private,不可访问
//a2._grade; _grade为private,不可访问
}
};
3、多态性综合运用
3.1 多态性的概念
在面向对象方法中,所谓多态性就是不同对象收到相同消息,产生不同的行为。在C++程序设计中,多态性是指用一个名字定义不同的函数,这些函数执行不同但又类似的操作,这样就可以用同一个函数名调用不同内容的函数。换言之,可以用同样的接口访问功能不同的函数,从而实现“一个接口,多种方法”。
事实上,在程序设计中经常会使用到多态性。最简单的例子就是运算符了,例如我们使用运算符+,就可以实现整型数、浮点数、双精度类型之间的加法运算,这三种类型的加法操作其实是互不相同的,是由不同内容的函数实现的。这个例子就是使用了多态的特征。
静态联编支持的多态性称为编译时多态性(静态多态性)。在C++中,编译时多态性是通过函数重载和模板实现的。利用函数重载机制,在调用同名函数时,编译系统会根据实参的具体情况确定索要调用的是哪个函数。
动态联编所支持的多态性称为运行时多态(动态多态)。在C++中,运行时多态性是通过虚函数来实现的。
3.2 多态性综合应用
前期准备: vs2019和xege
具体安装教程这里推荐看此博主的:
EGE的介绍、安装和使用
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;
}
运行截图: