实验结论
实验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() { 14 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 };
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 };
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 };
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 }
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 }
运行效果图如下:

实验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 }
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 }
运行效果图如下:

实验总结
实验3心得
关于ElectricCar.cpp的组织形式
需要把所有的类的具体实现都发到这里,不然会报重定义的错误
其他
1.可以在子类中调用父类的方法进行“坑老”,减少子类书写的代码量,如在info中调用父类的info
2.注意构造器的传入参数的个数,我一开始写错了
实验4心得
1.一个抽象函数的使用,简简单单
2.类名应该使用单数,而不是例如“petcats"这种的复数
浙公网安备 33010602011771号