C++期末试题5

改错

/*
请改正程序中指定位置的错误,使程序的输出结果如下: 
45123
65123

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

class BaseClass {
	public:
		int x;
};

/**********ERROR**********/
class ClassA:public BaseClass 
{	protected:
		int y;
};

class ClassB:public BaseClass {
	protected:
		int z;
};

/**********ERROR**********/
class Derived:public ClassA,public ClassB 
{	public:
		int x;
		Derived() { 
			x=1,y=2,z=3;

/**********ERROR**********/
			ClassA::x=4;ClassB::x=5; 
		}
	void Display() {
		cout<<ClassA::x<<ClassB::x<<x<<y<<z<<endl;
	}
};

int main() {
	Derived d;
	d.Display();

/**********ERROR**********/
	d.ClassA::x=6; 
	d.Display();
	return 0;
}

2

/*
请改正程序中指定位置的错误,使程序的输出结果如下: 
f1 function of base
f2 function of base
f1 function of derive
f2 function of base

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

class base {

/**********ERROR**********/
	public:   
 
/**********ERROR**********/
		void virtual f1() /*重要*/
		{ cout<<"f1 function of base"<<endl; }  
		void f2() { cout<<"f2 function of base "<<endl; }
};

/**********ERROR**********/
class derive:public base
{	public: 
		void f1() { cout<<"f1 function of derive"<<endl; }
		void f2() { cout<<"f2 function of derive "<<endl; }
};

int main() { 

/**********ERROR**********/
	base *p;
	base obj1; 
	derive obj2;
	p=&obj1; p->f1(); p->f2();
	p=&obj2; p->f1(); p->f2();
	return 0;
}

程序设计

1

/*
请将如下程序补充完整,使得程序运行时的输出结果为:
BCA
4, 2, 6
4; 2; 6

注意:仅允许在指定的下划线处填写内容,并删除下划线编号,不允许增加或删除语句,也不得改动程序中的其他部分。
试题源程序:
*/
#include <iostream>
using namespace std;
class Base1 {
	public:
		Base1(int x) { b1=x; cout<<"A"; }
		int b1;
};
class Base2 {
	public:
		Base2() { b2+=2; cout<<"B"; }

/**********FILL**********/
		static int b2 ; 
};
int Base2::b2=0;
class Base3 {
	public:
		Base3(int y):b3(y) { cout<<"C"; }
		int b3;
};

/**********FILL**********/
class Derived:public Base2,public Base3,public Base1{ 
	public:

/**********FILL**********/
		Derived(int x,int y):Base1(x),Base3(y) { } /*重要*/

/**********FILL**********/
		void Display() const { /*重要*/
			cout<<b1<<", "<<b2<<", "<<b3<<endl; }
};
int main() {
	const Derived d(4,6);
	cout<<endl;
	d.Display();
	cout<<d.b1<<"; "<<d.b2<<"; "<<d.b3<<endl;
	return 0;
}

2

/*
下面程序在定义分数(fract)类的基础上,重载复合赋值运算符“+=”和提取符“>>”。
请在程序的指定位置填入正确的内容,使程序得出正确的结果。
程序运行时,若依次输入:
3 4 
5 7
则输出结果为:
fr1: 41/28
fr2: 5/7 
注意:仅允许在指定的下划线处填写内容,并删除下划线编号,不允许增加或删减语句,也不得改动程序中的其他任何内容。
试题源程序:
*/
#include <iostream>
using namespace std;
class fract {
		int num; //分子
		int den; //分母
	public:
		fract(int n=0,int d=1):num(n),den(d) { }
		fract &operator +=(const fract&); 
		void show() const { cout<<num<<'/'<<den<<endl; } 

/**********FILL**********/
		friend istream &operator >>(istream &, fract &);
};

/**********FILL**********/
fract &fract::operator +=(const fract& f)
{	
	this->num=this->num*f.den+this->den*f.num;
	this->den*=f.den;

/**********FILL**********/
	return *this;
}
istream &operator >>(istream &in, fract &f) { 
	in>>f.num>>f.den;

/**********FILL**********/
	return in; 
}
int main() {
	int n,d;
	cin>>n>>d;
	fract fr1(n,d),fr2;
	cin>>fr2;
	fr1+=fr2; 
	cout<<"fr1: "; fr1.show(); 
	cout<<"fr2: "; fr2.show(); 
	return 0;
}

