#include<iostream>
using namespace std;
class Base {
public:
Base(int m,int n):m(m),n(n){}
void plus();
private:
int m, n;
};
void Base::plus() {
cout << "m+n=" << m + n << endl;
}
class A :public Base {
public:
A(int a,int b):Base(a,b),m1(a),n1(b){}
void min();
private:
int m1, n1;
};
void A::min() {
cout << "m-n=" << m1 - n1 << endl;
}
class B :public Base {
public:
B(int a, int b) :Base(a, b), m2(a), n2(b) {}
void mul();
private:
int m2, n2;
};
void B::mul() {
cout << "m*n=" << m2 * n2 << endl;
}
class C :public Base {
public:
C(int a,int b):Base(a,b),m3(a),n3(b){}
void div();
private:
int m3, n3;
};
void C::div() {
cout << "m/n=" << m3 / n3 << endl;
}
int main() {
Base(2, 1);
A a(2,1);
B b(2,1);
C c(2,1);
a.plus();
a.min();
b.plus();
b.mul();
c.plus();
c.div();
return 0;
}
#include<iostream>
using namespace std;
class vehicle {
public:
vehicle(int m, int w) :maxspeed(m), weight(w) {}
~vehicle();
void run();
void stop();
private:
int maxspeed, weight;
};
vehicle::~vehicle() {
cout << "Function is over" << endl;
}
void vehicle::run() {
cout << "RUN" << endl;
}
void vehicle::stop() {
cout << "STOP" << endl;
}
class bicycle :virtual public vehicle {
public:
bicycle(int m, int w, int h) :vehicle(m, w), height(h) {}
~bicycle();
private:
int height;
};
bicycle::~bicycle() {
cout << "Function is over" << endl;
}
class motorcar :virtual public vehicle {
public:
motorcar(int m, int w, int s) :vehicle(m, w), seatnum(s) {}
~motorcar();
private:
int seatnum;
};
motorcar::~motorcar() {
cout << "Function is over" << endl;
}
class motorcycle :public bicycle, public motorcar {
public:
motorcycle(int m, int w, int h, int s) :vehicle(m, w), bicycle(m, w, h), motorcar(m, w, s) {}
~motorcycle();
};
motorcycle::~motorcycle(){
cout << "Funtion is over" << endl;
}
int main() {
bicycle a(1, 1, 1);
motorcar b(1, 1, 1);
motorcycle c(1, 1, 1, 1);
a.run();
a.stop();
b.run();
b.stop();
c.run();
c.stop();
return 0;
}
//fraction.h
#pragma once
class Fraction {
public:
Fraction(int t, int b) :top(t), bottom(b) {};
Fraction(int t) :top(t), bottom(1) {};
Fraction() :top(0), bottom(1) {};
Fraction operator+(Fraction &f);
Fraction operator-(Fraction &f);
Fraction operator*(Fraction &f);
Fraction operator/(Fraction &f);
void compare(Fraction &pf1, Fraction &f2);
void show();
private:
int top,bottom;
};
//fraction.cpp
#include<iostream>
#include"fraction.h"
using namespace std;
Fraction Fraction::operator+(Fraction &f) {
Fraction a;
a.top = top * f.bottom + bottom * f.top;
a.bottom = bottom * f.bottom;
return a;
}
Fraction Fraction::operator-(Fraction &f) {
Fraction a;
a.top = top * f.bottom - bottom * f.top;
a.bottom = bottom * f.bottom;
return a;
}
Fraction Fraction::operator*(Fraction &f) {
Fraction a;
a.top = top * f.top;
a.bottom = bottom * f.bottom;
return a;
}
Fraction Fraction::operator/(Fraction &f) {
Fraction a;
a.top = top * f.bottom;
a.bottom = bottom * f.top;
return a;
}
void Fraction::compare(Fraction &f1, Fraction &f2) {
if (f1.top / f1.bottom>f2.top / f2.bottom)
cout << "f1>f2" << endl;
else if (f1.top / f1.bottom<f2.top / f2.bottom)
cout << "f1<f2" << endl;
else if (f1.top / f2.bottom == f2.top / f2.bottom)
cout << "f1=f2" << endl;
}
void Fraction::show() {
cout << top << "/" << bottom << endl;
}
//ifraction.h
#pragma once
#include<iostream>
#include”fraction.h”
using namespace std;
class iFraction :public Fraction {
public:
iFraction(int t, int b) :Fraction(t, b),top(t),bottom(b) {};
iFraction(int t) :Fraction(t),top(t),bottom(1){};
iFraction() :Fraction(),top(0),bottom(1) {};
private:
int top, bottom;
};
//main.cpp
#include<iostream>
#include"fraction.h"
#include"ifraction.h"
using namespace std;
int main() {
Fraction a;
Fraction b(1);
Fraction c(1, 2);
Fraction d, e, f, g;
d = b + c;
e = b - c;
f = b * c;
g = b / c;
a.show();
b.show();
c.show();
d.show();
e.show();
f.show();
g.show();
return 0;
}