从一个简单例子 到 虚函数(摘抄)

一个简单的Point类

#include <iostream>
#include <assert.h>
#include <string.h>


using namespace std;


class Point
{
public:
    Point(float x=0,float y=0);
    void setPoint(float,float);
    float getX()const{return x;}
    float getY()const{return y;}
    friend ostream & operator<<(ostream &os,const Point &p);
protected:
    float x,y;
};


Point::Point(float a,float b)
{
    x=a;y=b;
}


void Point::setPoint(float a,float b)
{
    x=a;y=b;
}


ostream & operator<<(ostream &os,const Point &p)
{
    os << "["<<p.x<<","<<p.y<<"]"<<endl;
    return os;
}


int main()
{
    Point p(3.5,6.4);
    cout << "x=" << p.getX() << ",y=" << p.getY() << endl;
    p.setPoint(8.5,6.8);
    cout << "p(new):" << p << endl;
}

加入其派生类Circle

#include <iostream>
#include <assert.h>
#include <string.h>

using namespace std;

class Point
{
public:
    Point(float x=0,float y=0);
    void setPoint(float,float);
    float getX()const{return x;}
    float getY()const{return y;}
    friend ostream & operator<<(ostream &os,const Point &p);
protected:
    float x,y;
};

Point::Point(float a,float b)
{
    x=a;y=b;
}

void Point::setPoint(float a,float b)
{
    x=a;y=b;
}

ostream & operator<<(ostream &os,const Point &p)
{
    os << "["<<p.x<<","<<p.y<<"]"<<endl;
    return os;
}

class Circle:public Point
{
private:
    float radius;
public:
    Circle(float x = 0,float y = 0,float r = 0):Point(x,y),radius(r){ }
    void setRadius(float r)
    {
        radius = r;
    }
    float getRadius() const{return radius;}
    float area() const{return 3.14159*radius*radius;}
    friend ostream &operator<<(ostream &output,const Circle&c)
    {
        output << "Center = [" << c.x << "," << c.y << "],r=" << c.radius << ",area=" << c.area() << endl;
        return output;
    }
};



/*
int main()
{
    Point p(3.5,6.4);
    cout << "x=" << p.getX() << ",y=" << p.getY() << endl;
    p.setPoint(8.5,6.8);
    cout << "p(new):" << p << endl;
}
*/

int main()
{
    Circle c(3.5,6.4,5.2);
    cout << "original circle:\nx = " << c.getX() << ",y = " << c.getY() << ",r=" << c.getRadius() << ",area=" << c.area() << endl;
    c.setRadius(7.5);
    c.setPoint(5,5);
    cout << "new circle:\n" <<c;
    Point &pRef = c;    //基类Point类的引用
    cout << "pRef:" << pRef;   //结果调用的基类的运算符重载函数,输出了一个点,而不是圆
    return 0;
}

继续加入Circle类的派生类Cylinder

#include <iostream>
#include <assert.h>
#include <string.h>

using namespace std;

class Point
{
public:
    Point(float x=0,float y=0);
    void setPoint(float,float);
    float getX()const{return x;}
    float getY()const{return y;}
    friend ostream & operator<<(ostream &os,const Point &p);
protected:
    float x,y;
};

Point::Point(float a,float b)
{
    x=a;y=b;
}

void Point::setPoint(float a,float b)
{
    x=a;y=b;
}

ostream & operator<<(ostream &os,const Point &p)
{
    os << "["<<p.x<<","<<p.y<<"]"<<endl;
    return os;
}

class Circle:public Point
{
protected:
    float radius;
public:
    Circle(float x = 0,float y = 0,float r = 0):Point(x,y),radius(r){ }
    void setRadius(float r)
    {
        radius = r;
    }
    float getRadius() const{return radius;}
    float area() const{return 3.14159*radius*radius;}
    friend ostream &operator<<(ostream &output,const Circle&c)
    {
        output << "Center = [" << c.x << "," << c.y << "],r=" << c.radius << ",area=" << c.area() << endl;
        return output;
    }
};

