pta第六章错题函数and编程

目录

6-6 创建计算机 (10 分)

6 - 2 时钟模拟

+ 运算符重载:

6-3 大整数求和(运算符重载)


6-6 创建计算机 (10 分)

定义一个简单的Computer类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,有两个公有成员函数run、stop。cpu为CPU类的一个对象,ram为RAM类的一个对象,cdrom为CDROM类的一个对象,定义并实现这个类,为以上的类编写构造和析构函数,注意使用类组合的思想解决该问题,使得给出的主函数代码可以正确运行并得到给出的输出结果。

裁判测试程序样例:

int main()
{
    COMPUTER cpt1(6,4.0,200,60,32);  //测试带参数构造
    cout<<"cpt1's parameter:"<<endl;
    cpt1.showinfo();
    cout<<"------------------------------"<<endl;
    COMPUTER cpt2; //测试不带参数构造
    cout<<"cpt2's parameter:"<<endl;
    cpt2.showinfo();
    cout<<"------------------------------"<<endl;
    COMPUTER cpt3(cpt1); //测试复制构造
    cout<<"cpt3's parameter:"<<endl;
    cpt3.showinfo();
    cout<<"------------------------------"<<endl;    
}

输出样例:

create a CPU!
create a RAM!
create a CDROM!
create a COMPUTER with para!
cpt1's parameter:
cpu parameter:
rank:6
frequency:4
voltage:200
ram parameter:
volumn:60 GB
cdrom parameter:
speed:32
------------------------------
create a CPU!
create a RAM!
create a CDROM!
no para to create a COMPUTER!
cpt2's parameter:
cpu parameter:
rank:1
frequency:2
voltage:100
ram parameter:
volumn:1 GB
cdrom parameter:
speed:16
------------------------------
create a CPU by copy!
create a RAM by copy!
create a CDROM by copy!
create a COMPUTER by copy!
cpt3's parameter:
cpu parameter:
rank:6
frequency:4
voltage:200
ram parameter:
volumn:60 GB
cdrom parameter:
speed:32
------------------------------
destruct a COMPUTER!
destruct a CDROM!
desturct a RAM!
desturct a CPU!
destruct a COMPUTER!
destruct a CDROM!
desturct a RAM!
desturct a CPU!
destruct a COMPUTER!
destruct a CDROM!
desturct a RAM!
desturct a CPU!

my answer: 

#include<iostream>
#include<string>
using namespace std;
class COMPUTER
{
	public:
		COMPUTER(int Rank, int Frequency, int Voltage, int Volumn, int Speed):rank(Rank), frequency(Frequency), voltage(Voltage), volumn(Volumn), speed(Speed)
		{
			cout << "create a CPU!" << endl;
			cout << "create a RAM!" << endl;
			cout << "create a CDROM!" << endl;
			cout << "create a COMPUTER with para!" << endl;
		 }
		 COMPUTER():rank(1), frequency(2), voltage(100), volumn(1), speed(16)
		 {
		 	cout << "create a CPU!" << endl;
			cout << "create a RAM!" << endl;
			cout << "create a CDROM!" << endl;
			cout << "no para to create a COMPUTER!" << endl;
		 }
		 COMPUTER(const COMPUTER& s);
		 showinfo();
		 ~ COMPUTER();
	
	private:
		int rank;
		int frequency;
		int voltage;
		int volumn;
		int speed;
		
};
COMPUTER :: COMPUTER(const COMPUTER& s)
{
	cout <<"create a CPU by copy!"<<endl;
	cout <<"create a RAM by copy!"<<endl;
	cout <<"create a CDROM by copy!"<<endl;
	cout <<"create a COMPUTER by copy!"<<endl;
	rank = s.rank;
	frequency = s.frequency;
	voltage = s.voltage;
	volumn = s.volumn;
	speed = s.speed;
}
COMPUTER :: showinfo()
{
	cout <<"cpu parameter:" << endl;
	cout <<"rank:" << rank << endl;
	cout <<"frequency:" << frequency<< endl;
	cout <<"voltage:" << voltage <<endl;
	cout <<"ram parameter:" << endl;
	cout <<"volumn:" << volumn;
	cout<< " GB"<< endl;
	cout <<"cdrom parameter:" << endl;
	cout <<"speed:" << speed <<endl;
}
COMPUTER :: ~COMPUTER()
{
	cout << "destruct a COMPUTER!" << endl;
	cout << "destruct a CDROM!" << endl;
	cout << "desturct a RAM!" << endl;
	cout << "desturct a CPU!" << endl;
}

