C++期末试题4

程序填空

/*
请将如下程序补充完整,使得程序运行时的输出结果为:
1, 1, 2.3
2, 4.6

注意:仅允许在指定的下划线处填写内容,并删除下划线编号,不允许增加或删除语句,也不得改动程序中的其他部分。
试题源程序:
*/
#include <iostream>
using namespace std;
class Base {
	public:

/**********FILL**********/
		Base(int a): idata(a)  { } 
		void print() { cout<<idata<<", "; }
	private:
		int idata;
};

/**********FILL**********/
class Derived:public Base {
	public:

/**********FILL**********/
		Derived(int a, double b):Base(a) { ddata=b; }
		void print() {

/**********FILL**********/
			Base::print(); 
			cout<<ddata<<endl;
		}
	private:
		double ddata;
};
int main() {
	Derived d1(1,2.3), d2(2,4.6);

/**********FILL**********/
	d1.Base::print(); 
	d1.print();
	d2.print();
	return 0;
}

2

/*
请将如下程序补充完整,使得程序运行时的输出结果为:
B0:1
B1:2
B2:3
D:1

注意:仅允许在指定的下划线处填写代码,不得改动程序中的其他内容(需删除下划线编号)。
试题源程序:
*/
#include<iostream>
using namespace std;
class B0 {
	protected:
		int a;
	public:
		B0(int x) { a=x; } 
		void f() { cout<<"B0:"<<a<<endl; }		
};

/**********FILL**********/
class B1 : virtual public B0 {
		int b;
	public:		
		B1(int x, int y):B0(x),b(y) { }
		void f() { cout<<"B1:"<<b<<endl; }		
};

/**********FILL**********/
class B2: virtual public B0 { 
		int b;
	public:		
		B2(int x, int y):B0(x),b(y) { }
		void f() { cout<<"B2:"<<b<<endl; }		
};

/**********FILL**********/
class D:public B2,public B1 {
	public:

/**********FILL**********/
		D(int x, int y,int z):B0(x),B1(x,y),B2(x,z) { } 
		void f() { cout<<"D:"<<a<<endl; }
};
int main() {
	D d(1,2,3);

/**********FILL**********/
	d.B0::f();  
	d.B1::f();
	d.B2::f();
	d.f();
	return 0;
}

改错

1

/*
请改正程序中指定位置的错误,使程序的输出结果如下: 
Base1
Base2
3, 1, 4

注意:只允许修改注释"ERROR"的下一行,不得改动程序中的其他内容,也不允许增加或删减语句。
源程序清单:
*/
#include <iostream>
using namespace std;
class Base1 {
	public: 
		Base1(int x) {
			b1=x;
			cout<<"Base1"<<endl;
		}
	protected: 
		int b1;  
};
class Base2 {
	public:
		Base2() {
			b2++;
			cout<<"Base2"<<endl;
		}
	protected:
		static int b2; 
};

/**********ERROR**********/
int Base2::b2=0; 

/**********ERROR**********/
class Derived:public Base1,public Base2 
{	public:

/**********ERROR**********/
		Derived(int a,int b):Base1(a)    
		{ d=b; }  // 形参a,b分别用于初始化数据成员b1,d

/**********ERROR**********/
		void Display() const
		{ cout<<b1<<", "<<b2<<", "<<d<<endl; }
	private:
		int d;
};
int main() {
	const Derived d(3,4);
	d.Display(); 
	return 0;
}

2

/*
请改正程序中指定位置的错误,使程序的输出结果如下: 
classBase
classD1
classD2
classD3
finBase
finD1
finD2

注意:只允许修改注释"ERROR"的下一行,不得改动程序中的其他内容,也不允许增加或删减语句。
源程序:
*/
#include <iostream>
using namespace std;
class Base{

/**********ERROR**********/
	public:
		Base(){cout<<"classBase"<<endl;}
		void f(){cout<<"finBase"<<endl;} 
};

