任务二 源代码

task2.cpp

#include<iostream>
#include<typeinfo>

class Graph
{
public:
    void draw() { std::cout << "Graph::draw() : just as an interface\n"; }
};

class Rectangle : public Graph
{
public:
    void draw() { std::cout << "Rectangle::draw(): programs of draw a rectangle\n"; }
};

class Circle : public Graph
{
public:
    void draw() { std::cout << "Circle::draw(): programs of draw a circle\n"; }
};

void fun(Graph* ptr)
{
    std::cout << "pointer type: " << typeid(ptr).name() << "\n";
    std::cout << "RTTI type: " << typeid(*ptr).name() << "\n";
    ptr->draw();
}

int main()
{
    Graph g1;
    Rectangle r1;
    Circle c1;

    g1.draw();
    r1.draw();
    c1.draw();

    std::cout << "\n";

    r1.Graph::draw();
    c1.Graph::draw();

    std::cout << "\n";

    fun(&g1);
    fun(&r1);
    fun(&c1);

}

 

 修改后的结果:

 

 

归纳总结:

①同名覆盖原则

如果子类与父类函数同名时,父类中的同名函数会覆盖子类的同名函数。

②二元作用域分辨符

如果想要调用父类中的同名函数,可以使用类作用域运算符::。

③类型兼容原则

在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。

 

任务三 源代码

car.hpp

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

class Car
{
public:
    Car( string maker0, string model0, int year0, int odometers0);
    void info()const;
    void update_odometers(int new_odometers);
private:
    string maker;
    string model;
    int year, odometers;
};

Car::Car(string maker0 = "", string model0 = "", int year0 = 0, int odometers0 = 0) : maker{ maker0 }, model{ model0 }, year{ year0 }, odometers{ odometers0 } {}

void Car::info()const
{
    cout.width(15);
    cout << setiosflags(ios::left) << "maker:";
    cout << this->maker << endl;
    cout.width(15);
    cout << setiosflags(ios::left) << "model:";
    cout << this->model << endl;
    cout.width(15);
    cout << setiosflags(ios::left) << "year:";
    cout << this->year << endl;
    cout.width(15);
    cout << setiosflags(ios::left) << "odometers:";
    cout << this->odometers << endl;
}

void Car::update_odometers(int new_odometers)
{
    if (new_odometers < this->odometers)
        cout << "Update failed... ";
    else
        this->odometers = new_odometers;
}

battery.hpp

#include<iostream>
#include<iomanip>
using namespace std;

class Battery
{
public:
    Battery( int capacity0);
    int get_capacity()const;
private:
    int capacity;
};

Battery::Battery(int capacity0 = 70):capacity{capacity0}{}
int Battery::get_capacity() const
{
    return this->capacity;
}

electricCar.hpp

#include<iostream>
#include"car.hpp"
#include"battery.hpp"

class ElectricCar : public Car 
{
public:
    ElectricCar(string maker0 = "", string model0 = "", int year0 = 0, int odometers0 = 0, int capacity = 70);
    void info()const;
private:
    Battery battery;
};

ElectricCar::ElectricCar(string maker0, string model0, int year0, int odometers0, int capacity0) :Car(maker0, model0, year0, odometers0),battery(capacity0)
{
    ;
}

void ElectricCar::info()const
{
    Car::info();
    cout.width(15);
    cout << setiosflags(ios::left) << "capacity:";
    cout << battery.get_capacity() << "-kwh" << endl;
}

test.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();

    // test class of ElectricCar 
    ElectricCar newcar("Tesla", "model s", 2016);
    newcar.update_odometers(2500); 
    cout << "\n--------newcar's info--------\n"; 
    newcar.info();

    ElectricCar secondnewcar("Mercedes-Benz", "CL", 2020, 3000, 150);
    secondnewcar.update_odometers(3500);
    cout << "\n--------secondnewcar's info--------\n";
    secondnewcar.info();
}

运行结果:

 

 任务四 源代码

pets.hpp

#pragma once
#include<iostream>
#include<string>
using namespace std;

class MachinePets
{
public:
    MachinePets(const string s) :nickname{ s } {}
    virtual string talk() = 0;
    string get_nickname()
    {
        return this->nickname;
    }
private:
    string nickname;
};

class PetCats :public MachinePets
{
public:
    PetCats(const string s) :MachinePets(s) {}
    string talk() { string mao = "miao wu~"; return mao; }
};

class PetDogs :public MachinePets
{
public:
    PetDogs(const string s) :MachinePets(s) {}
    string talk() { string gou = "wang wang~"; return gou; }
};

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);
}

运行结果:

 

 posted on 2021-11-27 09:17  Popcorn_ZSJ  阅读(16)  评论(3编辑  收藏  举报