#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";
}
虛函數(shù)的作用就是為了實(shí)現(xiàn)多態(tài),和php的延時(shí)綁定是一樣的。
函數(shù)重載是靜態(tài)的,在橫向上的功能, 虛函數(shù)是類繼承上的功能,是動(dòng)態(tài)的。文章來源地址http://www.zghlxwxcb.cn/news/detail-708241.html
文章來源:http://www.zghlxwxcb.cn/news/detail-708241.html
到了這里,關(guān)于【C++學(xué)習(xí)】第六章多態(tài)與虛函數(shù)案例實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!