task2
程序源码
#include <iostream> #include <typeinfo> // definitation of Graph class Graph { public: void draw() { std::cout << "Graph::draw() : just as an interface\n"; } }; // definition of Rectangle, derived from Graph class Rectangle : public Graph { public: void draw() { std::cout << "Rectangle::draw(): programs of draw a rectangle\n"; } }; // definition of Circle, derived from Graph class Circle : public Graph { public: void draw() { std::cout << "Circle::draw(): programs of draw a circle\n"; } }; // definitaion of fun(): as a call interface void fun(Graph *ptr) { std::cout << "pointer type: " << typeid(ptr).name() << "\n"; std::cout << "RTTI type: " << typeid(*ptr).name() << "\n"; ptr -> draw(); } // test int main() { Graph g1; Rectangle r1; Circle c1; // call by object name g1.draw(); r1.draw(); c1.draw(); std::cout << "\n"; // call by object name, and using the scope resolution operator:: r1.Graph::draw(); c1.Graph::draw(); std::cout << "\n"; // call by pointer to Base class fun(&g1); fun(&r1); fun(&c1);
未修改前截图
修改后截图
总结:
同名覆盖原则:派生类与基类中有相同成员时 ,若未强行指名,则通过派生类对象使用的是派生类的同名成员;如果要通过派生类的对象访问基类被覆盖的同名成员,需要加 对象名.基类名::同名成员 。
作用域运算符(::)的作用:
1.存在具有相同名称的局部变量时,访问全局变量。
2.在类之外定义类相关函数。
3.访问类的静态变量。
4.在多重继承的情况下,如果两个基类中存在相同的变量名,可以使用作用域运算符来进行区分。
5.限定成员函数所属的类。
类型兼容原则:类型兼容原则是指在需要基类对象的任何地方,都可以使用共有派生类的对象来替代。
类型兼容规则中所指的替代包括以下的情况:
1.派生类的对象可以赋值给基类对象
2.派生类的对象可以初始化基类的引用。
3.派生类对象的地址可以赋值给基类的指针。
task3
Car.hpp
#ifndef Car_HPP #define Car_HPP #include<iostream> #include<string> using namespace std; class Car { private: string maker,model; int year,odometers; public: Car(string a,string b,int c,int d=0):maker(a),model(b),year(c),odometers(0) {}; void info(); void update_odometers(int x); }; void Car::info() { cout<<"maker:\t\t"<<maker<<endl; cout<<"model:\t\t"<<model<<endl; cout<<"year:\t\t"<<year<<endl; cout<<"odometers:\t"<<odometers<<endl; } void Car::update_odometers(int x) { if(x<odometers) cout<<"更新数值有误!"<<endl; else odometers=x; } #endif
Battery.hpp
#ifndef Battery_HPP #define Battery_HPP #include<iostream> using namespace std; class Battery { private: int capacity; public: Battery(int x=70):capacity(x) {}; int get_capacity() const {return capacity;} }; #endif
ElectricCar.hpp
#ifndef ElectricCar_Hpp #define ElectricCar_Hpp #include<iostream> #include<string> #include"Car.hpp" #include"Battery.hpp" using namespace std; class ElectricCar: public Car { private: Battery battery; public: ElectricCar(string a, string b, int c, int d=70): Car(a,b,c),battery(d) {}; void info(); ~ElectricCar(){}; }; void ElectricCar::info() { Car::info(); cout<<"capacity:\t"<<battery.get_capacity()<<"-kwh"<<endl; } #endif
task3.cpp
#include <iostream> #include"ElectricCar.hpp" int main() { using namespace std; // test class of Car Car oldcar("AAA", "a3", 2021); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(27000); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("SSS", "model s", 2021); newcar.update_odometers(2700); cout << "\n--------newcar's info--------\n"; newcar.info(); }
task4
pets.hpp
#ifndef pets_HPP #define pets_HPP #include<iostream> #include<string> using namespace std; class MachinePets { private: string nickname; public: MachinePets(const string s):nickname(s) {} virtual string talk(); string get_nickname() {return nickname;} }; string MachinePets::talk() { return "Hello";} class PetCats:public MachinePets { public: PetCats(const string s):MachinePets(s) {} string talk() {return "miao wu~";} }; class PetDogs:public MachinePets { public: PetDogs(const string s):MachinePets(s) {} string talk() {return "wang wang~";} }; #endif
pets.cpp
#include <iostream> #include "pets.hpp" void play(MachinePets *ptr) { std::cout<<ptr->get_nickname()<<" says "<<ptr->talk()<<std::endl; } int main() { PetCats cat("miku"); PetDogs dog("da huang"); play(&cat); play(&dog); }