C++继承多态的小例子

C++继承多态的小例子,第1张

C++继承多态的小例子

几何类(父类)

#ifndef GeometricObject_hpp
#define GeometricObject_hpp

#include 
#include 
using namespace::std;
class GeometricObject{
protected:
    GeometricObject();
    GeometricObject(const string& color,bool filled);
public:
    string getColor();
    void setColor(const string& color);
    bool isFilled();
    void setFilled(bool filled);
    virtual string toString();
    virtual double getArea() const = 0;
    virtual double getPerimeter() const = 0;
private:
    string color;
    bool filled;
};
#endif 
#include "GeometricObject.hpp"
GeometricObject::GeometricObject(){
    color = "white";
    filled = false;
}
GeometricObject::GeometricObject(const string& color,bool filled){
    setColor(color);
    setFilled(filled);
}
string GeometricObject::getColor(){
    return color;
}
void GeometricObject::setColor(const string &color){
    this->color = color;
}
bool GeometricObject::isFilled(){
    return filled;
}
void GeometricObject::setFilled(bool filled){
    this->filled=filled;
}
string GeometricObject::toString(){
    return "Geometric Object";
}

圆和矩形子类

#include "Circle.hpp"
Circle::Circle(){
    radius = 1;
}
Circle::Circle(double radius){
    setRadius(radius);
}
Circle::Circle(double radius,const string& color,bool filled){
    setColor(color);
    setRadius(radius);
    setFilled(filled);
}
double Circle::getRadius(){
    return radius;
}
void Circle::setRadius(double radius){
    this->radius=(radius>=0)? radius : 0;
}
double Circle::getArea()const{
    return radius*radius*3.14;
}
double Circle::getPerimeter()const{
    return 2.0*3.14*radius;
}
double Circle::getDiameter(){
    return 2.0*radius;
}
string Circle::toString(){
    return "我是一个圆";
}
#ifndef Circle_hpp
#define Circle_hpp
#include "GeometricObject.hpp"
#include 
class Circle:public GeometricObject
{
public:
    Circle();
    Circle(double);
    Circle(double radius,const string& color,bool filled);
    double getRadius();
    void setRadius(double);
    double getArea()const;
    double getPerimeter()const;
    double getDiameter();
    string toString();
private:
    double radius;
};
#endif 
#ifndef Rectangle_hpp
#define Rectangle_hpp

#include 
#include "GeometricObject.hpp"
class Rectangle:public GeometricObject{
public:
    Rectangle();
    Rectangle(double width,double height);
    Rectangle(double width,double height,const string& color,bool filled);
    double getWidth();
    void setWidth(double);
    double getHeight();
    void setHeight(double);
    double getArea()const;
    double getPerimeter()const;
    string toString();
private:
    double width,height;
};
#endif 
#include "Rectangle.hpp"
Rectangle::Rectangle(){
    width = 1;
    height = 1;
}
Rectangle::Rectangle(double width,double height){
    setWidth(width);
    setHeight(height);
}
Rectangle::Rectangle(double width,double height,const string& color,bool filled){
    setWidth(width);
    setHeight(height);
    setColor(color);
    setFilled(filled);
}
double Rectangle::getWidth(){
    return width;
}
void Rectangle::setWidth(double width){
    this->width=(width>=0)?width:0;
}
double Rectangle::getHeight(){
    return height;
}
void Rectangle::setHeight(double height){
    this->height=(height>=0)?height:0;
}
double Rectangle::getArea()const{
    return width*height;
}
double Rectangle::getPerimeter()const{
    return (width+height)*2;
}
string Rectangle::toString(){
    return "我是一个矩形";
}

测试

//C++中父类是抽象类,子类继承父类必须将所有抽象函数功能全部实现(纯虚函数全部实现),否则子类也将是一个抽象类
//虚函数有函数体,而纯虚函数在基类中没有函数体
//虚函数中,对象的引用必须用指针或者引用的形式传递
//变量的类型声明决定编译时匹配哪个函数称为静态绑定,变量的实际类型所决定的被称为动态绑定
//proteced行可以被派生类中访问,private只能在本类中访问
#include 
#include "GeometricObject.hpp"
#include "Circle.hpp"
#include "Rectangle.hpp"
using namespace::std;
bool equalArea(const GeometricObject& g1,const GeometricObject& g2){
    return g1.getArea()==g2.getArea();
}
void displayGeometriObject(const GeometricObject& g){
    cout<<"面积为"< 

结果

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4948896.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存