CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(Share)
set(CMAKE_CXX_STANDARD 11)
include_directories( ./include )
add_executable(Share src/main.cpp src/AbstractShape.cpp include/AbstractShape.h src/Triangle.cpp include/Triangle.h src/Rectangle.cpp include/Rectangle.h)
AbstractShape.h
//
// Created by oceanstar on 2020/8/3.
//
#ifndef SHARE_ABSTRACTSHAPE_H
#define SHARE_ABSTRACTSHAPE_H
/**
* 抽象形状类
*/
class AbstractShape {
private:
//私有字段
int edge;
public:
//构造函数
AbstractShape(int edge);
//实例方法,子类继承后可以重用
int getEdge();
//纯虚函数,父类没有实现,调用时只会调用子类的实现
virtual int calcArea()=0;
};
#endif //SHARE_ABSTRACTSHAPE_H
AbstractShape.cpp
//
// Created by oceanstar on 2020/8/3.
//
#include "AbstractShape.h"
AbstractShape::AbstractShape(int edge)
{
this->edge = edge;
}
int AbstractShape::getEdge()
{
return this->edge;
}
Rectangle.h
//
// Created by oceanstar on 2020/8/3.
//
#ifndef SHARE_RECTANGLE_H
#define SHARE_RECTANGLE_H
#include "AbstractShape.h"
/**
* 矩形类,继承自形状类
*/
class Rectangle: public AbstractShape
{
private:
//私有字段
int bottom;
int height;
public:
//构造函数
Rectangle(int bottom, int height);
//重写父类同名方法,用于实现多态性
int calcArea();
void UniqueFunc();
};
#endif //SHARE_RECTANGLE_H
Rectangle.cpp
//
// Created by oceanstar on 2020/8/3.
//
#include
#include "Rectangle.h"
Rectangle::Rectangle(int bottom, int height) :AbstractShape(4)
{
this->bottom = bottom;
this->height = height;
}
int Rectangle::calcArea()
{
return this->bottom * this->height;
}
void Rectangle::UniqueFunc(){
std::cout << "UniqueFunc()\n";
}
Triangle.h
//
// Created by oceanstar on 2020/8/3.
//
#include "AbstractShape.h"
#ifndef SHARE_TRIANGLE_H
#define SHARE_TRIANGLE_H
class Triangle : public AbstractShape{
private:
//私有字段
int bottom;
int height;
public:
//构造函数
Triangle(int bottom, int height);
//重写父类同名方法,用于实现多态性
int calcArea();
};
#endif //SHARE_TRIANGLE_H
Triangle.cpp
//
// Created by oceanstar on 2020/8/3.
//
#include "Triangle.h"
Triangle::Triangle(int bottom, int height) :AbstractShape(3)
{
this->bottom = bottom;
this->height = height;
}
int Triangle::calcArea()
{
return this->bottom * this->height / 2;
}
main中使用:
Triangle triangle = Triangle(4, 5);
cout << triangle.getEdge() << ",\t";
cout << triangle.calcArea() << endl;
Rectangle rectangle = Rectangle(4, 5);
cout << rectangle.getEdge() << ",\t";
cout << rectangle.calcArea() << endl;
rectangle.UniqueFunc();
//---------------上转---------------------
AbstractShape *pShape = NULL; //定义了一个抽象类的指针,注意抽象类不能定义对象但是可以定义指针
pShape = new Triangle(4, 5); //基类指针指向派生类的对象
cout << pShape->getEdge() << ",\t";
cout << pShape->calcArea() << endl;
delete pShape;//释放了CCirle对象所占的内存,但是指针是没有消失的,它现在就是一个野指针,我们在使用之前必须对它赋值
pShape = &rectangle; //基类指针指向派生类的对象
cout << pShape->getEdge() << ",\t";
cout << pShape->calcArea() << endl;
// 可以看到,我们使用父类的指针调用同一个函数,分别调用了这两个派生类的对应函数,它根据指针指向的类型的不同来决定调用的方法。即使我们以后需要新增加几个类,我们还是这种调用方法,这就是多态的巨大魅力。
//---------------上转---------------------
AbstractShape *pShape = NULL; //定义了一个抽象类的指针,注意抽象类不能定义对象但是可以定义指针
Rectangle *rectangle_bynew = new Rectangle(4, 5);
cout << rectangle_bynew->getEdge() << ",\t";
cout << rectangle_bynew->calcArea() << endl;
rectangle_bynew->UniqueFunc();
pShape = rectangle_bynew;
cout << pShape->getEdge() << ",\t";
cout << pShape->calcArea() << endl;
delete rectangle_bynew;
AbstractShape *pShape_1 = new Rectangle(4, 5);
Rectangle *rectangle_cast = dynamic_cast<Rectangle*>(pShape_1);
if (rectangle_cast != nullptr)
{
cout << "The radius is "
<< rectangle_cast->getEdge() << endl;
}
等待:
http://blog.chinaunix.net/uid-26921272-id-3360269.html
https://blog.csdn.net/m0_38015368/article/details/71336552
https://www.cnblogs.com/welen/articles/3427379.html
https://blog.csdn.net/lsky380/article/details/102502945
https://blog.csdn.net/weixin_42043407/article/details/106883052
https://blog.csdn.net/shanshenyuyou/article/details/95202112