C++程序设计实践-6

 

#include<iostream>
using namespace std;
class base{
	public:
	base(int a,int b);
	void add();
	private:
		int m;
		int n;
}; 

class chipA:public base{
	public:
		chipA(int a,int b);
		void min();
	private:
		int m;
		int n;
			
};

class chipB:public base{
	public:
		chipB(int a,int b);
		void tim();
	private:
		int m;
		int n;
};

class chipC:public base{
	public:
		chipC(int a,int b);
		void div();
	private:
		int m;
		int n;
};

base::base(int a,int b):m(a),n(b){
};
void base::add(){
	cout<<m+n<<endl;
}

chipA::chipA(int a,int b):base( a, b),m(a),n(b){
};
void chipA::min(){
	cout<<m-n<<endl;
};

chipB::chipB(int a,int b):base( a, b),m(a),n(b){
};
void chipB::tim(){
	cout<<m*n<<endl;
};

chipC::chipC(int a,int b):base( a, b),m(a),n(b){
};
void chipC::div(){
	cout<<m/n<<endl;
};

int main(){
	chipA A(5,2);
	A.add();
	A.min();
	
	chipB B(6,3);
	B.add();
	B.tim();
	
	chipC C(7,4);
	C.add();
	C.div();
	
	return 0;
}













 

错误:

改正:

#include<iostream>

 

编程时发现了以上错误,学习过程发现这个error成因很复杂,但尝试这样修改则编译通过,但仍不知错误原因,希望有同学帮我解答。

 

 

 

 
#include<iostream>
using namespace std;

//类的定义 
class vehicle{
public:
     vehicle(int max,int wei);
     void run();
     void stop();
     int maxspeed;
     int weight;
     ~vehicle();
};

class bicycle:virtual public vehicle{
public:
     bicycle(int max,int wei,int hei);
     int height;
     ~bicycle();
};

class motorcar:virtual public vehicle{
public:
     motorcar(int max,int wei,int sea);
     int seatnum;
     ~motorcar();
};

class motorcycle:public bicycle,public motorcar{
public:
     motorcycle(int max,int wei,int hei,int sea);
    ~motorcycle();
};

//类的实现 
vehicle::vehicle(int max,int wei):maxspeed(max),weight(wei){
     cout<<maxspeed<<" "<<weight<<" "<<"constructing vehicle"<<endl;
}

void vehicle::run(){
     cout<<"run"<<endl;
}

void vehicle::stop(){
     cout<<"stop"<<endl;
}

vehicle::~vehicle(){
     cout<<"destructing vehicle"<<endl;
}

bicycle::bicycle(int max,int wei,int hei):vehicle(max,wei),height(hei){
     cout<<maxspeed<<" "<<weight<<" "<<height<<" "<<"constructing vehicle"<<endl;
}

bicycle::~bicycle(){
     cout<<"destructing bicycle"<<endl;
}

motorcar::motorcar(int max,int wei,int sea):vehicle(max,wei),seatnum(sea){
     cout<<maxspeed<<" "<<weight<<" "<<seatnum<<" "<<"constructing motorcar"<<endl;
}

motorcar::~motorcar(){
     cout<<"destructing motorcar"<<endl;
}

motorcycle::motorcycle(int max,int wei,int hei,int sea):vehicle(max,wei),bicycle(max,wei,hei),motorcar(max,wei,sea){
     cout<<maxspeed<<" "<<weight<<" "<<height<<" "<<seatnum<<" "<<"constructing motorcycle"<<endl;
}

motorcycle::~motorcycle(){
     cout<<"destructing motorcycle"<<endl;
}

//主函数 
int main(){
     motorcycle mot(250,30,1,2);
     mot.run();
     mot.stop();
     return 0;
}

 

这一题我尝试把62行改为以下形式(想写少一点),编译一大堆报错,证明类的多重继承必须这样写:

 

 

 

 

 

 

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

//类的定义
class Fraction {
	public:
		Fraction(int a=0,int b=1):top(a),bottom(b){}
		Fraction operator+(const Fraction &c1);
		Fraction operator-(const Fraction &c2);
		Fraction operator*(const Fraction &c3);
		Fraction operator/(const Fraction &c4);
		void output();
	private:
		int top;
		int bottom;
};

//类的实现
Fraction Fraction::operator+(const Fraction &c1){
	return Fraction(top*c1.bottom+bottom*c1.top,bottom*c1.bottom);
}

Fraction Fraction::operator-(const Fraction &c2){
	return Fraction(top*c2.bottom-bottom*c2.top,bottom*c2.bottom);
}

Fraction Fraction::operator*(const Fraction &c3){
	return Fraction(top*c3.top,bottom*c3.bottom);
}

Fraction Fraction::operator/(const Fraction &c4){
	return Fraction(top*c4.bottom,bottom*c4.top);
}

void Fraction::output(){
    cout<<top<<"/"<<bottom<<endl;
}


//主函数
int main(){
	Fraction aa(1,3);
	Fraction bb(1,4);
	Fraction cc;
	cc=aa+bb;
	cc.output();
	cc=aa-bb;
	cc.output();
	cc=aa*bb;
	cc.output();
	cc=aa/bb;
	cc.output();
	return 0;
}

 


 

 

 

这次实验更加注重代码的简洁性和规范性。实验过程遇到了一些问题,都写在上面了,希望老师同学能帮我解答。

 
<strong>
</strong>
 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值