12歲孩子入QQ群想免費領遊戲面板 結果被騙7.7萬元
阿新 • • 發佈:2021-11-19
實驗結論
實驗2:驗證抽象函式的使用
main.cpp
1 #include<iostream> 2 #include<typeinfo> 3 4 class Graph{ 5 public: 6 virtual void draw() { 7 std::cout << "Graph::drwa() : just as an interface\n"; 8 } 9 }; 10 11 class Rectangle: public Graph { 12 public: 13 void draw() {View Code14 std::cout << "Rectangle::drwa(): programs of draw a rectangle\n"; 15 } 16 }; 17 18 class Circle: public Graph { 19 public: 20 void draw() { 21 std::cout << "Circle::draw((): programs of draw a circle\n"; 22 } 23 }; 24 25 void fun(Graph *ptr) { 26 std::cout << "pointer type: " << typeid(ptr).name() << std::endl; 27 std::cout << "PTTI type: " << typeid(*ptr).name() << std::endl; 28 ptr->draw(); 29 } 30 31 int main() { 32 Graph g1; 33 Rectangle r1; 34 Circle c1; 35 36 g1.draw(); 37 r1.draw(); 38 c1.draw();39 40 std::cout << "\n"; 41 42 fun(&g1); 43 fun(&r1); 44 fun(&c1); 45 }
將draw設定為virtual函式之前:
將draw設定為virtual函式之後:
我的結論:
1. 在向上轉型(將子類的變數轉換為父類時),會優先使用父類的對應的函式,除非將該函式virtual化
2. 如果想通過子類呼叫父類的函式(與子類的函式申明一致,已經被過載),需要用FartherClass::function()的函式來呼叫父類對應的函式
實驗3:模擬簡單的車輛資訊管理
Battery.hpp
1 #pragma once 2 #include<iostream> 3 class Battery { 4 public: 5 Battery(int _capacity = 70); 6 int get_capacity(); 7 private: 8 int capacity; 9 10 };View Code
Car.hpp
1 #pragma once 2 #include<iostream> 3 4 using namespace std; 5 6 class Car 7 { 8 public: 9 Car(string maker, string model, int year); 10 void info(); 11 void update_odometers(int curr_odometers); 12 13 private: 14 string maker; 15 string model; 16 int year; 17 int odometers; 18 };View Code
ElectricCar.hpp
1 #pragma once 2 #include "Car.hpp" 3 #include "Battery.hpp" 4 class ElectricCar : public Car{ 5 public: 6 ElectricCar(string maker, string model, int year, int capacity = 70); 7 void info(); 8 9 private: 10 Battery battery; 11 };View Code
ElectricCar.cpp
1 #include "ElectricCar.hpp" 2 3 Battery::Battery(int _capacity) : capacity(_capacity) { 4 5 } 6 7 int Battery::get_capacity() { 8 return capacity; 9 } 10 11 Car::Car(string _maker, string _model, int _year) : maker(_maker), model(_model), year(_year), odometers(0) { 12 13 } 14 15 void Car::info() { 16 cout << 17 "製造商:\t" << maker << "\n" 18 "型號:\t\t" << model << "\n" 19 "生產年份:\t" << year << "\n" 20 "當前行車裡程數:" << odometers << endl; 21 } 22 23 void Car::update_odometers(int curr_odometers) { 24 if (curr_odometers < odometers) { 25 cout << "輸入里程數小於原始里程數,請查證後重新輸入" << endl; 26 } 27 else { 28 odometers = curr_odometers; 29 } 30 } 31 32 ElectricCar::ElectricCar(string _maker, string _model, int _year, int _capacity) : Car(_maker, _model, _year), battery(_capacity) { 33 34 } 35 36 void ElectricCar::info() { 37 Car::info(); 38 cout << "電池容量:\t" << battery.get_capacity() << "-kwh" << endl; 39 }View Code
task3.cpp
1 #include"ElectricCar.hpp" 2 3 int main() { 4 Car oldcar("Audi", "a7", 2021); 5 cout << "------oldcar's info------" << endl; 6 oldcar.update_odometers(30000); 7 oldcar.info(); 8 cout << endl; 9 10 ElectricCar newCar("Tesla", "mldel S", 2020); 11 newCar.update_odometers(3000); 12 cout << "\n------newCar's info------\n"; 13 newCar.info(); 14 }View Code
執行效果圖如下:
實驗4:模擬簡單的機器寵物
Pet.hpp
1 #pragma once 2 #include<iostream> 3 using namespace std; 4 5 class MachinePet { 6 public: 7 MachinePet(const string s); 8 virtual string talk(); 9 string get_nickname(); 10 private: 11 string nickname; 12 }; 13 14 class PetCat : public MachinePet{ 15 public: 16 PetCat(const string s); 17 string talk(); 18 }; 19 20 class PetDog : public MachinePet{ 21 public: 22 PetDog(const string s); 23 string talk(); 24 }; 25 26 MachinePet::MachinePet(const string s) : nickname(s) { 27 28 } 29 30 string MachinePet::get_nickname() { 31 return nickname; 32 } 33 34 string MachinePet::talk() { 35 return "動物叫"; 36 } 37 38 PetCat::PetCat(const string s) : MachinePet(s) { 39 40 } 41 42 string PetCat::talk() { 43 return "喵喵喵"; 44 } 45 46 PetDog::PetDog(const string s) : MachinePet(s) { 47 48 } 49 50 string PetDog::talk() { 51 return "汪汪汪"; 52 }View Code
task4.cpp
1 #include <iostream> 2 #include "Pet.hpp" 3 4 void play(MachinePet* ptr) { 5 cout << ptr->get_nickname() << "說" << ptr->talk() << endl; 6 } 7 8 int main() 9 { 10 PetCat cat("小花"); 11 PetDog dog("大黃"); 12 13 play(&cat); 14 play(&dog); 15 }View Code
執行效果圖如下:
實驗總結
實驗3心得
關於ElectricCar.cpp的組織形式
需要把所有的類的具體實現都發到這裡,不然會報重定義的錯誤
其他
1.可以在子類中呼叫父類的方法進行“坑老”,減少子類書寫的程式碼量,如在info中呼叫父類的info
2.注意構造器的傳入引數的個數,我一開始寫錯了
實驗4心得
1.一個抽象函式的使用,簡簡單單
2.類名應該使用單數,而不是例如“petcats"這種的複數