/* 请在这里填写答案 */

//在这里给出函数被调用进行测试的例子。例如:

int main()
{
    COMPUTER cpt1(6,4.0,200,60,32);  //测试带参数构造
    cout<<"cpt1's parameter:"<<endl;
    cpt1.showinfo();
    cout<<"------------------------------"<<endl;
    COMPUTER cpt2; //测试不带参数构造
    cout<<"cpt2's parameter:"<<endl;
    cpt2.showinfo();
    cout<<"------------------------------"<<endl;
    COMPUTER cpt3(cpt1); //测试复制构造
    cout<<"cpt3's parameter:"<<endl;
    cpt3.showinfo();
    cout<<"------------------------------"<<endl;    
}

明明在dev上编译通过,但在pta上却是编译错误,呜呜呜,平台问题,之后老师说要带头文件;

壹.如果成员函数没有编辑返回值,在dev上无编译错误,也无提示出错

贰:删掉了拷贝构造函数中地参数名

叁:将构造函数中类的地址换为数据成员地形参值

THE SECONG

运用含有对象成员的构造函数 

#include<iostream>
#include<string>
using namespace std;

class CPU
{
	public:
		CPU(int Rank, int Frequency, int Voltage):rank(Rank), frequency(Frequency), voltage(Voltage)
		{
			cout << "create a CPU!" << endl;
		}//带参数的构造函数 
		CPU(const CPU& );//拷贝构造函数 
		~CPU();//析构函数 
		//无参构造函数 
	private:
		int rank;
		int frequency;
		int voltage;
};


class RAM
{
	public:
		RAM(int Volumn):volumn(Volumn)
		{
			cout << "create a RAM!" << endl;
		}
		RAM(const RAM&);
		~RAM();
	private:
		int volumn;
};

class CDROM
{
	public:
		CDROM(int Speed):speed(Speed)
		{
			cout << "create a CDROM!" << endl;
		}
		CDROM(const CDROM&);
		~CDROM();
	private:
		int speed;
};





class COMPUTER
{
	public:
		COMPUTER(int Rank, int Frequency, int Voltage, int Volumn, int Speed):cpu(Rank,Frequency,Voltage), ram(Volumn), cdrom(Speed)
		{	
			cout << "create a COMPUTER with para!" << endl;
		 }
		 COMPUTER():cpu(1, 2, 100), ram(1), cdrom(16)
		 {
		 	cout << "create a CPU!" << endl;
			cout << "create a RAM!" << endl;
			cout << "create a CDROM!" << endl;
			cout << "no para to create a COMPUTER!" << endl;
		 }
		 COMPUTER(const COMPUTER&);
		 void showinfo();
		 ~ COMPUTER();
	private:
			CPU cpu;
			RAM ram;
			CDROM cdrom;	
};

