北科程序设计第十四周上机

                                   

目录

                                   

                                    程序设计题  

1. 改错:对基类protected的成员访问

2. Point类公有继承

3. 改错:多继承,虚基类

4. 两用沙发SleeperSofa类

5. vehicle类的继承与派生

 6. 定义Employee类子类

7. Point3d类重载print函数

8. Document类的继承与派生

9. circle类的继承与派生

 10. StudentTeacher类

11. Shape类的继承与派生

12. 类的派生与继承

                                      程序片段题

1. 继承与派生练习,shape类为例

2. vehicle类

3. 两用沙发SleeperSofa类


                                    程序设计题  

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值