class Cylinder:public Circle
{
protected:
    float height;
public:
    Cylinder(float x=0,float y=0,float r=0,float h=0):Circle(x,y,r),height(h){}
    void setHeight(float h){height = h;}
    float getHeight() const {return height;}
    float area() const {return 2*Circle::area() + 2*3.14159*radius*height;}
    float volume()const {return Circle::area()*height;}
    friend ostream &operator<<(ostream &output,const Cylinder&cy)
    {
        output << "Center = [" << cy.x << "," << cy.y << "],r=" << cy.radius <<",h=" << cy.height << "\narea=" << cy.area() <<",volume =" <<cy.volume() << endl;
        return output;
    }
};

/*
int main()
{
    Point p(3.5,6.4);
    cout << "x=" << p.getX() << ",y=" << p.getY() << endl;
    p.setPoint(8.5,6.8);
    cout << "p(new):" << p << endl;
}


int main()
{
    Circle c(3.5,6.4,5.2);
    cout << "original circle:\nx = " << c.getX() << ",y = " << c.getY() << ",r=" << c.getRadius() << ",area=" << c.area() << endl;
    c.setRadius(7.5);
    c.setPoint(5,5);
    cout << "new circle:\n" <<c;
    Point &pRef = c;    //基类Point类的引用
    cout << "pRef:" << pRef;   //结果调用的基类的运算符重载函数,输出了一个点,而不是圆
    return 0;
}
*/

int main()
{
    Cylinder cy1(3.5,6.4,5.2,10);
    cout << "\noriginal cylinder:\nx="<<cy1.getX() << ",y="<<cy1.getY() << ",r=" << cy1.getRadius() << ",h=" << cy1.getHeight() << "\narea=" << cy1.area()
        <<",volume=" << cy1.volume() << endl;
    cy1.setHeight(15);
    cy1.setRadius(7.5);
    cy1.setPoint(5,5);
    cout << "\nnew cylinder:\n" << cy1;
    Point &pRef = cy1;
    cout << "\npRef as a Point:" << pRef;
    Circle &cRef = cy1;
    cout << "\ncRef as a Circle:" << cRef;
    return 0;
}
Cylinder类的area 同名覆盖了Circle类的area函数;

而三个运算符重载的函数是重载而不是同名覆盖,因为有一个形参类型不同!


运行程序可以看到,使用其基类的指针指向它(Cylinder类对象)时,运行cout<< 函数 得到的并不是我们想要的结果,程序运行了该指针被定义时的类的cout<<函数。

由此 我们引入虚函数,由虚函数引入抽象基类,完善后的程序如下:

#include <iostream>
#include <assert.h>
#include <string.h>

using namespace std;

class Shape
{
public:
    virtual float area() const{return 0.0;}
    virtual float volume() const{return 0.0;}
    virtual void shapeName() const = 0;
};

class Point:public Shape
{
public:
    Point(float a=0,float b=0):x(a),y(b){}
    void setPoint(float,float);
    float getX()const{return x;}
    float getY()const{return y;}
    virtual void shapeName() const {cout << "Point:";}
    friend ostream & operator<<(ostream &os,const Point &p);
protected:
    float x,y;
};

void Point::setPoint(float a,float b)
{
    x=a;y=b;
}

ostream & operator<<(ostream &os,const Point &p)
{
    os << "["<<p.x<<","<<p.y<<"]"<<endl;
    return os;
}

class Circle:public Point
{
protected:
    float radius;
public:
    Circle(float x = 0,float y = 0,float r = 0):Point(x,y),radius(r){ }
    void setRadius(float r){radius = r;}
    float getRadius() const{return radius;}
    virtual float area() const{return 3.14159*radius*radius;}
    virtual void shapeName() const{cout << "Circle:";}
    friend ostream &operator<<(ostream &output,const Circle&c)
    {
        output<< "[" << c.x << "," << c.y << "],r=" << c.radius  << endl;
        return output;
    }
};

class Cylinder:public Circle
{
protected:
    float height;
public:
    Cylinder(float x=0,float y=0,float r=0,float h=0):Circle(x,y,r),height(h){}
    void setHeight(float h){height = h;}
    float getHeight() const {return height;}
    virtual float area() const {return 2*Circle::area() + 2*3.14159*radius*height;}
    virtual float volume()const {return Circle::area()*height;}
    virtual void shapeName() const{cout << "Cylinder:";}
    friend ostream &operator<<(ostream &output,const Cylinder&cy)
    {
        output << "[" << cy.x << "," << cy.y << "],r=" << cy.radius <<",h=" << cy.height << endl;
        return output;
    }
};


