实验四 继承
作者:互联网
- 实验任务二
- task2.cpp
- task2.cpp(虚函数)
-
总结:
1.同名覆盖原则
派生类与基类中有相同成员时:
若未强行指名,则通过派生类对象使用的是派生类的同名成员;2. 二元作用域分辨符
如果成员函数中定义了和类域中的变量同名的变量,则类域中的变量被块作用域中的变量隐藏。这时可以在变量名前面加类名和作用域运算符(::)访问这种隐藏变量。
3.类型兼容原则
在需要基类对象的任何地方,都可以使用公有派生类的对象来替代,编译器会隐式地执行派生类到基类的转换。
存在三种转换:
->>派生类的对象可以隐含转换为基类对象。
->>派生类的对象可以初始化基类的引用。
->>派生类的指针可以隐含转换为基类的指针。 - 实验任务三
-
1 #ifndef CAR_HPP 2 #define CAR_HPP 3 #include <iostream> 4 #include <string> 5 6 using namespace std; 7 8 class Car 9 { 10 public: 11 Car(string maker0,string model0,int year0,int odometers0=0):maker(maker0),model(model0),year(year0),odometers(odometers0){ }; 12 void info()const; 13 void update_odometers(int new_odometers); 14 private: 15 string maker,model; 16 int year,odometers; 17 }; 18 void Car::info()const 19 { 20 cout << "maker:\t\t" << maker <<endl; 21 cout << "model:\t\t" << model <<endl; 22 cout << "year:\t\t" << year <<endl; 23 cout << "odometers:\t" << odometers <<endl; 24 } 25 void Car::update_odometers(int new_odometers) 26 { 27 if(new_odometers<odometers) 28 cout << "数据错误,请重新输入" << endl; 29 odometers=new_odometers; 30 } 31 #endif // CAR_HPP
Car.hpp -
1 #ifndef BATTERY_HPP 2 #define BATTERY_HPP 3 #include<iostream> 4 5 using namespace std; 6 7 class Battery 8 { 9 public: 10 Battery(int capacity0 = 70):capacity{capacity0} {}; 11 int get_capacity() {return capacity;} 12 private: 13 int capacity; 14 }; 15 16 #endif // BATTERY_HPP
Battery.hpp -
1 #ifndef ElECTRIC_CAR_HPP 2 #define ElECTRIC_CAR_HPP 3 #include "Car.hpp" 4 #include "Battery.hpp" 5 #include <iostream> 6 #include <string> 7 8 using namespace std; 9 10 class ElectricCar:public Car 11 { 12 public: 13 ElectricCar(string maker0,string model0,int year0,int odometers0=0,int capacity0=70):Car(maker0,model0,year0,odometers0),battery(capacity0) {}; 14 void info() ; 15 private: 16 Battery battery; 17 }; 18 void ElectricCar:: info() 19 { 20 Car::info(); 21 cout << "capacity:\t" << battery.get_capacity() << endl; 22 } 23 #endif // ElECTRIC_CAR_HPP
EletricCar.hpp -
1 #include <iostream> 2 #include "electricCar.hpp" 3 int main() 4 { 5 using namespace std; 6 // test class of Car 7 Car oldcar("Audi", "a4", 2016); 8 cout << "--------oldcar's info--------" << endl; 9 oldcar.update_odometers(25000); 10 oldcar.info(); 11 cout << endl; // test class of ElectricCar 12 ElectricCar newcar("Tesla", "model s", 2016); 13 newcar.update_odometers(2500); 14 cout << "\n--------newcar's info--------\n"; 15 newcar.info(); 16 }
main.hpp - 实验任务四
-
1 #ifndef PETS_HPP 2 #define PETS_HPP 3 #include <iostream> 4 #include <string> 5 using namespace std; 6 7 class MachinePets 8 { 9 public: 10 MachinePets(const string s):nickname(s){ } 11 virtual string talk() {return "a~";} 12 string get_nickname() const {return nickname;} 13 private: 14 string nickname; 15 }; 16 17 class PetCats:public MachinePets 18 { 19 public: 20 PetCats(const string s):MachinePets(s) { } 21 string talk() {return "miao wu~";} 22 }; 23 24 class PetDogs:public MachinePets 25 { 26 public: 27 PetDogs(const string s):MachinePets(s) { } 28 string talk() {return "wang wang~";} 29 }; 30 #endif // PETS_HPP
Pets.hpp -
1 #include <iostream> 2 #include "pets.hpp" 3 4 void play(MachinePets *ptr) 5 { 6 std::cout << ptr->get_nickname() << " says " << ptr->talk() << std::endl; 7 } 8 int main() 9 { 10 PetCats cat("miku"); 11 PetDogs dog("da huang"); 12 play(&cat); 13 play(&dog); 14 }
main.hpp -
标签:HPP,string,继承,Car,hpp,int,实验,include 来源: https://www.cnblogs.com/hf200930/p/15616585.html