一、实验任务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); }
同名覆盖原则:派生类和继承类函数完全相同时,通过派生类对象只访问派生类中的函数。
二元作用域分辨符:可以指定想要访问某个类的函数。
类型兼容原则:派生类对象可以作为基类的对象使用。
实验任务3
#ifndef BATTERY_HPP #define BATTERY_HPP #include<iostream> using namespace std; class Battery{ public: Battery(int c=70):capacity(c){} int get_capacity(){ return capacity; } private: int capacity; }; #endif
#ifndef CAR_HPP #define CAR_HPP #include<iostream> #include<string> using namespace std; class Car{ public: Car(string a,string m,int y,int o=0):maker(a),model(m),year(y),odometers(o){} void info(); void update_odometers(int od); private: string maker,model; int year,odometers; }; void Car::info(){ cout<<"maker: "<<maker<<endl; cout<<"model: "<<model<<endl; cout<<"year: "<<year<<endl; cout<<"odometers: "<<odometers<<endl; } void Car::update_odometers(int od) { if(od<odometers) { cout<<"Error updating value\n"; } else { odometers=od; } } #endif
#ifndef ELECTRICCAR_HPP #define ELECTRICCAR_HPP #include "car.hpp" #include "battery.hpp" #include<iostream> using namespace std; class ElectricCar:public Car{ public: ElectricCar(string a,string m,int y,int o=0,int b=70):Car(a,m,y,o),battery(b){} void info(); private: Battery battery; }; void ElectricCar::info(){ Car::info(); cout<<"capacity: "<<battery.get_capacity()<<"-kWh"<<endl; } #endif
#include <iostream> #include "electricCar.hpp" int main() { using namespace std; // test class of Car Car oldcar("benzy", "s1", 2026); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(34000); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("paul", "g6", 2030); newcar.update_odometers(2666); cout << "\n--------newcar's info--------\n"; newcar.info(); }
实验任务4
#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(){ return nickname; } virtual string talk(){} private: string nickname; }; class PetCats:public MachinePets{ public: PetCats(const string s):MachinePets(s){} string talk(){ return "miao miao~"; } }; class PetDogs:public MachinePets{ public: PetDogs(const string s):MachinePets(s){} string talk(){ return "wang wang~"; } }; #endif
#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); }