Experiment Task Two
#Program Source
1 #include <iostream> 2 #include <typeinfo> 3 4 // definitation of Graph 5 class Graph 6 { 7 public: 8 virtual void draw() { std::cout << "Graph::draw() : just as an interface\n"; } 9 }; 10 11 12 // definition of Rectangle, derived from Graph 13 class Rectangle : public Graph 14 { 15 public: 16 void draw() { std::cout << "Rectangle::draw(): programs of draw a rectangle\n"; } 17 }; 18 19 20 // definition of Circle, derived from Graph 21 class Circle : public Graph 22 { 23 public: 24 void draw() { std::cout << "Circle::draw(): programs of draw a circle\n"; } 25 }; 26 27 28 // definitaion of fun(): as a call interface 29 void fun(Graph *ptr) 30 { 31 std::cout << "pointer type: " << typeid(ptr).name() << "\n"; 32 std::cout << "RTTI type: " << typeid(*ptr).name() << "\n"; 33 ptr -> draw(); 34 } 35 36 // test 37 int main() 38 { 39 Graph g1; 40 Rectangle r1; 41 Circle c1; 42 43 // call by object name 44 g1.draw(); 45 r1.draw(); 46 c1.draw(); 47 48 std::cout << "\n"; 49 50 // call by object name, and using the scope resolution operator:: 51 r1.Graph::draw(); 52 c1.Graph::draw(); 53 54 std::cout << "\n"; 55 56 // call by pointer to Base class 57 fun(&g1); 58 fun(&r1); 59 fun(&c1); 60 }
# Summary:
1. The principle of coverage by the same name:
When a derived class has the same members as a base class: if a name is not enforced, the same members of the derived class are used through the derived class object;
If you want to access the same-name member overridden by the base class through the object of the derived class, you need to add the object name.Base class name:: Members with the same name are qualified.
2. Binary Scope Discriminator:
Scope Symbol:The first is generally the class name, then the member name of the class. C++ distinguishes by scope to avoid having the same name for different classes.
3. Type Compatibility Principles:
The principle of type compatibility refers to the substitution of objects of public derived classes wherever a base class object is required. Through public inheritance, the derived class obtains all members of the base class except the constructor and destructor. In this way, public derived classes have all the functions of the base class. Anything that can be solved by the class. Public derived classes can be resolved. Substitutions referred to in the principle of type compatibility include the following:
1) Subclass objects can be used as parent objects
2) Subclass objects can be directly assigned to parent objects
3) Subclass objects can directly initialize parent objects
4) Parent pointer can point directly to child class object
5) Parent references can refer directly to subclass objects
4. virtual virtual function action:
Both base class and subclass have the same function. Define a base class pointer to the subclass object, call the function with the pointer, call the base class function if the function is not virtual, otherwise call the subclass function. If the pointer is the same as the pointer to the object, each calls its own function. With a common base class as the virtual base class, there is only one copy of the same data member inherited from different paths in memory and only one mapping for the same function name. This not only solves ambiguity, but also saves memory and avoids data inconsistency.
Experiment Task Three
battery.hpp
1 #ifndef BATTERY_HPP 2 #define BATTERY_HPP 3 #include<iostream> 4 #include<string> 5 using namespace std; 6 7 class battery{ 8 public: 9 battery(int capacity = 70): m_capacity{capacity} {} 10 11 int get_capacity() 12 { 13 return m_capacity; 14 } 15 private: 16 int m_capacity; 17 }; 18 19 #endif
car.hpp
1 #ifndef CAR_HPP 2 #define CAR_HPP 3 #include<iostream> 4 #include<string> 5 using namespace std; 6 7 class car{ 8 public: 9 car(string maker, string model, int year, int odometers = 0): m_maker{maker}, m_model{model}, m_year{year}, m_odometers{odometers} {} 10 void info() 11 { 12 cout << "maker: \t" << m_maker << endl; 13 cout << "model: \t" << m_model << endl; 14 cout << "year: \t" << m_year << endl; 15 cout << "odometers:\t" << m_odometers << endl; 16 } 17 18 void update_odometers(int n) 19 { 20 if(n < m_odometers) 21 { 22 cout << "Error updating value" << endl; 23 } 24 else 25 { 26 m_odometers = n; 27 } 28 } 29 30 private: 31 string m_maker; 32 string m_model; 33 int m_year; 34 int m_odometers; 35 }; 36 37 #endif
electricCar.hpp
1 #ifndef CAR_HPP 2 #define CAR_HPP 3 #include<iostream> 4 #include<string> 5 using namespace std; 6 7 class car{ 8 public: 9 car(string maker, string model, int year, int odometers = 0): m_maker{maker}, m_model{model}, m_year{year}, m_odometers{odometers} {} 10 void info() 11 { 12 cout << "maker: \t" << m_maker << endl; 13 cout << "model: \t" << m_model << endl; 14 cout << "year: \t" << m_year << endl; 15 cout << "odometers:\t" << m_odometers << endl; 16 } 17 18 void update_odometers(int n) 19 { 20 if(n < m_odometers) 21 { 22 cout << "Error updating value" << endl; 23 } 24 else 25 { 26 m_odometers = n; 27 } 28 } 29 30 private: 31 string m_maker; 32 string m_model; 33 int m_year; 34 int m_odometers; 35 }; 36 37 #endif
main_car.cpp
#include <iostream> #include "electricCar.hpp" int main() { using namespace std; // test class of Car car oldcar("Audi", "a4", 2016); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(25000); oldcar.info(); cout << endl; // test class of ElectricCar electricCar newcar("Tesla", "model s", 2020, 0, 150); newcar.update_odometers(5200); cout << "\n--------newcar's info--------\n"; newcar.info(); }
Experiment Task Four
pets.hpp
1 #ifndef MACHINEPETS_HPP 2 #define MACHINEPETS_HPP 3 #include<iostream> 4 #include<string> 5 6 using namespace std; 7 8 class MachinePets 9 { 10 public: 11 MachinePets(const string s): nickname{s} {} 12 13 string get_nickname() 14 { 15 return nickname; 16 } 17 18 virtual string talk() 19 { 20 21 } 22 private: 23 string nickname; 24 }; 25 26 class PetCats: public MachinePets 27 { 28 public: 29 PetCats(const string s) : MachinePets(s) {} 30 string talk() 31 { 32 return "miao wu~ miao wu~ miao wu~" ; 33 } 34 }; 35 36 class PetDogs: public MachinePets 37 { 38 public: 39 PetDogs(const string s) : MachinePets(s) {} 40 string talk() 41 { 42 return "wang wang~ wang wang~ wang wang~" ; 43 } 44 }; 45 #endif
main_pets.cpp
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 9 int main() 10 { 11 PetCats cat("miku"); 12 PetDogs dog("da huang"); 13 14 play(&cat); 15 play(&dog); 16 }