实验1:
1 #pragma once 2 3 class add 4 { 5 private: 6 int x,y; 7 public: 8 add(int, int); 9 int add_result(); 10 }; 11 12 class subtract 13 { 14 private: 15 int x, y; 16 public: 17 subtract(int, int); 18 int subtract_result(); 19 }; 20 21 class A:public add,public subtract 22 { 23 private: 24 int x, y; 25 public: 26 A(int, int); 27 int getX(); 28 int getY(); 29 };
1 #pragma once 2 #include "A.h" 3 4 class multiply 5 { 6 private: 7 int x, y; 8 public: 9 multiply(int, int); 10 int multiply_result(); 11 }; 12 13 class B :public add, public multiply 14 { 15 private: 16 int x, y; 17 public: 18 B(int, int); 19 int getX(); 20 int getY(); 21 };
1 #pragma once 2 #include "A.h" 3 4 class devide 5 { 6 private: 7 int x, y; 8 public: 9 devide(int, int); 10 int devide_result(); 11 }; 12 13 class C :public add, public devide 14 { 15 private: 16 int x, y; 17 public: 18 C(int, int); 19 int getX(); 20 int getY(); 21 };
1 #include "A.h" 2 3 add::add(int m, int n):x(m),y(n){} 4 5 int add::add_result() 6 { 7 return x+y; 8 } 9 10 subtract::subtract(int m, int n) :x(m), y(n) {} 11 12 int subtract::subtract_result() 13 { 14 return x-y; 15 } 16 17 A::A(int m, int n) :x(m), y(n), add(m, n), subtract(m, n){} 18 19 int A::getX() 20 { 21 return x; 22 } 23 24 int A::getY() 25 { 26 return y; 27 }
#include "B.h" multiply::multiply(int m, int n) :x(m), y(n) {} int multiply::multiply_result() { return x*y; } B::B(int m, int n) :x(m), y(n), add(m, n), multiply(m, n) {} int B::getX() { return x; } int B::getY() { return y; }
1 #include "C.h" 2 3 devide::devide(int m, int n) :x(m), y(n) {} 4 5 int devide::devide_result() 6 { 7 return x/y; 8 } 9 10 C::C(int m, int n) :x(m), y(n), add(m, n), devide(m, n) {} 11 12 int C::getX() 13 { 14 return x; 15 } 16 17 int C::getY() 18 { 19 return y; 20 }
1 // 1.cpp: 定义控制台应用程序的入口点。 2 // 3 4 #include <iostream> 5 #include "A.h" 6 #include "B.h" 7 #include "C.h" 8 9 using namespace std; 10 11 int main() 12 { 13 A x1(1, 2); 14 cout << "A:" << endl; 15 cout << x1.getX() << "+" << x1.getY() << "=" << x1.add_result() << endl; 16 cout << x1.getX() << "-" << x1.getY() << "=" << x1.subtract_result() << endl; 17 18 B x2(4, 8); 19 cout << "B:" << endl; 20 cout << x2.getX() << "+" << x2.getY() << "=" << x2.add_result() << endl; 21 cout << x2.getX() << "*" << x2.getY() << "=" << x2.multiply_result() << endl; 22 23 C x3(16, 32); 24 cout << "C:" << endl; 25 cout << x3.getX() << "+" << x3.getY() << "=" << x3.add_result() << endl; 26 cout << x3.getX() << "/" << x3.getY() << "=" << x3.devide_result() << endl; 27 28 return 0; 29 }
实验2:
1 #pragma once 2 #include "vehicle.h" 3 4 class bicycle :virtual public vehicle 5 { 6 private: 7 int height; 8 public: 9 bicycle(int, int, int); 10 int get_height(); 11 };
1 #pragma once 2 #include "vehicle.h" 3 4 class motorcar :virtual public vehicle 5 { 6 private: 7 int seatnum; 8 public: 9 motorcar(int,int,int); 10 int get_seatnum(); 11 };
1 #pragma once 2 #include "bicycle.h" 3 #include "motorcar.h" 4 5 class motorcycle :public bicycle, public motorcar 6 { 7 private: 8 public: 9 motorcycle(int spe, int wei, int hei, int seat); 10 };
1 #pragma once 2 3 class vehicle 4 { 5 private: 6 int maxspeed, weight; 7 public: 8 vehicle(int, int); 9 void run(); 10 void stop(); 11 int get_maxspeed(); 12 int get_weight(); 13 };
1 #include "bicycle.h" 2 3 bicycle::bicycle(int x, int y, int z) :vehicle(x, y), height(z) {} 4 5 int bicycle::get_height() 6 { 7 return height; 8 }
1 #include "motorcar.h" 2 3 motorcar::motorcar(int x, int y, int z) :vehicle(x, y), seatnum(z) {} 4 5 int motorcar::get_seatnum() 6 { 7 return seatnum; 8 }
1 #include "motorcycle.h" 2 3 motorcycle::motorcycle(int spe, int wei, int hei, int seat) :vehicle(spe, wei), bicycle(spe, wei, hei), motorcar(spe, wei, seat) {}
1 #include "vehicle.h" 2 #include <iostream> 3 4 using namespace std; 5 6 vehicle::vehicle(int x, int y) :maxspeed(x), weight(y) {} 7 8 void vehicle::run() 9 { 10 cout << "run" << endl; 11 } 12 13 void vehicle::stop() 14 { 15 cout << "stop" << endl; 16 } 17 18 int vehicle::get_maxspeed() 19 { 20 return maxspeed; 21 } 22 23 int vehicle::get_weight() 24 { 25 return weight; 26 }
1 // 2.cpp: 定义控制台应用程序的入口点。 2 // 3 4 #include <iostream> 5 #include "motorcycle.h" 6 7 using namespace std; 8 9 int main() 10 { 11 motorcycle x1(1, 2, 3, 4); 12 cout << "motorcycle:" << endl; 13 x1.run(); 14 x1.stop(); 15 cout << "maxspeed= " << x1.get_maxspeed() << " , weight= " << x1.get_weight() << " , height= " << x1.get_height() << " , seatnum= " << x1.get_seatnum() << endl; 16 return 0; 17 }
实验3:
1 #pragma once 2 #include <iostream> 3 4 using namespace std; 5 6 class Fraction 7 { 8 public: 9 Fraction(int a = 0, int b = 1); 10 Fraction(Fraction &a); 11 void check(); 12 void compare(Fraction &f); 13 int get_Top() const; 14 int get_Bottem() const; 15 ~Fraction(); 16 friend ostream & operator<<(ostream &out, const Fraction &n); 17 friend Fraction operator+(const Fraction &a, const Fraction &b); 18 friend Fraction operator-(const Fraction &a, const Fraction &b); 19 friend Fraction operator*(const Fraction &a, const Fraction &b); 20 friend Fraction operator/(const Fraction &a, const Fraction &b); 21 private: 22 int top, bottem; 23 };
1 #pragma once 2 #include <iostream> 3 #include "Fraction.h" 4 5 using namespace std; 6 7 class iFraction :public Fraction 8 { 9 public: 10 iFraction(int left_ = 0, int top_ = 0, int bottem_ = 0); 11 ~iFraction(); 12 private: 13 };
1 #include "Fraction.h" 2 #include <iostream> 3 #include <algorithm> 4 5 using namespace std; 6 7 //------------------------------------------------------------------------------------------------- 8 // 最大公约数 9 //------------------------------------------------------------------------------------------------- 10 int gcd(int a, int b) 11 { 12 for (int i = min(a, b);; --i) 13 { 14 if (a%i == 0 && b%i == 0) 15 return i; 16 } 17 } 18 19 Fraction::Fraction(int a, int b) :top(a), bottem(b) 20 { 21 check(); 22 } 23 24 Fraction::Fraction(Fraction & a) : top(a.top), bottem(a.bottem) 25 { 26 check(); 27 } 28 29 void Fraction::check() 30 { 31 //--------------------------------------------------------------------------------------------- 32 // 检查分母正负 33 //--------------------------------------------------------------------------------------------- 34 if (bottem < 0) 35 { 36 bottem *= -1; 37 top *= -1; 38 } 39 40 //--------------------------------------------------------------------------------------------- 41 // 约分 42 //--------------------------------------------------------------------------------------------- 43 if (top != 0) 44 { 45 int k = gcd(static_cast<int>(fabs(top)), static_cast<int>(fabs(bottem))); 46 top /= k; 47 bottem /= k; 48 } 49 else 50 bottem = 1; 51 } 52 53 void Fraction::compare(Fraction & f) 54 { 55 cout << " " << top << " / " << bottem; 56 if ((float(top)) / (float(bottem)) > (float(f.top)) / (float(f.bottem))) 57 cout << " > "; 58 else if ((float(top)) / (float(bottem)) < (float(f.top)) / (float(f.bottem))) 59 cout << " < "; 60 else 61 cout << " == "; 62 cout << " " << f.top << " / " << f.bottem << endl; 63 } 64 65 int Fraction::get_Top() const 66 { 67 return top; 68 } 69 70 int Fraction::get_Bottem() const 71 { 72 return bottem; 73 } 74 75 Fraction::~Fraction() 76 { 77 } 78 79 //------------------------------------------------------------------------------------------------- 80 // 输出 81 //------------------------------------------------------------------------------------------------- 82 ostream & operator<<(ostream & out, const Fraction & n) 83 { 84 if (n.get_Bottem() == 1) 85 out << n.top; 86 else if (n.top / n.bottem == 0) 87 out << n.top%n.bottem << " / " << n.bottem << " = " << static_cast<float>(n.top) / static_cast<float>(n.bottem); 88 else 89 out << n.top / n.bottem << " + " << n.top%n.bottem << " / " << n.bottem << " = " << static_cast<float>(n.top) / static_cast<float>(n.bottem); //规范化 90 return out; 91 } 92 93 //------------------------------------------------------------------------------------------------- 94 // 加法 95 //------------------------------------------------------------------------------------------------- 96 Fraction operator+(const Fraction & a, const Fraction & b) 97 { 98 Fraction x(a.bottem*b.top + b.bottem*a.top, a.bottem*b.bottem); 99 return x; 100 } 101 102 //------------------------------------------------------------------------------------------------- 103 // 减法 104 //------------------------------------------------------------------------------------------------- 105 Fraction operator-(const Fraction & a, const Fraction & b) 106 { 107 Fraction x(b.bottem*a.top - a.bottem*b.top, a.bottem*b.bottem); 108 return x; 109 } 110 111 //------------------------------------------------------------------------------------------------- 112 // 乘法 113 //------------------------------------------------------------------------------------------------- 114 Fraction operator*(const Fraction & a, const Fraction & b) 115 { 116 Fraction x(a.top*b.top, a.bottem*b.bottem); 117 return x; 118 } 119 120 //------------------------------------------------------------------------------------------------- 121 // 除法 122 //------------------------------------------------------------------------------------------------- 123 Fraction operator/(const Fraction & a, const Fraction & b) 124 { 125 Fraction x(a.top*b.bottem, a.bottem*b.top); 126 return x; 127 }
1 #include "iFraction.h" 2 3 iFraction::iFraction(int left_, int top_, int bottem_) : Fraction(top_ + bottem_ * left_, bottem_) //化成分数 4 { 5 check(); 6 } 7 8 iFraction::~iFraction() 9 { 10 }
1 // 3.cpp: 定义控制台应用程序的入口点。 2 // 3 4 #include <iostream> 5 #include "iFraction.h" 6 7 using namespace std; 8 9 int main() 10 { 11 iFraction a(1, 5, 4); 12 Fraction b(10, -5); 13 cout << " a = " << a << endl; 14 cout << " b = " << b << endl; 15 cout << " a + b = " << a + b << endl; 16 cout << " a - b = " << a - b << endl; 17 cout << " a * b = " << a * b << endl; 18 cout << " a / b = " << a / b << endl; 19 a.compare(b); 20 return 0; 21 }
现在学会了重载简单的运算符,使之前的程序有了极大的优化空间。但对我来说,最关键的问题是如何优化算法……(不是局限于这些题目的)