实验任务4
pets.hpp:
#ifndef PETS_HPP #define PETS_HPP using namespace std; #include<iostream> #include<string> class MachinePets { public: MachinePets() {}; MachinePets(const string s) { nickname = s; } string get_nickname() { return nickname; } virtual string talk() { return ""; } void machinepets(const string s) { nickname = s; } private: string nickname; }; 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_HPP
task4.hpp:
#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); }
运行结果

实验任务3
battery.hpp:
#ifndef BATTERY_HPP #define BATTERY_HPP using namespace std; class Battery { public: Battery(double capa = 70) :capacity(capa) {}; double get_capacity() { return capacity; } private: double capacity; }; #endif BATTERY_HPP
car.hpp:
#ifndef CAR_HPP #define CAR_HPP #include<string> #include<iostream> using namespace std; class Car { public: Car() {} Car(string ma, string mo, int ye, double od = 0) :maker(ma), model(mo), year(ye), odometers(od) {} void info() { cout << "maker: " << maker << endl; cout << "model: " << model << endl; cout << "year: " << year << endl; cout << "odometers: " << odometers << endl; } void update_odometers(double a) { if (a < odometers) { cout << "wrong update" << endl; } else { odometers = a; } } string get_maker() { return maker; } string get_model() { return model; } int get_year() { return year; } double get_odometers() { return odometers; } void car(string ma, string mo, int ye) { maker = ma; model = mo; year = ye; } private: string maker; string model; int year; double odometers; }; #endif CAR_HPP
electriccar.hpp:
#include"car.hpp" #include"battery.hpp" #ifndef ELECTRICCAAR_HPP #define ELECTRICCAAR_HPP #include<iostream> using namespace std; class ElectricCar :public Car { public: ElectricCar(string ma, string mo, int ye) { car(ma, mo, ye); } void info() { cout << "maker: " << get_maker() << endl; cout << "model: " << get_model() << endl; cout << "year: " << get_year() << endl; cout << "odometers: " << get_odometers() << endl; cout << "capacity: " << battery.get_capacity() << "-kWh" << endl; } private: Battery battery; }; #endif ELECTRICCAAR_HPP
task3.hpp:
#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(); }
运行结果:

实验任务2
源码:
#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 arectangle\n"; } }; // definition of Circle, derived from Graph class Circle : public Graph { public: void draw() { std::cout << "Circle::draw(): programs of draw acircle\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); }


同名覆盖原则:派生类和继承类函数完全相同时,通过派生类对象只访问派生类中的函数。
二元作用域分辨符:可以指定想要访问某个类的函数。
类型兼容原则:派生类对象可以作为基类的对象使用。
浙公网安备 33010602011771号