Browse Source

修改部分代码

Pchen. 5 months ago
parent
commit
e1d1d0047f

+ 9 - 6
code/定义一个人民币的类(重载+、-、+=、++、输入输出流).cpp

@@ -2,7 +2,6 @@
 #include<iomanip>
 #include<iomanip>
 using namespace std;
 using namespace std;
 
 
-//人民币类的定义与实现
 class RMB {
 class RMB {
 private:
 private:
     int yuan, jiao, fen;
     int yuan, jiao, fen;
@@ -16,11 +15,14 @@ public:
         return in;
         return in;
     }
     }
 
 
-    friend ostream& operator<<(ostream& out, RMB& r) {
-    	int sum = 100 * yuan + 10 * jiao + fen;
-        out << "¥ " << fixed << setprecision(2) << sum << endl;
-        return out;
-    }
+	friend ostream& operator<<(ostream& out, const RMB& r) {
+	    int totalFen = r.yuan * 100 + r.jiao * 10 + r.fen;
+	    int yuan = totalFen / 100;
+	    int jiao = (totalFen / 10) % 10;
+	    int fen = totalFen % 10;
+	    out << "¥ " << yuan << "." << jiao << fen;
+	    return out;
+	}
 
 
     RMB operator+(const RMB& r) const {
     RMB operator+(const RMB& r) const {
         RMB result;
         RMB result;
@@ -70,6 +72,7 @@ public:
     }
     }
 };
 };
 
 
+
 int main()
 int main()
 {
 {
     RMB r1(14,  5  ,7),r2,r3;    //定义三个人民币的对象,  其中两个调用缺省的构造,  构造为0
     RMB r1(14,  5  ,7),r2,r3;    //定义三个人民币的对象,  其中两个调用缺省的构造,  构造为0

+ 12 - 12
code/车基类派生出自行车类和汽车类再从自行车类和汽车类派生摩托车(多态).cpp

@@ -3,45 +3,45 @@ using namespace std;
 
 
 class Vehicle {
 class Vehicle {
 public:
 public:
-    void Run() {
-        cout << "Vehicle is running" << endl;
+    virtual void Run() {
+        cout << "vehicle run!" << endl;
     }
     }
 
 
-    void Stop() {
-        cout << "Vehicle stops" << endl;
+    virtual void Stop() {
+        cout << "vehicle stop!" << endl;
     }
     }
 };
 };
 
 
-class Bicycle : public Vehicle {
+class Bicycle : virtual public Vehicle {
 public:
 public:
     void Run() {
     void Run() {
-        cout << "Bicycle is running" << endl;
+        cout << "bicycle run!" << endl;
     }
     }
 
 
     void Stop() {
     void Stop() {
-        cout << "Bicycle stops" << endl;
+        cout << "bicycle stop!" << endl;
     }
     }
 };
 };
 
 
-class Motorcar : public Vehicle {
+class Motorcar : virtual public Vehicle {
 public:
 public:
     void Run() {
     void Run() {
-        cout << "Motorcar is running" << endl;
+        cout << "motocar run!" << endl;
     }
     }
 
 
     void Stop() {
     void Stop() {
-        cout << "Motorcar stops" << endl;
+        cout << "motocar stop!" << endl;
     }
     }
 };
 };
 
 
 class Motorcycle : public Bicycle, public Motorcar {
 class Motorcycle : public Bicycle, public Motorcar {
 public:
 public:
     void Run() {
     void Run() {
-        cout << "Motorcycle is running" << endl;
+        cout << "motocycle run!" << endl;
     }
     }
 
 
     void Stop() {
     void Stop() {
-        cout << "Motorcycle stops" << endl;
+        cout << "motocycle stop!" << endl;
     }
     }
 };
 };