标签:tor set nbsp mes iostream eth 子类 The gety
#include<iostream> using namespace std; class Point{ public: Point(float=0,float=0); void setPoint(float,float); float getX()const{return x;} float getY()const{return y;} friend ostream& operator<<(ostream&,const Point&); 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& cout,const Point& p){ cout<<p.x<<" "<<p.y<<endl; return cout; } class Circle:public Point{ public: Circle(float=0,float=0,float=0); void setRadius(float); float getRadius()const{return radius;} float area()const; friend ostream& operator<<(ostream&,const Circle&); protected: float radius; }; Circle::Circle(float a,float b,float r):Point(a,b),radius(r){} void Circle::setRadius(float r){ radius=r; } float Circle::area()const{ return 3.14*radius*radius; } ostream& operator<<(ostream& cout,const Circle& c){ cout<<c.radius<<" "<<c.x<<" "<<c.y<<endl; return cout; } class Cylinder:public Circle{ public: Cylinder(float=0,float=0,float=0,float=0); void setHeight(float); float getHeight()const{return height;} float area()const; float volume()const; friend ostream& operator<<(ostream&,const Cylinder&); protected: float height; }; Cylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){} void Cylinder::setHeight(float h){ height=h; } float Cylinder::area()const{ return (2*Circle::area()+2*3.14*radius*height); } float Cylinder::volume()const{ return height*Circle::area(); } ostream& operator<<(ostream& cout,const Cylinder& cy){ cout<<cy.x<<" "<<cy.y<<" "<<cy.radius<<" "<<cy.height<<endl; } void test01(){ Circle c(1,1,2); cout<<c; c.setPoint(2,2); c.setRadius(3); cout<<c.getX()<<" "<<c.getY()<<" "<<c.getRadius()<<" "<<c.area()<<endl; } void test02(){ //子类继承父类和爷爷类的成员属性和成员函数 Cylinder cy(1,1,2,3); cout<<cy; cy.setPoint(2,2); cy.setRadius(3); cy.setHeight(4); cout<<cy.getX()<<" "<<cy.getY()<<" "<<cy.getRadius()<<" "<<cy.getHeight()<<endl; cout<<cy.area()<<" "<<cy.volume()<<endl; } int main(){ test02(); return 0; }
#include<iostream> using namespace std; class Shape{ public: virtual float area()const{return 0;} virtual float volume()const{return 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;} friend ostream& operator<<(ostream&,const Point&); virtual void shapeName()const{cout<<"Point"<<endl;} 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& cout,const Point& p){ cout<<p.x<<" "<<p.y<<endl; return cout; } class Circle:public Point{ public: Circle(float=0,float=0,float=0); void setRadius(float); float getRadius()const{return radius;} virtual float area()const; virtual void shapeName()const{cout<<"Circle"<<endl;} friend ostream& operator<<(ostream&,const Circle&); protected: float radius; }; Circle::Circle(float a,float b,float r):Point(a,b),radius(r){} void Circle::setRadius(float r){ radius=r; } float Circle::area()const{ return 3.14*radius*radius; } ostream& operator<<(ostream& cout,const Circle& c){ cout<<c.radius<<" "<<c.x<<" "<<c.y<<endl; return cout; } class Cylinder:public Circle{ public: Cylinder(float=0,float=0,float=0,float=0); void setHeight(float); float getHeight()const{return height;} virtual float area()const; virtual float volume()const; virtual void shapeName()const{cout<<"Cylinder"<<endl;} friend ostream& operator<<(ostream&,const Cylinder&); protected: float height; }; Cylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){} void Cylinder::setHeight(float h){ height=h; } float Cylinder::area()const{ return (2*Circle::area()+2*3.14*radius*height); } float Cylinder::volume()const{ return height*Circle::area(); } ostream& operator<<(ostream& cout,const Cylinder& cy){ cout<<cy.x<<" "<<cy.y<<" "<<cy.radius<<" "<<cy.height<<endl; } int main(){ Shape *sp; Point p(1,1); Circle c(2,2,3); Cylinder cy(3,3,4,5); //利用多态可以方便地求出不同图形的面积和体积 sp=&p; sp->shapeName(); cout<<sp->area()<<" "<<sp->volume()<<endl; sp=&c; sp->shapeName(); cout<<sp->area()<<" "<<sp->volume()<<endl; c.setRadius(30); cout<<sp->area()<<" "<<sp->volume()<<endl; sp=&cy; sp->shapeName(); cout<<sp->area()<<" "<<sp->volume()<<endl; cy.setRadius(40);cy.setHeight(50); cout<<sp->area()<<" "<<sp->volume()<<endl; return 0; }
标签:tor set nbsp mes iostream eth 子类 The gety
原文地址:https://www.cnblogs.com/lyt888/p/12490699.html