/**********ERROR**********/
class D1: virtual public Base
{	public:
		D1(){cout<<"classD1"<<endl;}
		void f(){cout<<"finD1"<<endl;} 
};
class D2:virtual protected Base  
{	public:
		D2(){cout<<"classD2"<<endl;} 
		void f(){cout<<"finD2"<<endl;} 
};

/**********ERROR**********/
class D3:public D1,public D2
{	public:
		D3(){cout<<"classD3"<<endl;} 
};
int main() {
	D3 d;

/**********ERROR**********/
	d.Base::f();
	d.D1::f();
	d.D2::f();
	return 0;
}

程序设计

/*
点(Point)类成员如下: 
(1)公有成员:
Point(float xx, float yy)  // 构造函数,初始化点的x, y坐标
float getX() const  // 返回横坐标x
float getY() const  // 返回纵坐标y
void setX(float newX)  // 重设横坐标为newX
void setY(float newY)  // 重设纵坐标为newY
(2)私有成员:
float x, y  // 点的横坐标,纵坐标
由Point类公有派生出圆(Circle)类,基类Point的x, y成员作为圆心的坐标,并新增如下成员:
(1)公有成员:
Circle(float x=0.0, float y=0.0, float r=1.0)  // 构造函数,其中(x,y)为圆心位置,r为圆的半径
void moveTo(float newX, float newY)  // 平移操作,将圆心移动到newX, newY
float getRadius() const  // 返回圆的半径
double getCircumference() const  // 计算并返回圆的周长,π=3.14159
double getArea() const  // 计算并返回圆的面积,π=3.14159
double dist(const Circle &c) const  // 计算并返回到另一个圆的距离(圆心之间的距离)
bool isEqual(const Circle &c) const  // 判断与另一个圆是否大小相等
(2)私有成员:
float radius  // 圆的半径 

请根据上述说明,完成Point,Circle两个类的定义。
注意:部分源程序已给出,仅允许在注释“Begin”和“End”之间填写内容,不得改动其他已有的任何内容。

测试样例:
输入:
0 0 1
3 4
输出:
初始: 
c1:圆心: (0, 0), 半径: 1, 周长: 6.28318, 面积: 3.14159
c2:圆心: (0, 0), 半径: 1, 周长: 6.28318, 面积: 3.14159
c1与c2圆心之间距离:0
c1与c2大小是否相等:1

平移后: 
c2:圆心: (3, 4), 半径: 1, 周长: 6.28318, 面积: 3.14159
c1与c2圆心之间距离:5
c1与c2大小是否相等:1

试题程序:
*/
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
const double PI=3.14159;

/*******Begin*******/
class Point{
	public:
		Point(float xx, float yy):x(xx),y(yy){}  // 构造函数,初始化点的x, y坐标
		float getX() const{return this->x;}  // 返回横坐标x
		float getY() const{return this->y;}  // 返回纵坐标y
		void setX(float newX) {this->x=newX;} // 重设横坐标为newX
		void setY(float newY){this->y=newY;}  // 重设纵坐标为newY
	private:
		float x, y;  // 点的横坐标,纵坐标
}; 

class Circle:public Point {
	public:
		Circle(float x=0.0, float y=0.0, float r=1.0):Point(x,y){
			this->radius=r;
		}  // 构造函数,其中(x,y)为圆心位置,r为圆的半径
		void moveTo(float newX, float newY){
			setX(newX);setY(newY);
		}  // 平移操作,将圆心移动到newX, newY
		float getRadius() const {
			return this->radius;
		} // 返回圆的半径
		double getCircumference() const {
			return 2*PI*radius;
		} // 计算并返回圆的周长,π=3.14159
		double getArea() const {
			return PI*radius*radius;
		} // 计算并返回圆的面积,π=3.14159
		double dist(const Circle &c) const{
			double x=this->getX()-c.getX();
			double y=this->getY()-c.getY();
			return sqrt(x*x+y*y);
		}  // 计算并返回到另一个圆的距离(圆心之间的距离)
		bool isEqual(const Circle &c) const{
			if(this->getRadius()==c.getRadius())return true;
			return false;
		}  // 判断与另一个圆是否大小相等
	private:
		float radius ; // 圆的半径 
};

