继承访问权限测试
- 设计类A具有public, protected, private等不同属性的成员函数或变量
class base {
public:
int q=10;
protected:
int w = 11;
private:
int e = 12;
};
-
实现类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;
}
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;
}
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;
}
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;
}
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;
};
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;
A a1;
a1.c;
}
int a;
};
3、设计类D继承C,在D的成员函数中测试访问类A的成员变量a,类B的成员变量a, b。
class D :public C {
public:
void Test() {
A a1;
a1.a;
B b2;
b2.a;
}
};
多态的综合性运用
一般多态性函数:输入输出参数完全一样,在父类中添加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;
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(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() {
}
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(const CTriangle& tri) {
for (int i = 0; i < 3; i++) {
m_pts[i] = tri.m_pts[i];
}
}
CTriangle::~CTriangle() {
}
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(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() {
}
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 {
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 };
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 };
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(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 {
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 };
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() {
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 };
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);
vector<CShape*>shapes;
vector<CShape*>shapestmp;
shapes.push_back(new CTriangle(CPoint(320, 320), CPoint(250, 340), CPoint(340, 450)));
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;
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)) {
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关键字的函数就是虚拟函数. 虚函数是指一个类中你希望重载的成员函数 ,当你用一个基类指针或引用指向一个继承类对象的时候,调用一个虚函数时, 实际调用的是继承类的版本,它可以在运行时判断指针指向的对象,并自动调用相应的函数. 虚函数最关键的特点是“动态联编”,它可以在运行时判断指针指向的对象,并自动调用相应的函数。
|