int main()
{
    Point point(3.5,6.4);
    Circle circle(2.4,1.2,5.6);
    Cylinder cylinder(3.5,6.4,5.2,10.5);
    point.shapeName();
    cout << point << endl;

    circle.shapeName();
    cout << circle << endl;

    cylinder.shapeName();
    cout << cylinder << endl;

    Shape *pt;

    pt = &point;
    pt ->shapeName();
    cout << "x="<<point.getX() << ",y="<<point.getY() << "\narea=" << pt->area() <<"\nvolume=" << pt->volume() <<endl;

    pt = &circle;
    pt ->shapeName();
    cout << "x="<<circle.getX() << ",y="<<circle.getY() << "\narea=" << pt->area()<<"\nvolume=" << pt->volume() << endl;

    pt = &cylinder;
    pt ->shapeName();
    cout << "x="<<cylinder.getX() << ",y="<<cylinder.getY() << "\narea=" << pt->area()
        <<"\nvolume=" << pt->volume() << endl;

}


个人编了一个virtual的print函数加上去
#include <iostream>
#include <assert.h>
#include <string.h>

using namespace std;

class Shape
{
public:
    virtual float area() const{return 0.0;}
    virtual float volume() const{return 0.0;}
    virtual void shapeName() const = 0;
    virtual void print() const = 0;
};

class Point:public Shape
{
public:
    Point(float a=0,float b=0):x(a),y(b){}
    void setPoint(float,float);
    float getX()const{return x;}
    float getY()const{return y;}
    virtual void shapeName() const {cout << "Point:";}
    virtual void print() const
    {
        cout << "x="<< x << ",y="<< y << "\narea=" << area() <<"\nvolume=" << volume() <<endl << endl ;
    }
    friend ostream & operator<<(ostream &os,const Point &p);
protected:
    float x,y;
};

void Point::setPoint(float a,float b)
{
    x=a;y=b;
}

ostream & operator<<(ostream &os,const Point &p)
{
    os << "["<<p.x<<","<<p.y<<"]"<<endl;
    return os;
}

class Circle:public Point
{
protected:
    float radius;
public:
    Circle(float x = 0,float y = 0,float r = 0):Point(x,y),radius(r){ }
    void setRadius(float r){radius = r;}
    float getRadius() const{return radius;}
    virtual float area() const{return 3.14159*radius*radius;}
    virtual void shapeName() const{cout << "Circle:";}
    virtual void print() const
    {
        cout << "x="<< x << ",y="<< y << "\narea=" << area()<<"\nvolume=" << volume() << endl << endl;
    }
    friend ostream &operator<<(ostream &output,const Circle&c)
    {
        output<< "[" << c.x << "," << c.y << "],r=" << c.radius  << endl;
        return output;
    }
};

class Cylinder:public Circle
{
protected:
    float height;
public:
    Cylinder(float x=0,float y=0,float r=0,float h=0):Circle(x,y,r),height(h){}
    void setHeight(float h){height = h;}
    float getHeight() const {return height;}
    virtual float area() const {return 2*Circle::area() + 2*3.14159*radius*height;}
    virtual float volume()const {return Circle::area()*height;}
    virtual void shapeName() const{cout << "Cylinder:";}
    virtual void print() const
    {
        cout << "x="<< x << ",y="<< y << "\narea=" << area() <<"\nvolume=" << volume() << endl << endl;
    }
    friend ostream &operator<<(ostream &output,const Cylinder&cy)
    {
        output << "[" << cy.x << "," << cy.y << "],r=" << cy.radius <<",h=" << cy.height << endl;
        return output;
    }
};


int main()
{
    Point point(3.5,6.4);
    Circle circle(2.4,1.2,5.6);
    Cylinder cylinder(3.5,6.4,5.2,10.5);
    point.shapeName();
    cout << point << endl;

    circle.shapeName();
    cout << circle << endl;

    cylinder.shapeName();
    cout << cylinder << endl;

    Shape *pt;

    pt = &point;
    pt ->shapeName();
    pt->print();

    pt = &circle;
    pt ->shapeName();
    pt->print();

    pt = &cylinder;
    pt ->shapeName();
    pt->print();

}
用virtual 实现了同样的函数名,调用了不同纵向类的同名方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值