/*******End*********/

int main() {
	float x,y,r;
	cin>>x>>y>>r;
	Circle c1(x,y,r),c2;
	cout<<"初始: "<<endl;
	cout<<"c1:圆心: ("<<c1.getX()<<", "<<c1.getY()<<"), "<<"半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<", 面积: "<<c1.getArea()<<endl;
	cout<<"c2:圆心: ("<<c2.getX()<<", "<<c2.getY()<<"), "<<"半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<", 面积: "<<c2.getArea()<<endl;
	cout<<"c1与c2圆心之间距离:"<<c1.dist(c2)<<endl;	
	cout<<"c1与c2大小是否相等:"<<c1.isEqual(c2)<<endl<<endl;
	
	cin>>x>>y;
	c2.moveTo(x,y);
	cout<<"平移后: "<<endl;
	cout<<"c2:圆心: ("<<c2.getX()<<", "<<c2.getY()<<"), "<<"半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<", 面积: "<<c2.getArea()<<endl;
	cout<<"c1与c2圆心之间距离:"<<c1.dist(c2)<<endl;	
	cout<<"c1与c2大小是否相等:"<<c1.isEqual(c2)<<endl<<endl;

	ifstream in1("7.1.2_1-s2_in.dat");
	ofstream out1("7.1.2_1-s2_out.dat");
	while(in1>>x>>y>>r) {
		Circle c1(x,y,r),c2;
		out1<<"初始: "<<endl;
		out1<<"c1:圆心: ("<<c1.getX()<<", "<<c1.getY()<<"), "<<"半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<", 面积: "<<c1.getArea()<<endl;
		out1<<"c2:圆心: ("<<c2.getX()<<", "<<c2.getY()<<"), "<<"半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<", 面积: "<<c2.getArea()<<endl;
		out1<<"c1与c2圆心之间距离:"<<c1.dist(c2)<<endl;	
		out1<<"c1与c2大小是否相等:"<<c1.isEqual(c2)<<endl<<endl;
		
		in1>>x>>y;
		c2.moveTo(x,y);
		out1<<"平移后: "<<endl;
		out1<<"c2:圆心: ("<<c2.getX()<<", "<<c2.getY()<<"), "<<"半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<", 面积: "<<c2.getArea()<<endl;
		out1<<"c1与c2圆心之间距离:"<<c1.dist(c2)<<endl;	
		out1<<"c1与c2大小是否相等:"<<c1.isEqual(c2)<<endl<<endl;
	}
	in1.close();
	out1.close();
	return 0;
}

2

/*
点(Point)类成员如下:
(1)公有成员:
Point(float xx, float yy)  // 构造函数,初始化点的x, y坐标
float getX() const  // 返回横坐标x
float getY() const  // 返回纵坐标y
void moveTo(float newX, float newY)  // 将点的x, y坐标移动到newX, newY
(2)私有成员:
float x, y  // 点的横坐标,纵坐标
由Point类保护派生出正方形(Square)类,基类Point的x,y成员作为正方形左下角的坐标,并新增如下成员:
(1)公有成员:
Square(float x=0.0,float y=0.0,float len=1.0) // 构造函数,初始化所有数据成员,其中(x,y)为左下角位置,len为边长
float getX() const  // 返回正方形左下角横坐标x
float getY() const  // 返回正方形下角纵坐标y
float getLen() const // 返回正方形的边长
double getArea() const // 返回正方形的面积
void moveTo(float newX, float newY)  // 平移操作,将左下角移动到(newX, newY)
double dist(const Square &s) const // 计算并返回到另一个正方形的距离(左下角之间的距离)
(2)私有成员:
float length  // 正方形的边长

请根据上述说明,完成Point,Square两个类的定义。
注意:部分源程序已给出,仅允许在注释"Begin"和"End"之间填写内容,不得改动main函数和其他已有的任何内容。
试题程序:
*/
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;

