实验四(继承)
task2.1:
#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); }
task2.2:
#include <iostream> #include <typeinfo> // definitation of Graph class Graph { public: virtual 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.定义类中声明的成员函数时可以用类名和二元作用域运算符(::),将每个成员函数绑定到声明成员函数与数据成员的类定义上。
类型兼容原则:在需要基类对象的任何地方,都可以使用公有派生类的对象来代替。通过公有继承,派生类得到了基类中除构造函数。析构函数之外的所有成员。这样,共有派生类实际就具备了基类的所有功能,凡是基类能解决的问题,公有派生类都可以解决。
electricCar.hpp
#include<iostream> #include<string> using namespace std; class Car { public: Car(string ma, string mo, int y, int o = 0) :maker(ma), model(mo), year(y), odometers(o) {} void info() { cout << "maker:\t\t\t" << maker << endl; cout << "model:\t\t\t" << model << endl; cout << "year:\t\t\t" << year << endl; cout << "odometers:\t\t" << odometers << endl; } void update_odometers(int n) { if (n >= odometers) odometers = n; else cout << "Wrong data" << endl; } private: string maker; string model; int year; int odometers; }; class Battery { public: Battery(int c = 70) :capacity(c) {} int get_capacity() { return capacity; } private: int capacity; }; class ElectricCar:public Car { public: ElectricCar(string ma, string mo, int y, Battery ba=(70), int o = 0) :Car(ma,mo,y,o),battery(ba) {} void info() { Car::info(); cout << "capacity:\t\t" << battery.get_capacity() <<"-kWh"<< endl; } private: Battery battery; };
task3.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", 2021); newcar.update_odometers(2500); cout << "\n--------newcar's info--------\n"; newcar.info(); }
pets.hpp
#pragma once #include<iostream> #include<string> using namespace std; class MachinePets { public: MachinePets(const string s) :nickname(s) {} virtual string talk() { return ""; } string get_nickname() { return nickname; } private: string nickname; }; class PetCats :public MachinePets { public: PetCats(const string s) :MachinePets(s) {} virtual string talk() { return "miao wu~"; } }; class PetDogs :public MachinePets { public: PetDogs(const string s) :MachinePets(s) {} virtual string talk() { return "wang wang~"; } };
task4.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); }