3

/*
请将如下程序补充完整,使得程序运行时的输出结果为:
Base::display
Derive::display
Derive::display const

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

/**********FILL**********/
		void virtual display(){
			cout<<"Base::display"<< endl; 
		}
};
class Derive:public Base { 
	public:

/**********FILL**********/
		void display() const; 
		void display() { cout<<"Derive::display"<<endl; }
};
void Derive::display() const {
	cout<<"Derive::display const"<<endl; }

/**********FILL**********/
void Display(Base &p) 
{ p.display(); } 
 
int main() {
	Base b1; Display(b1);
	Derive d1; Display(d1);
	const Derive d2; 

/**********FILL**********/
	d2.display(); 
	return 0;
}

设计

1

/*
复数(Complex)类定义如下:  
class Complex {	
	public:	
		Complex(int r = 0, int i = 0): real(r), imag(i) { }	// 构造函数
	private:
		int real;	// 复数实部
		int imag;  // 复数虚部
};
基于上述定义,请完成以下运算符重载:
(1)以非成员函数形式重载前置“++”运算符,使实部和虚部分别加1。
(2)将运算符“==”重载为成员函数形式,实现判断两个复数是否相等(若相等则返回true,否则返回false)。
(3)重载“<<”运算符,用于复数对象的输出(格式为“(real, imag)”)。
注意:部分源程序已给出,仅允许在注释“Begin”和“End”之间补全代码,不得改动其他已有的任何内容。

测试样例:
输入:
1 2 2 3
输出:
c1的值为:(1, 2)
c2的值为:(2, 3)
c3的值为:(0, 0)
c1!=c2

c3=++c1的值为:(2, 3)
c1==c2

试题程序: 
*/
#include <iostream>
#include<fstream>
using namespace std;

/*******Begin*******/
class Complex {	
	public:	
		Complex(int r = 0, int i = 0): real(r), imag(i) { }	// 构造函数
		bool operator==(const Complex &c2) const;
		friend Complex& operator++(Complex &c);
		friend ostream& operator<<(ostream &,const Complex &);
	private:
		int real;	// 复数实部
		int imag;  // 复数虚部
};

Complex& operator++(Complex &c){
	c.real++;
	c.imag++;
	return c;
} 

bool Complex::operator==(const Complex &c2) const{
	if(this->real==c2.real&&this->imag==c2.imag) return true;
	return false;
}

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

ostream & operator <<(ostream &out, const Complex &c) { //运算符<<重载函数实现
	out << "(" << c.real << ", " << c.imag << ")";
	return out;
}

int main() {
	int r1,i1,r2,i2;
	cin>>r1>>i1>>r2>>i2;
	Complex c1(r1,i1),c2(r2,i2),c3;
	cout<<endl;
	cout<<"c1的值为:"<<c1<<endl;
	cout<<"c2的值为:"<<c2<<endl;
	cout<<"c3的值为:"<<c3<<endl;
	if(c1==c2) cout<<"c1==c2"<<endl; 
	else cout<<"c1!=c2"<<endl;
	cout<<endl;
	c3=++c1;
	cout<<"c3=++c1的值为:"<<c3<<endl;
	if(c1==c2) cout<<"c1==c2"<<endl; 
	else cout<<"c1!=c2"<<endl;	
	ifstream in1("8.2.1.1-s3.in");
	ofstream out1("8.2.1.1-s3.out");
	streambuf *cinbackup;
	streambuf *coutbackup;
	cinbackup=cin.rdbuf(in1.rdbuf());
	coutbackup=cout.rdbuf(out1.rdbuf());
	while(cin>>r1>>i1>>r2>>i2) {
		Complex c1(r1,i1),c2(r2,i2),c3;
		cout<<"c1的值为:"<<c1<<endl;
		cout<<"c2的值为:"<<c2<<endl;
		cout<<"c3的值为:"<<c3<<endl;
		if(c1==c2) cout<<"c1==c2"<<endl; 
		else cout<<"c1!=c2"<<endl;
		c3=operator ++(c1);
		cout<<"c3=++c1的值为:"<<c3<<endl;
		if(c1.operator ==(c2)) cout<<"c1==c2"<<endl; 
		else cout<<"c1!=c2"<<endl;
		cout<<endl;
	}
	cin.rdbuf(cinbackup);
	cout.rdbuf(coutbackup);
	in1.close();
	out1.close();
	return 0;
}

