实验四
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 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); std::cout << "\n"; fun(&r1); std::cout << "\n"; fun(&c1); }
同名覆盖原则:如果派生类声明了一个与基类成员函数同名的函数成员,那么直接使用成员名只能访问派生类的对应成员。
二元作用域分辨符:使用作用域分辨符和基类名来访问被隐藏的基类成员函数。
类型兼容原则:派生类对象可以作为基类对象使用,但是只能使用从基类继承的成员。
调整后:
#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); std::cout << "\n"; fun(&r1); std::cout << "\n"; fun(&c1); }
运行截图:
使用虚函数之前,调用的是基类函数成员;定义虚函数后,调用的是派生类函数成员。
task3.hpp
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include<iostream> #include<string> #include<iomanip> using namespace std; class Car { public: Car(string s1, string s2, int a); void info(); void update_odometers(int d); private: string maker, model; int year, odometers; }; Car::Car(string s1, string s2, int a) { maker = s1; model = s2; year = a; odometers = 49999; } void Car::info() { cout << left << setw(15) << setfill(' ') << "maker:" << maker << endl; cout << left << setw(15) << setfill(' ') << "model:" << model << endl; cout << left << setw(15) << setfill(' ') << "year:" << year << endl; cout << left << setw(15) << setfill(' ') << "odometers:" << odometers << endl; } void Car::update_odometers(int d) { if (d < odometers) cout << "error"<<endl; else odometers = d; } class Battery { public: Battery(int n = 70); int get_capacity(); private: int capacity; }; Battery::Battery(int n) { capacity = n; } int Battery::get_capacity() { return capacity; } class ElectricCar :public Car { public: ElectricCar(string s1, string s2, int b, int c=70) :Car(s1, s2, b), battery(c) {} void info(); private: Battery battery; }; void ElectricCar::info() { Car::info(); cout << left << setw(15) << setfill(' ') << "capacity:" << battery.get_capacity()<<"-kwh"<< endl; } #endif
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", 2016); newcar.update_odometers(2500); cout << "\n--------newcar's info--------\n"; newcar.info(); }
运行截图:
task4.hpp
#ifndef PETS_H #define PETS_H #include<iostream> #include<string> using namespace std; class MachinePets { public: MachinePets(const string s):nickname(s){} string get_nickname() { return nickname; } virtual string talk() { return " "; } private: string nickname; }; class PetsCats:public MachinePets { public: PetsCats(const string s):MachinePets(s) {} string talk(); private: string nickname; }; string PetsCats::talk() { return " miao wu~"; } class PetDogs :public MachinePets { public: PetDogs(const string s) :MachinePets(s){} string talk(); }; string PetDogs::talk() { return " wang wang~" ; } #endif
task4.cpp
#include<iostream> #include<string> #include"pets.hpp" void play(MachinePets* ptr) { std::cout << ptr->get_nickname() << " says " << ptr->talk() << std::endl; } int main() { PetsCats cat("miku"); PetDogs dog("da huang"); play(&cat); play(&dog); }
运行截图: