目录
程序设计题
1. 改错:对基类protected的成员访问
阅读下面程序,找出语法错误所在的行并改正。
#include<iostream> using namespace std; class Point //基类Point类的定义 { public: //公有函数成员 void InitP(float xx=0, float yy=0) { X=xx;Y=yy; } void Move(float xOff, float yOff) { X+=xOff;Y+=yOff; } float GetX() { return X; } float GetY() { return Y; } protected: //私有数据成员 float X,Y; }; class Rectangle: public Point //派生类声明部分 { public: //新增公有函数成员 void InitR(float x, float y, float w, float h) { X=x; Y=y; //访问基类私有数据成员 W=w; H=h; } float GetH() {return H;} float GetW() {return W;} private: //新增私有数据成员 float W,H; }; int main() { Rectangle rect; rect.InitR(1,2,3,4); cout<<rect.X<<endl; cout<<rect.GetY()<<endl; return 0; }
示例代码:
#include<iostream>
using namespace std;
class Point //基类Point类的定义
{
public: //公有函数成员
void InitP(float xx=0, float yy=0) { X=xx;Y=yy; }
void Move(float xOff, float yOff) { X+=xOff;Y+=yOff; }
float GetX() { return X; }
float GetY() { return Y; }
protected: //私有数据成员
float X,Y;
};
class Rectangle: public Point //派生类声明部分
{
public: //新增公有函数成员
void InitR(float x, float y, float w, float h)
{
InitP(x,y); //访问基类私有数据成员
W=w;
H=h;
}
float GetH() {return H;}
float GetW() {return W;}
float GetfatherX()
{
return GetX();
}
float GetfatherY()
{
return GetY();
}
private: //新增私有数据成员
float W,H;
};
int main()
{
Rectangle rect;
rect.InitR(1,2,3,4);
cout<<rect.GetfatherX()<<endl;
cout<<rect.GetfatherY()<<endl;
return 0;
}
知识回顾:继承中的基类私有成员不能直接访问,需要设置函数接口。
2. Point类公有继承
【问题描述】
有如下的Point类的定义:class Point //基类Point类的声明
{
public: //公有函数成员
void InitP(float xx=0, float yy=0) {X=xx;Y=yy;}
void Move(float xOff, float yOff) {X+=xOff;Y+=yOff;}
float GetX() {return X;}
float GetY() {return Y;}
private: //私有数据成员
float X,Y;
};
从Point类中派生出新的Rectangle(矩形)类。矩形是由一个点加上长宽构成。矩形的点具备了Point类的全部特征。继承方式为公有继承。
【输入形式】
第一行输入四个数,前两个数为给定矩形左上角的坐标(x,y),后两个数为矩形的长、宽。
第二行输入两个数,分别为矩形在x、y方向上的移动量。
【输出形式】
一行四个数,x,y,l,w,分别代表移动后矩形左上角的坐标(x,y)以及矩形的长、宽。
【样例输入】
3 2 20 10
2 3
【样例输出】
5 5 20 10
示例代码:
#include <iostream>
using namespace std;
class Point //基类Point类的声明
{
public: //公有函数成员
void InitP(float xx=0, float yy=0) {X=xx;Y=yy;}
void Move(float xOff, float yOff) {X+=xOff;Y+=yOff;}
float GetX() {return X;}
float GetY() {return Y;}
private: //私有数据成员
float X,Y;
};
class Rectangle :public Point
{
public:
void InitR(float x, float y, float w, float h)
{
InitP(x,y); //访问基类私有数据成员
W=w;
H=h;
}
float GetH() {return H;}
float GetW() {return W;}
float GetfatherX()
{
return GetX();
}
float GetfatherY()
{
return GetY();
}
private:
float W,H;
};
int main()
{
Rectangle text;
float a1,a2,a3,a4;
cin>>a1>>a2>>a3>>a4;
float movex,movey;
cin>>movex>>movey;
text.InitR(a1,a2,a3,a4);
text.Move(movex,movey);
cout<<text.GetX()<<" ";
cout<<text.GetY()<<" ";
cout<<text.GetW()<<" ";
cout<<text.GetH()<<endl;
return 0;
}
3. 改错:多继承,虚基类
阅读下面程序,找出语法错误并改正。
#include <iostream> using namespace std; class A { public: int n; }; class B:public A{}; class C:public A{}; class D:public B,public C { int getn() { return n; } }; int main() { D d; d.n=10; cout<<d.n<<endl; return 0; }
输出:10
示例代码:
#include <iostream>
using namespace std;
class A
{
public:
int n;
};
class B:virtual public A{};
class C:virtual public A{};
class D:public B,public C
{
int getn()
{
return n;
}
};
int main()
{
D d;
d.n=10;
cout<<d.n<<endl;
return 0;
}
知识回顾:菱形继承中的虚继承
4. 两用沙发SleeperSofa类
【问题描述】
已有一个Bed类和一个Sofa类,现要求定义一个两用沙发类SleeperSofa,继承前两个类,并且自身还有一个方法FoldOut(),用来表示折叠或打开沙发,在此方法中输出“Fold out”(注意两个单词之间有一个空格)。
【样例输出】
Watching
Fold out
Sleeping
示例代码:
#include <iostream>
using