CPU :: CPU(const CPU& copy_CPU)
{
	rank = copy_CPU.rank;
	frequency = copy_CPU.frequency;
	voltage = copy_CPU.voltage ;
}
RAM :: RAM(const RAM& copy_RAM)
{
	volumn = copy_RAM.volumn;
}
CDROM :: CDROM(const CDROM& copy_COROW)
{
	speed = copy_COROW.speed;
}
COMPUTER :: COMPUTER(const COMPUTER& s)
{
	cout <<"create a CPU by copy!"<<endl;
	cout <<"create a RAM by copy!"<<endl;
	cout <<"create a CDROM by copy!"<<endl;
	cout <<"create a COMPUTER by copy!"<<endl;
	CPU=s.cpu;
	RAM=s.ram;
	CDROM=s.cdrom;
}
void COMPUTER :: showinfo()
{
	cout <<"cpu parameter:" << endl;
	cout <<"rank:" << cpu.rank << endl;
	cout <<"frequency:" << cpu.frequency<< endl;
	cout <<"voltage:" << cpu.voltage <<endl;
	cout <<"ram parameter:" << endl;
	cout <<"volumn:" << ram.volumn;
	cout<< " GB"<< endl;
	cout <<"cdrom parameter:" << endl;
	cout <<"speed:" << cdrom.speed <<endl;
}
COMPUTER :: ~COMPUTER()
{
	cout << "destruct a COMPUTER!" << endl;	
}
CDROM :: ~CDROM()
{
	cout << "destruct a CDROM!" << endl;
}
RAM :: ~RAM()
{
	cout << "desturct a RAM!" << endl;
}
CPU :: ~CPU()
{
	cout << "desturct a CPU!" << endl;
}
int main()
{
    COMPUTER cpt1(6,4.0,200,60,32);  //???????
    cout<<"cpt1's parameter:"<<endl;
    cpt1.showinfo();
    cout<<"------------------------------"<<endl;
    COMPUTER cpt2; //????????
    cout<<"cpt2's parameter:"<<endl;
    cpt2.showinfo();
    cout<<"------------------------------"<<endl;
    COMPUTER cpt3(cpt1); //??????
    cout<<"cpt3's parameter:"<<endl;
    cpt3.showinfo();
    cout<<"------------------------------"<<endl;    
}

1.89    39    E:\c++ 第三章6-6.cpp    [Error] no matching function for call to 'CPU::CPU()';没有调用CPU::CPU()函数,尝试放在COMPUTER的前面或后面结果都不通过;

THE THIRD

1.创建拷贝构造函数时,如果类A中含有类型是B的对象成员,需要在类B中构建一个无参构造函数,因为在调用拷贝构造函数时,系统会为拷贝构造函数的形参创建一个内存空间暂时储存形参,拷贝构造函数的形参类型是类B而且没有提供参数,因此需要在类B中调用拷贝构造函数。

2.最初构想是在COMPUTER中编辑show函数,然后用对象成员.数据成员的方式调用数据,这样便捷,但编译错误[Error] no matching function for call to 'CPU::CPU()';没有调用CPU::CPU()函数,;改用在RAM,RDOW,CPU中编辑构造函数,然后在COMPUTER类中编辑调用三个类中输出函数的成员函数。

3.COMPUTER类中的无参构造函数中,输出的CPU,RAM,RDOW其他类的有参构造函数也存在,会造成重复。

6 - 2 时钟模拟

一个Time类,数据成员有时、分、秒。要求模拟秒表,每次走一秒,满60秒进位,秒又从零开始计数。满60分进位,分又从零开始计数。输出时、分和秒的值。(使用重载++运算符实现)

时间类定义class MyTime

测试程序样例:/* 请在这里填写答案 */ int main() { MyTime t1,t2(23,59,59),t3; cin>>t3; ++t1; cout<<t1<<endl; ++t2; cout<<t2<<endl; ++t3; cout<<t3<<endl; return 0; }

输入样例:12 35 59

输出样例:0:0:1
                0:0:0
                12:36:0

#include<iostream>

using namespace std;

class MyTime
{
	public:
		MyTime operator++();
		MyTime(int a,int b, int c);
		MyTime()
		{
			hour = 0;
			min = 0;
			second = 0;
		}
		friend istream& operator>> (istream&, MyTime&);
		friend ostream& operator<< (ostream&, MyTime&);
		
	private:
		int hour;
		int second;
		int min;
};
MyTime :: MyTime(int a,int b, int c) : hour(a),min(b),second(c)
		{
		}
