实验六
实验六1
源码
#include<iostream>
using namespace std;
class chip{
protected:
int m;
int n;
double result;
public:
chip(int a,int b,int c=0):m(a),n(b),result(c){}
void addition(){
cout<<m<<"+"<<n<<"=";
result=m+n;
cout<<result<<endl;
result=0;
}
};
class chip_A:public chip{
public:
chip_A(int a,int b,int c=0):chip(a,b,c){}
void subraction(){
cout<<m<<"-"<<n<<"=";
result=m-n;
cout<<result<<endl;
result=0;
}
};
class chip_B:public chip{
public:
chip_B(int a,int b,int c=0):chip(a,b,c){}
void mutilp(){
cout<<m<<"*"<<n<<"=";
result=m*n;
cout<<result<<endl;
result=0;
}
};
class chip_C:public chip{
public:
chip_C(int a,int b,int c=0):chip(a,b,c){}
void chufa(){
cout<<m<<"/"<<n<<"=";
result=(double)m/(double)n;
cout<<result<<endl;
result=0;
}
};
int main(){
chip_A chip1(4,2);
chip1.addition();
chip1.subraction();
chip_B chip2(3,4);
chip2.mutilp();
chip_C chip3(6,4);
chip3.chufa();
return 0;
}
运行结果:
实验六2
源码:
#include<iostream>
using namespace std;
class vehicle{
private:
int maxspeed;
int weight;
public:
vehicle(int a,int b):maxspeed(a),weight(b){ }
void run(){
cout<<"running by"<<maxspeed<<endl;
}
void stop(){
cout<<"stop"<<endl;
}
~vehicle(){}
};
class bicycle:virtual public vehicle{
private:
int height;
public:
bicycle(int a,int b,int c):vehicle(a,b),height(c){}
~bicycle(){}
};
class motorcar:virtual public vehicle{
private:
int seatnum;
public:
motorcar(int a,int b,int c):vehicle(a,b),seatnum(c){}
~motorcar(){}
};
class motorcycle:public bicycle,public motorcar{
public:
motorcycle(int a,int b,int c,int d):bicycle(a,b,c),motorcar(a,b,d),vehicle(a,b){ }
~motorcycle(){}
};
int main(){
bicycle A(10,5,3);
A.run();
A.stop();
motorcar B(10,4,4);
B.run();
B.stop();
motorcycle C(10,5,4,4);
C.run();
C.stop();
return 0;
}
运行结果:
实验六3
项目二:
源码:
类的定义
class Fraction{
public:
Fraction(int a=0,int b=1 ):top(a),bottom(b){}//构造函数
Fraction operator+(const Fraction &a);
Fraction operator-(const Fraction &b);
Fraction operator*(const Fraction &c);
Fraction operator/(const Fraction &d);//运算符重载函数;
void output(); //输出函数
void compare(Fraction &a);//比较函数;
private:
int top;//分子;
int bottom;//分母;
};
类的实现
#include"Fraction.h"
#include<iostream>string gon()
#include<cmath>
using namespace std;
Fraction Fraction::operator+(const Fraction &a){
return Fraction(top*a.bottom+a.top*bottom,a.bottom*bottom);
}
Fraction Fraction::operator-(const Fraction &b){
return Fraction(top*b.bottom-b.top*bottom,bottom*b.bottom);
}
Fraction Fraction::operator*(const Fraction &c){
return Fraction(top*c.top,bottom*c.bottom);
}
Fraction Fraction::operator/(const Fraction &d){
return Fraction(top*d.bottom,bottom*d.top);
}
void Fraction::output(){
int t,x,y;
if(abs(top)>abs(bottom))
t=abs(bottom);
else
t=abs(top);
for(int i=1;i<t;i++){
if(top%i==0&&bottom%i==0){
top/=i;
bottom/=i;
i=1;
}
}
if(bottom<0){
top*=-1;
bottom*=-1;
}
cout<<top<<"/"<<bottom<<endl;
}
主函数
#include"Fraction.h"
#include<iostream>
using namespace std;
int main(){
Fraction a(1,-6);
Fraction b(4,-6);
Fraction c;
c=a+b;
c.output();
c=a*b;
c.output();
c=a/b;
c.output();
c=a-b;
c.output();
return 0;
}
运行结果:
总结
实验三中因为不明白老师所要求的派生类是什么意思,就没有写。对于函数的多态还有继承需要更多的尝试和理解。