/*******Begin*******/
class Point{
	public:
		Point(float xx, float yy):x(xx),y(yy){}  // 构造函数,初始化点的x, y坐标
		float getX() const {return this->x;} // 返回横坐标x
		float getY() const {return this->y;} // 返回纵坐标y
		void moveTo(float newX, float newY){
			this->x=newX;this->y=newY; 
		}  // 将点的x, y坐标移动到newX, newY
		
	private:
		float x, y;  // 点的横坐标,纵坐标
};

class Square:protected Point{
	public:
		Square(float x=0.0,float y=0.0,float len=1.0):Point(x,y){
			this->length=len;
		} // 构造函数,初始化所有数据成员,其中(x,y)为左下角位置,len为边长
		float getX() const{
			return Point::getX();
		}  // 返回正方形左下角横坐标x
		float getY() const{
			return Point::getY();
		}  // 返回正方形下角纵坐标y
		float getLen() const{
			return this->length;
		} // 返回正方形的边长
		double getArea() const{
			return this->length*this->length;
		} // 返回正方形的面积
		void moveTo(float newX, float newY) {
			Point::moveTo(newX,newY);
		} // 平移操作,将左下角移动到(newX, newY)
		double dist(const Square &s) const{
			double x=this->getX()-s.getX();
			double y=this->getY()-s.getY();
			return sqrt(x*x+y*y);
		} // 计算并返回到另一个正方形的距离(左下角之间的距离)
		
	private:
		float length ; // 正方形的边长
};

/*******End*********/

int main() {
	float x,y,len;
	cin>>x>>y>>len;
	Square s1(x,y,len),s2;
	cout<<"初始: "<<endl;
	cout<<"s1左下角: ("<<s1.getX()<<", "<<s1.getY()<<")"<<endl;	 
	cout<<"边长: "<<s1.getLen()<<", 面积: "<<s1.getArea()<<endl;
	cout<<"s2左下角: ("<<s2.getX()<<", "<<s2.getY()<<")"<<endl;	 
	cout<<"边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
	cout<<"距离:"<<s2.dist(s1)<<endl;	
	cin>>x>>y;
	s2.moveTo(x,y);
	cout<<"平移后: "<<endl;
	cout<<"s2左下角: ("<<s2.getX()<<", "<<s2.getY()<<")"<<endl;	 
	cout<<"边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
	cout<<"距离:"<<s2.dist(s1)<<endl;
	
	ifstream in1("7.1.3_5-3_in.dat");
	ofstream out1("7.1.3_5-3_out.dat");
	while(in1>>x>>y>>len) {
		Square s1(x,y,len),s2;
		out1<<"初始: "<<endl;
		out1<<"s1左下角: ("<<s1.getX()<<", "<<s1.getY()<<")"<<endl;	 
		out1<<"边长: "<<s1.getLen()<<", 面积: "<<s1.getArea()<<endl;
		out1<<"s2左下角: ("<<s2.getX()<<", "<<s2.getY()<<")"<<endl;	 
		out1<<"边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
		out1<<"距离:"<<s2.dist(s1)<<endl;		
		in1>>x>>y;
		s2.moveTo(x,y);
		out1<<"平移后: "<<endl;
		out1<<"s2左下角: ("<<s2.getX()<<", "<<s2.getY()<<")"<<endl;	 
		out1<<"边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
		out1<<"距离:"<<s2.dist(s1)<<endl<<endl;		
	}
	in1.close();
	out1.close();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值