实验4 继承
实验任务2
<task2.cpp>
#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 are ctangle\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.cpp>
#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 are ctangle\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); }
实验任务3
<Battery.hpp>
#ifndef BATTERY_HPP #define BATTERY_HPP #include<iostream> #include<string> #include<iomanip> using namespace std; class Battery { private: double capacity; public: Battery(double a = 70) :capacity(a) {}; double get_capacity() { return capacity; } }; #endif
<Car.hpp>
#ifndef CAR_HPP #define CAR_HPP #include"Battery.hpp" class Car { private: string maker; string model; int year; int odometers; public: Car(string a, string b, int c) :maker(a), model(b), year(c), odometers(0) {} void info() { cout << "maker: " << maker << endl << "model: " << model << endl << "year: " << year << endl << "odometers: " << odometers << endl; } void update_odometers(int a) { if (a < odometers) cout << "fault\n"; else odometers = a; } }; #endif
<EletricCar.hpp>
#ifndef ELECTRICCAR_HPP #define ELECTRICCAR_HPP #include"Car.hpp" class ElectricCar :public Car { private: Battery battery; public: ElectricCar(string a, string b, int c, double d = 70) :Car(a, b, c), battery(d) {} void info() { Car::info(); cout << "capacity: " << battery.get_capacity() << "-kWh" << endl; } }; #endif
<task3.cpp>
#include"ElectricCar.hpp" int main() { // test class of Car Car oldcar("jojo", "p1", 2010); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(2500); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("giao", "p2", 2020); newcar.update_odometers(25000); cout << "\n--------newcar's info--------\n"; newcar.info(); }
odometers输入-1后
实验任务4
<pets.hpp>
#ifndef PETS_HPP #define PETS_HPP # include<iostream> # include<string> using namespace std; class MachinePets { public: MachinePets(const string s) :nickname(s) {} string get_nickname ()const { return nickname; } virtual string talk() { return "~~~"; } private: string nickname; }; class PetCats :public MachinePets { public: PetCats(const string s) :MachinePets(s) {} string talk() { return "wu~"; } }; class PetDogs :public MachinePets { public: PetDogs(const string s) :MachinePets(s) {} string talk() { return "wang~"; } }; #endif
<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); }