题解 | #KiKi设计类继承#
KiKi设计类继承
https://www.nowcoder.com/practice/699ba050e2704591ae3e62401a856b0e
#include <iostream> using namespace std; class shape{ protected: double x,y; public: shape(double x,double y):x(x),y(y){} virtual double GetArea(){ return 0.0; } }; class Rectangle:public shape{ protected: double len,wid; public: Rectangle(double x,double y,double len,double wid):shape(x,y),len(len),wid(wid){} double GetArea() override{ return len*wid; } }; class circle:public shape{ private: double redius; public: circle(double x,double y,double redius):shape(x,y),redius(redius){} double GetArea() override{ return 3.14*redius*redius; } }; class squre:public Rectangle{ protected: double side; public: squre(double x,double y,double side):Rectangle( x, y, side, side){} }; int main() { double len,wid,redius,side; cin>>len>>wid; cin>>redius; cin>>side; Rectangle a(0,0,len,wid); circle b(0,0,redius); squre c(0,0,side); cout<<a.GetArea()<<endl; cout<<b.GetArea()<<endl; cout<<c.GetArea()<<endl; } // 64 位输出请用 printf("%lld")