zoukankan      html  css  js  c++  java
  • [原]C++程序示例:涉及到抽象类、继承…

    C++复习题。
    有助于理解:
    1. .h与.cpp分离
    2. 基类、派生类、抽象类
    3. 多态、纯虚函数
    4. 对象指针、引用
    5. 派生类使用基类的构造函数
    6. 。。。
    ==================================================================================================
    t3.h

    #ifndef T3_H_INCLUDED
    #define T3_H_INCLUDED
    //此程序定义一个Shape类。矩形,圆形从Shape继承。矩形可以计算面积,改变大小,比较大小。改变大小,比较大小。
    //继承、虚函数、重载
    //protected成员可以被派生类直接访问,private成员不可以被派生类访问
    //涉及析构
    class Shape{
    protected:
    float length;
    float width;
    float radius;
    public:
    Shape();
    Shape(float r);
    Shape(float l,float w);
    ~Shape();

    float getRadius();
    float getWidth();
    float getLength();

    virtual void display()=0;
    virtual void changeSize(float l,float w)=0;
    virtual double getArea()=0;
    virtual bool compareSize(Shape* s0)=0; 
    };

    class Rectangular:public Shape{
    public:
    Rectangular():Shape(){}
    Rectangular(float l,float w):Shape(l,w){}
    void display();
    void changeSize(float l,float w);
    double getArea();
    bool compareSize(Shape* s0); 
    };

    class Circle:public Shape{
    public:
    Circle():Shape(){}
    Circle(float r):Shape(r){}
    void display();
    void changeSize(float l,float w);
    double getArea();
    bool compareSize(Shape* s0); 
    };

    #endif 

    ==================================================================================================

    t3.cpp

    #include
    #include "t3.h"
    using namespace std;

    Shape::Shape(){length=0;width=0;radius=0;}
    Shape::Shape(float r){radius=r;}
    Shape::Shape(float l,float w){length=l;width=w;}
    Shape::~Shape(){
    cout<<"I was dead."<<endl;
    }

    float Shape::getRadius(){
    return radius;
    }
    float Shape::getLength(){
    return length;
    }
    float Shape::getWidth(){
    return width;
    }
    //Shape end
    void Rectangular::display(){
    cout<<"I'm a rectangular."<<endl;
    }
    void Rectangular::changeSize(float l,float w){
    length=length+l;
    width=width+w;
    }
    double Rectangular::getArea(){
    return length*width;
    }
    bool Rectangular::compareSize(Shape* s0){
    return this->getArea()>s0->getArea();
    }
    //Rectangular end
    void Circle::display(){
    cout<<"I'm a circle."<<endl;
    }
    void Circle::changeSize(float l,float w=0){
    radius=radius+l;
    }
    double Circle::getArea(){
    return 3.14*radius*radius;
    }
    bool Circle::compareSize(Shape* s0){
    return this->getArea()>s0->getArea();
    }
    //Circle end
    int main(){
    Rectangular r1=Rectangular();
    r1.display();
    cout<<"My area is:"<<r1.getArea()<<endl;
    r1.changeSize(5,6);
    r1.display();
    cout<<"My area is:"<<r1.getArea()<<endl;
    Rectangular r2=Rectangular(3,4);
    r2.display();
    cout<<"My area is:"<<r2.getArea()<<endl;
    Circle c1=Circle();
    c1.display();
    cout<<"My area is:"<<c1.getArea()<<endl;
    c1.changeSize(5);
    c1.display();
    cout<<"My area is:"<<c1.getArea()<<endl;
    Circle c2=Circle(6);
    c2.display();
    cout<<"My area is:"<<c2.getArea()<<endl;
    Shape *p;
    p=&c2;
    cout<<r2.compareSize(p)<<endl;;
    }
    作者:gcy77 发表于2014-3-12 15:16:28 原文链接
    阅读:77 评论:0 查看评论
  • 相关阅读:
    es6 Set 和Map 数据结构
    es6 Symbol
    es6 对象的扩展
    es6 class
    es6 数组扩展方法
    Docker入门01——Image
    GORM 中文文档
    将以前的文章开始慢慢转到这里发表
    环境变量
    在 Linux 中安装 VMware Tools
  • 原文地址:https://www.cnblogs.com/gcy77/p/4082492.html
Copyright © 2011-2022 走看看