实验任务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
battery.hpp
#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
car.hpp
#ifndef CAR_HPP #define CAR_HPP #include<iostream> #include<string> using namespace std; class Car{ public: Car(string m,string mo,int y,int od=0):maker(m),model(mo),year(y),odometres(od){} void info(); void update_odometers(int odo); private: string maker; string model; int year; int odometres; }; void Car::info(){ cout<<"maker: "<<maker<<endl; cout<<"model: "<<model<<endl; cout<<"year: "<<year<<endl; cout<<"odometres: "<<odometres<<endl; } void Car::update_odometers(int odo){ if(odo<odometres) { cout<<"Error updating value\n"; } else { odometres=odo; } } #endif
electricCar.hpp
#ifndef ELECTRICCAR_HPP #define ELECTRICCAR_HPP #include "car.hpp" #include "battery.hpp" #include<iostream> #include<string> using namespace std; class ElectricCar:public Car { public: ElectricCar(string m,string mo,int y,int b=70):Car(m,mo,y),battery(b){} void info(); private: Battery battery; }; void ElectricCar::info(){ Car::info(); cout<<"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("Hongqi", "h6", 2020); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(25009); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("Rongwei", "i5", 2021); newcar.update_odometers(1000); cout << "\n--------newcar's info--------\n"; newcar.info(); }
实验任务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){} virtual string talk(){} string get_nickname(){ return nickname; } 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
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); }