I. inheritance access test
1 code implementation
#ifndef CSHAPE_H
#define CSHAPE_H
class A
{
public:
int _a;
protected:
int _b;
private:
int _c;
};
class Pub_B :public A {
public:
void Test() {
_a = 10;
_b = 10;
//_ c = 10; Inaccessible
}
int pub_pub_b1;
protected:
int pub_pro_b2;
private:
int pub_pri_b3;
};
class Pro_B :protected A {
public:
void Test() {
_a = 10;
_b = 10;
//_ c = 10; Inaccessible
}
int pro_pub_b1;
protected:
int pro_pro_b2;
private:
int pro_pri_b3;
};
class Pri_B :private A {
public:
void Test() {
_a = 10;
_b = 10;
//_ c = 10; Inaccessible
}
int pri_pub_b1;
protected:
int pri_pro_b2;
private:
int pri_pri_b3;
};
class newPri_B :private A {
public:
void Test() {
_a = 10;
_b = 10;
//_ c = 10; Inaccessible
}
int pri_pub_b1;
using A::_a;
protected:
int pri_pro_b2;
private:
int pri_pri_b3;
};
#endif
2 Summary

- B inherits A in A private way, and uses {using A::_a;} to promote some private members in A to public
- Using redeclare. The access permission of the name in the using declaration statement is determined by the access specifier before the using declaration statement.

- Conversion of inaccessible base class A is not allowed
Binary class inheritance test
1 code implementation
#include<istream>
using namespace std;
class A {
private:
int _a;
friend class C;
};
class B: public A {
private:
int _b;
};
class C {
public:
void Test() {
B b1;
b1._a;
//b1._b; Inaccessible
}
};
class D :public C {
public:
void Test() {
A a1;
//a1._a; Inaccessible
B b2;
//b2._a; Inaccessible
//b2._b; Inaccessible
}
};
2 Summary
- When class C is described as a friend of class A, all member functions in friend class C can access all members (including private members) in class A.
- Class B inherits class A, but class A is a friend of class C, and class B is not a friend of class C; Class D inherits class C, but class A is a friend of class C, and class A is not a friend of class D. That is, friends are not inherited.
Comprehensive application of three polymorphisms
1 code implementation
1 main.cpp
#include<vector>
#include "graphics.h"
#include<iostream>
#include "CShape.h"
using namespace std;
int main()
{
//Graphics canvas basic settings
initgraph(640, 480);
setbkcolor(WHITE);
delay_ms(0);
setcolor(BLACK);
setfont(20, 0, "Regular script");
setbkmode(TRANSPARENT);
//enter + left click -- > new rectangle "");
//enter + right click -- > New Triangle "");
//enter + middle of scroll wheel -- > new composite graphics
//ctrl + left click -- > copy graphic "");
//ctrl + right click -- > Paste graphic "");
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))));
//move
bool move_flag = false;
bool copy_flag = false;
bool redraw = true;
//Record its coordinates when the mouse clicks
int clickX, clickY;
int copyX, copyY;
int checkedid = -1;
int copyid = -1;
for (; is_run(); delay_fps(60)) {
while (mousemsg()) {
mouse_msg msg = getmouse();
//Judge the movement of the mouse
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;
}
// Left mouse button
else if (msg.is_left()) {
// Judge whether the left mouse button is pressed
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;
//If the mouse is in the graphics area, set the moving flag to true
move_flag = true;
checkedid = i;
redraw = true;
break;
}
}
if (isIn == 0)
checkedid = -1;
}
else {
move_flag = false;
}
}
}
// Redrawn
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()) {
// Judge whether the left mouse button is pressed
if (msgm.is_down()) {
shapes.push_back(new CRect(CPoint(msgm.x, msgm.y)));
redraw = true;
}
}
if (msgm.is_right()) {
// Judge whether the right mouse button is pressed
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));
// Determine whether the middle mouse button is pressed
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()) {
// Judge whether the left mouse button is pressed
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)) {
//If the mouse is in the graphics area, set the moving flag to true
copy_flag = true;
copyid = i;
break;
}
}
}
}
if (msgm.is_right()) {
// Judge whether the right mouse button is pressed
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;
}
2.CShape.h
#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 {
// Store the x,y coordinates of n vertices
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 };
// To draw a polygon with n vertices, the first parameter must be passed in n+1, pts and the coordinates of the last vertex are the same as the first
//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 };
// To draw a polygon with n vertices, the first parameter must be passed in n+1, pts and the coordinates of the last vertex are the same as the first
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 {
// Store the x,y coordinates of n vertices
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 };
// To draw a polygon with n vertices, the first parameter must be passed in n+1, pts and the coordinates of the last vertex are the same as the first
//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() {
// Store the x,y coordinates of n vertices
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 };
// To draw a polygon with n vertices, the first parameter must be passed in n+1, pts and the coordinates of the last vertex are the same as the first
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;
}
3.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 {
// Store the x,y coordinates of n vertices
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 };
// To draw a polygon with n vertices, the first parameter must be passed in n+1, pts and the coordinates of the last vertex are the same as the first
//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 };
// To draw a polygon with n vertices, the first parameter must be passed in n+1, pts and the coordinates of the last vertex are the same as the first
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 {
// Store the x,y coordinates of n vertices
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 };
// To draw a polygon with n vertices, the first parameter must be passed in n+1, pts and the coordinates of the last vertex are the same as the first
//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() {
// Store the x,y coordinates of n vertices
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 };
// To draw a polygon with n vertices, the first parameter must be passed in n+1, pts and the coordinates of the last vertex are the same as the first
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;
}
2 operation results

3 Summary
- The configuration of ege graphic line library in VS only needs to download ege and replace the include and lib in VS Software with the include and lib in ege.
- EGE in the EGE graphic line library is the abbreviation of easy graphics engine. It is a simple drawing library under windows. It is a graphic library similar to BGI(graphics.h) for novices in C/C + + language. Its goal is to replace TC's BGI library. Its usage is quite similar to that of graphics.h in TC.
- Virtual inheritance is required when diamond inheritance (class A derives from class B and class C, and class D inherits from class B and class C). Otherwise, ambiguity will occur when accessing A's member variables.
- When C + + is polymorphic, we often encounter a situation that when the pointer to the base class is released, the destructor of the derived class is not called, resulting in the space applied in the derived class is not released, resulting in memory leakage. At this time, the virtual destructor can effectively prevent this situation.
- . special polymorphism function: the input or output parameters are the pointer of the parent class or the reference of the base class in the subclass, and the pointer of the subclass or the reference of the subclass in the subclass.
- General polymorphism function: the input and output parameters are exactly the same, and virtual is added to the parent class;