2

/*
时钟(Clock)类成员声明如下:
class Clock	{	
	public:	
		Clock(int h=0,int m=0,int s=0); //构造函数,h, m, s分别用于初始化时钟的时、分、秒
		void setTime(int newH,int newM,int newS); // 设置时钟的时、分、秒 
	protected:	
		int hour,minute,second; // 时钟的时、分、秒(24小时制)
};
请根据注释,在完善Clock类定义的基础上,完成以下运算符重载:
(1)以成员函数形式重载前置“++”运算,实现时钟秒数(second)加1的操作。
(2)将运算符“==”重载为非成员函数形式,用于判断两个时钟对象是否相等(若相等则返回true,否则返回false)。
(3)重载“<<”运算符,用于时钟对象的输出,输出格式为“时:分:秒”。

注意:部分源程序已给出,仅允许在注释“Begin”和“End”之间补全代码,不得改动其他已有的任何内容。

测试样例:
输入:
0 0 0
0 0 0
输出:
时钟c1:0:0:1
时钟c2:0:0:1
时钟c1与c2相等!
重置后时钟c2:0:0:0
时钟c1与c2不相等!

试题程序: 
*/
#include<iostream>
#include<fstream>
using namespace std;

/*******Begin*******/
class Clock	{	
	public:	
		Clock(int h=0,int m=0,int s=0){
			this->hour=h;this->minute=m;this->second=s;
		} //构造函数,h, m, s分别用于初始化时钟的时、分、秒
		void setTime(int newH,int newM,int newS){
			this->hour=newH;this->minute-newM;this->second=newS;
		} // 设置时钟的时、分、秒 
		Clock& operator++();
		friend bool operator==(const Clock &, const Clock &);
		friend ostream & operator<<(ostream&, const Clock &); 
	protected:	
		int hour,minute,second; // 时钟的时、分、秒(24小时制)
};

Clock & Clock::operator++(){
	second++;
	if(second>=60){
		second-=60;
		minute++;
		if(minute>=60){
			minute-=60;
			hour=(hour+1)%24;
		}
	}
	return *this;
}

bool operator==(const Clock &c1, const Clock &c2){
	if(c1.hour==c2.hour&&c1.minute==c2.minute&&c1.second==c2.second)return true;
	return false;
}

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

ostream & operator <<(ostream &out, const Clock &c) {	// 运算符<<重载
	out << c.hour << ":" << c.minute << ":" << c.second;
	return out;
}

int main()
{
	int h,m,s;
	cin>>h>>m>>s;
	Clock c1(h,m,s),c2=++c1;	
	cout<<"时钟c1:"<<c1<<endl;
	cout<<"时钟c2:"<<c2<<endl;
	if(operator ==(c1,c2)) cout<<"时钟c1与c2相等!"<<endl;
	else cout<<"时钟c1与c2不相等!"<<endl; 
	cin>>h>>m>>s;
	c2.setTime(h,m,s); 
	cout<<"重置后时钟c2:"<<c2<<endl;
	if(c1==c2) cout<<"时钟c1与c2相等!"<<endl;
	else cout<<"时钟c1与c2不相等!"<<endl; 

	ifstream in1("8.2.2.5_1-s1_in.dat");
	ofstream out1("8.2.2.5_1-s1_out.dat");
	streambuf *cinbackup;
	streambuf *coutbackup;
	cinbackup=cin.rdbuf(in1.rdbuf());
	coutbackup=cout.rdbuf(out1.rdbuf());
	while(cin>>h>>m>>s) {
		Clock c1(h,m,s),c2=c1.operator ++(); 
		cout<<"时钟c1:"<<c1<<endl;
		cout<<"时钟c2:"<<c2<<endl;
		if(operator ==(c1,c2)) 
			cout<<"时钟c1与c2相等!"<<endl;
		else cout<<"时钟c1与c2不相等!"<<endl; 
		cin>>h>>m>>s;
		c2.setTime(h,m,s); 
		cout<<"重置后时钟c2:"<<c2<<endl;
		if(c1==c2) cout<<"时钟c1与c2相等!"<<endl;
		else cout<<"时钟c1与c2不相等!"<<endl;
		cout<<endl;
	}
	cin.rdbuf(cinbackup);
	cout.rdbuf(coutbackup);
	in1.close();
	out1.close();
 	return 0;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值