#include<iostream>
#include<string>
using namespace std;
class Poly{
protected:
string name;
double base;
double height;
public:
Poly(string nm = "default_poly", double bs = 0, double ht = 0):name(nm),base(bs),height(ht){}
virtual double getArea()const;
string getName()const;
};
string Poly::getName()const{
return name;
}
double Poly::getArea()const{
return 0;
}
//======================================
class Rect : public Poly{
public:
Rect(string nm = "default_rect", double bs = 0, double ht = 0):Poly(nm,bs,ht){}
virtual double getArea()const;
};
double Rect::getArea()const{
return base * height;
}
//=======================================
int main(int argc, char *argv){
Poly p1;
cout<<p1.getName()<<" : "<<p1.getArea()<<endl;
Poly p2("first_poly", 2, 3);
cout<<p2.getName()<<" : "<<p2.getArea()<<endl;
Rect r1;
cout<<r1.getName()<<" : "<<r1.getArea()<<endl;
Rect r2("rect2", 4, 5);
cout<<r2.getName()<<" : "<<r2.getArea()<<endl;
return EXIT_SUCCESS;
}
SimpleInherit.cpp
最新推荐文章于 2024-06-05 11:31:37 发布