#include<iostream>
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=0, float=0);
void setPoint(float, float);
float getX() const{return x;}
float getY() const{return y;}
virtual void shapeName() const {
cout << "Point:";
}
friend ostream & operator << (ostream &, const Point &);
protected:
float x,y;
};
class Circle:public Point{
public:
Circle(float x=0, float y=0,float r=0);
void setRedius(float);
float getR() const{return radius;}
virtual void shapeName() const {
cout << "Circle:";
}
float area() const;
friend ostream & operator << (ostream &, const Circle &);
protected:
float radius;
};
class Cylinder:public Circle{
public:
Cylinder(float x=0, float y=0,float r=0,float h=0);
void setHeight(float);
float getHeight() const{return height;}
virtual void shapeName() const {
cout << "Cylinder:";
}
float area() const;
float volume() const;
friend ostream & operator << (ostream &, const Cylinder &);
protected:
float height;
};
Point::Point(float a, float b){
x = a;
y = b;
}
void Point::setPoint(float a, float b){
x = a;
y = b;
}
ostream & operator << (ostream & output, const Point &p){
output <<"[p.x="<<p.x << ",p.y" << p.y <<"]";
return output;
}
ostream & operator << (ostream & output, const Circle &c){
output <<"[c.x="<<c.x << ",c.y" << c.y << ",c.redius"<< c.radius<<"]";
return output;
}
Circle::Circle(float a, float b,float c):Point(a,b),radius(c){}
void Circle::setRedius(float r){
radius = r;
}
float Circle::area() const {
return 3.14 * radius * radius;
}
Cylinder::Cylinder(float a, float b,float c, float h):Circle(a,b,c),height(h){}
float Cylinder::area() const {
return 3.14 * radius * radius + 2*3.14*radius*height;
}
float Cylinder::volume() const {
return 3.14 * radius * radius *height;
}
ostream & operator << (ostream & output, const Cylinder &cy){
output <<"[cy.x="<<cy.x << ",cy.y" << cy.y << ",cy.redius"<< cy.radius<< ",cy.height" <<cy.height<<"]";
return output;
}
int main(){
Point point(3.2, 4.5);
Circle circle(2.4,12,18);
Cylinder cylinder(1,2,3,4);
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() << "\n area=" << pt->area() <<"\n volumn="<<pt->volume() << "\n\n";
pt = &circle;
pt->shapeName();
cout << "x=" << circle.getX() <<",y=" << circle.getY() << ",r=" << circle.getR() <<"\n area=" << pt->area() <<"\n volumn="<<pt->volume() << "\n\n";
pt = &cylinder;
pt->shapeName();
cout << "x=" << cylinder.getX() <<",y=" << cylinder.getY() << ",r=" << cylinder.getR() <<"\n area=" << pt->area() <<"\n volumn="<<pt->volume() << "\n\n";
}
虚函数的作用就是为了实现多态,和php的延时绑定是一样的。
函数重载是静态的,在横向上的功能, 虚函数是类继承上的功能,是动态的。文章来源地址https://www.toymoban.com/news/detail-708241.html
文章来源:https://www.toymoban.com/news/detail-708241.html
到了这里,关于【C++学习】第六章多态与虚函数案例实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!