实验6

11111111111111111111111111111111111111111111111111

#include<iostream>
using namespace std;
class xinpian0{
private:
int a,b;
public:
xinpian0(int a=0,int b=0):a(a),b(b){
}
void add(int x,int y){
a=x;b=y;
cout<<x+y<<endl;
}
void add(){
cout<<a+b<<endl;
}
};
class xinpian1:public xinpian0{
private:
int c,d;
public:
xinpian1(int c=0,int d=0):c(c),d(d){
}
void sub(int x,int y){
c=x,d=y;
cout<<x-y<<endl;
}
void sub(){
cout<<c-d<<endl;
}
};
class xinpian2:public xinpian0{
private:
int c,d;
public:
xinpian2(int c=0,int d=0):c(c),d(d){
}
void mul(int x,int y){
c=x,d=y;
cout<<x*y<<endl;
}
void mul(){
cout<<c*d<<endl;
}
};
class xinpian3:public xinpian0{
private:
int c,d;
public:
xinpian3(int c=0,int d=0):c(c),d(d){
}
void div(int x,int y){
c=x,d=y;
cout<<x/y<<endl;
}
void div(){
cout<<c/d<<endl;
}
};
int main(){
xinpian1 x1(9,3);
xinpian2 x2(9,3);
xinpian3 x3(9,3);
x1.add();
x1.sub();
x1.sub(4,2);
x2.mul();
x3.div();
return 0;
}

 

222222222222222222222222222222222222222

//声明与定义
#ifndef _VEHICLE_H_
#define _VEHICLE_H_
#include<iostream>
using namespace std;
class vehicle{
private:
int maxspeed;
int weigh;
public:
vehicle(int a=0,int b=0):maxspeed(a),weigh(b){
cout<<"constructing vehicle."<<endl;
}
~vehicle(){
cout<<"destructing vehicle."<<endl;
}
void run(){
cout<<"start to run this vehicle."<<endl;
}
void stop(){
cout<<"stop this vehicle."<<endl;
}
int show(){
cout<<maxspeed<<" "<<weigh<<endl;
}

};


class bicycle:virtual public vehicle{
private:
int maxspeed;
int weigh;
public:
bicycle(int a,int b){
vehicle(a,b);
cout<<"constructing bicycle."<<endl;
}
~bicycle(){
cout<<"destructing bicycle."<<endl;
}
void height(){
cout<<"describe the height."<<endl;
}
int show(){
cout<<maxspeed<<" "<<weigh<<endl;
}
void run(){
cout<<"start to run this bicycle."<<endl;
}
void stop(){
cout<<"stop this bicycle."<<endl;
}
};


class car:virtual public vehicle{
private:
int maxspeed;
int weigh;
public:
car(){
cout<<"constructing car."<<endl;
}
~car(){
cout<<"destructing car."<<endl;
}
void seatnum(){
cout<<"describe the seatnum."<<endl;
}
int show(){
cout<<maxspeed<<" "<<weigh<<endl;
}
void run(){
cout<<"start to run this car."<<endl;
}
void stop(){
cout<<"stop this car."<<endl;
}
};

class motorcycle:public bicycle,public car{
private:
int maxspeed;
int weigh;
public:
motorcycle(int x,int y ):bicycle(x,y){
cout<<"constructing motorcycle."<<endl;
}
~motorcycle(){
cout<<"destructing motorcycle."<<endl;
}
};
#endif

//main函数
#include"vehicle.h"
#include<iostream>
using namespace std;
int main(){
vehicle x0(500,500);
bicycle x1(500,100);
car x2;
motorcycle x3(600,200);
x0.run();
x0.stop();
x1.run();
x1.height();
x2.seatnum();
cout<<"1111111111111111"<<endl;


x3.height();
x3.seatnum();
return 0;
}

 


33333333333333333333333333333333333333333333333

感觉原题目代码过多,又从编了一个简化版的进行实验里基本内容的测试

#include<iostream>
using namespace std;
class fraction{
public:
fraction(int a=0,int b=1):top(a),bottom(b){}
~fraction(){cout<<"destructing"<<endl;}
void deal(){
for(int i=top;i>1;i--){
if(top%i==0&&bottom%i==0)
{top/=i;bottom/=i;break;}
}
}
void add(const fraction &p){
top=top*p.bottom+bottom*p.top;
bottom*=p.bottom;
deal();
}
int itop(){ return top;}
int ibottom(){return bottom; }
void show(){
cout<<top<<"/"<<bottom<<endl;
}
private:
int top;
int bottom;

};
class ifraction:public fraction{
public:
ifraction(int x=0,int y=1):fraction(x,y),x(x),y(y){
}
void add(const ifraction &p){
x=x*p.y+y*p.x;
y*=p.y;
}
void show(){
cout<<x<<"/"<<y<<endl;
}
friend ostream &operator<<(ostream &out,const ifraction &c);
friend ifraction operator+(ifraction &d,const ifraction &c);
private:
int x;
int y;
};
ifraction operator+(ifraction &d,const ifraction &c){
d.x=d.x*c.y+d.y*c.x;
d.y*=c.y;
}
ostream &operator<<(ostream &out,const ifraction &c){
out<<c.x<<"/"<<c.y ;
return out;
}
int main(){
ifraction c1(3,4);
ifraction c2(7,2);
c1+c2;
c1.show();
cout<<c1<<endl;
return 0;
}

 

posted @ 2018-06-06 20:36  鳄鱼加冕  阅读(101)  评论(0编辑  收藏  举报