## C++第四次作业

2019-10-13 18:36  jormungandlalala  阅读(193)  评论(0编辑  收藏  举报

1.知识要点：

（2）此处说的访问一是派生类中的成员访问基类继承成员，二是在派生类外部通过派生类的对象访问基类成员

# include

using namespace std;
class Point{
public:
void initPoint(float x,float y){this->x=x;this->y=y;}
void Move(float xOff,float yOff){x=x+xOff;y=y+yOff;}
float getX(){return x;}
float getY(){return y;}
private:
float x,y;
};
class Rectangle:public Point{
public:
void initRectangle(float x,float y,float w,float h){
initPoint(x,y);
this->w=w;
this->h=h;
}
float getH(){return h;}
float getW(){return w;}
private:
float w,h;
};
int main()
{
Rectangle rect;
rect.initRectangle(2,3,20,10);
rect.Move(3,2);
cout<<"the data of the rect is (x,y,w,h):"<<endl;
cout<<rect.getX()<<","
<<rect.getY()<<","
<<rect.getW()<<","
<<rect.getH()<<endl;
return 0;
}

1.知识要点：

   借用上一道题 我们将继承Point类的方式改为私有继承
代码如下：


# include

using namespace std;
class Point{
public:
void initPoint(float x,float y){this->x=x;this->y=y;}
void Move(float xOff,float yOff){x=x+xOff;y=y+yOff;}
float getX(){return x;}
float getY(){return y;}
private:
float x,y;
};
class Rectangle:public Point{
public:
void initRectangle(float x,float y,float w,float h){
initPoint(x,y);
this->w=w;
this->h=h;
}
void Move(float OffX,float OffY){Point::Move(OffX,OffY);}
float getX(){return Point::getX();}
float getY(){return Point::getY();}
float getH(){return h;}
float getW(){return w;}
private:
float w,h;
};
int main()
{
Rectangle rect;
rect.initRectangle(2,3,20,10);
rect.Move(3,2);
cout<<"the data of the rect is (x,y,w,h):"<<endl;
cout<<rect.getX()<<","
<<rect.getY()<<","
<<rect.getW()<<","
<<rect.getH()<<endl;
return 0;
}

1.知识要点：

class A{
protected:
int x;
};
class B:public A{
void fun();
};
void B::fun()
{
x=5;
}