26785-1 继承与派生

来源:互联网 发布:电气预算软件 编辑:程序博客网 时间:2024/05/17 09:40

5-1 继承与派生

#include<iostream>#include<cstring>using namespace std;class Point{public :  Point(){x=0;y=0;}  Point(float  a,float b){x=a;y=b;}  float Move (float,float);  float Getx(float);  float Gety(float);  void setting();  void disshow();private:    float x,y;};void Point ::setting(){    cin>>x>>y;}float Point::Move(float a,float b){    x+=a;    y+=b;}float Point ::Getx(float a){    return x+a;}float Point ::Gety(float b){    return y+b;}void Point ::disshow(){    cout<< x<<" "<<y;}class Rectangle:public Point{public :    Rectangle(float a,float b,float c,float d):Point(a,b)    {        w=c;        h=d;    }   float Geth();   float Getw();   void setting1();   void show();private:    float w;    float h;};void Rectangle::setting1(){    setting();    cin>>w>>h;    if(w<=0) w=0;    if(h<=0) h=0;}float Rectangle ::Geth(){    return h;}float Rectangle ::Getw(){    return w;}void Rectangle :: show(){    disshow();    cout<<" "<<w<<" "<<h<<endl;}int main(){    float c,d,a,b;    Rectangle r1(a,b,c,d);//注意构造函数的形式    r1.setting1();    cin>>a>>b;    r1.Move(a,b);    r1.show();    return 0;}
0 0
原创粉丝点击