MyTime MyTime :: operator++()
{
	second++;
	if(second >= 60)
	{
		 min = min + second / 60;
		 second %= 60; 
	}
	if(min >= 60)
	{
		 hour = hour + min / 60;
		 min %= 60; 
	}
	hour %=24;
}
istream& operator>>(istream& is, MyTime& s)
{
	is>>s.hour>>s.min>>s.second;
	return is;
}
ostream& operator<<(ostream& os, MyTime& s)
{
	os << s.hour << ":" << s.min << ":" << s.second;
	return os;
}


/* 请在这里填写答案 */

int main()
{
    MyTime t1,t2(23,59,59),t3;
    cin>>t3;
    ++t1;
    cout<<t1<<endl;
    ++t2;
    cout<<t2<<endl;
    ++t3;
    cout<<t3<<endl;
    return 0;
}

在声明operator<<和operator>>时,错误地声明了两个operator>>,因此重载operator<<虽然被正确定义,但在程序看了来是类外普通函数,类外没有权限访问私有成员,只有在类内才可以访问类的私有成员。

+ 运算符重载:

BigInt  BigInt :: operator+(BigInt &s)
{
	num = num + s.num;
	return *this;
}

这样如果在后面再次调用该对象的num成员数据,就是进行加法之后的num,无法使用加法前的num数据成员

6-3 大整数求和(运算符重载)

BigInt类表示不超过100位的无符号大整数。试重载>>,<<和+,以支持无符号大整数的输入、输出与求和(假设结果仍是一个不超过100位的无符号大整数)。

重载面向BigInt类对象的运算符:>> << +

裁判测试程序样例:

#include <iostream>

#include <string>

using namespace std; 

int main()

{

BigInt a, b, c;

cin>>a>>b;

c=a+b;

cout<<a<<"+"<<b<<"="<<c<<endl;

return 0;

}

输入样例:

123456789
987654321

输出样例:

123456789+987654321=1111111110

answer: 

#include <iostream>
#include <string>
using namespace std;
class BigInt
{
	public:
		friend istream& operator>> (istream&, BigInt&);
		friend ostream& operator<< (ostream&, BigInt&);
		BigInt operator+(BigInt&); 
		BigInt operator= ( const BigInt&);
		BigInt swap();
		BigInt()
		{
			a = new int [100];
			leng = 0;
		}
	private:
		string num;
		int *a;
		int leng;
};
BigInt BigInt :: swap()
{
	leng = num.length() ;
	for(int i = leng - 1; i >= 0; i--)
	{
		a[leng - i - 1] = num[i] - '0';
	}
	for(int i = leng; i < 100; i++)
	{
		a[i] = 0;
	}
	cout<<leng<<endl;
}
istream& operator>>(istream& is, BigInt& s)
{
	is>>s.num;
	return is;
}
ostream& operator<< (ostream& os, BigInt& s)
{
	os<<s.num;
	return os;
}
BigInt  BigInt :: operator+(BigInt &s)
{
	
	BigInt b;
	swap();
	s.swap();
	int lb = 0, ls = 0, la = 0;
	int l = 0;
	while(la < leng || ls < s.leng)
	{
		b.a[lb] = a[la] + s.a[ls] + l;
		l = b.a[lb] / 10;
		b.a[lb] %= 10;
		lb++;
		la++;
		ls++; cout<<"OK"<<endl;
	}
	while(l > 0)
	{
		b.a[lb] += l;
		l = b.a[lb] / 10;
		b.a[lb] %= 10;
		lb++;
	}
	b.leng = lb;
	for( int j = b.leng - 1; j >= 0; j--)
	{
		b.num += b.a[j] +'0';
	}
	return b;
}
BigInt BigInt :: operator= ( const BigInt&s)
{
	if(this == &s) return *this;
	num = s.num;
	return *this;
}

在重载运算符+时,并没有对s中的*a赋值;找到原因了,在对swap的声明和定义中,把swap的类型定义为类,就成了重载函数,那么没有对象的调用重载函数,就不会有任何事情发生。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值