实验四

#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);
    fun(&r1);
    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);
    fun(&r1);
    fun(&c1);
}

 

 

task3

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

car.hpp

#ifndef Car_hpp
#define Car_hpp

#include<iostream>
#include<string>

using namespace std;

class Car
{
    public:
        Car(string maker1, string model1, int year1, int odometers1=0): maker(maker1), model(model1), year(year1), odometers(odometers1) { };
        ~Car(){}
        virtual void info();
        void update_odometers(int new_odometers);
    private:
        string maker;
        string model;
        int year;
        int odometers;
};

void Car::info(){
    cout<<"maker:\t\t"<<maker<<endl;
    cout<<"model:\t\t"<<model<<endl;
    cout<<"year:\t\t"<<year<<endl;
    cout<<"odometers:\t"<<odometers<<endl;
}

void Car::update_odometers(int new_odometers){
    if(new_odometers<odometers)
    cout<<"更新数值有误"<<endl;
    else
    odometers=new_odometers;
}

#endif

electriccar.hpp

#ifndef ElectricCar_hpp
#define ElectricCar_hpp

#include<iostream>
#include<string>
#include"Car.hpp"
#include"Battery.hpp"

using namespace std;

class ElectricCar: public Car{
    public:
     ElectricCar(string a, string o, int y, int c=60): Car(a, o, y), battery(c) {
     };
     void info();
    private:
        Battery battery;
};

void ElectricCar::info(){
    Car::info();
    cout<<"capacity:    "<<battery.get_capacity()<<"-kwh"<<endl;
}

#endif

battery.hpp

#ifndef Battery_hpp
#define Battery_hpp

using namespace std;

class Battery{
    public:
        Battery(int c=60): capacity(c){
        };
        int get_capacity(){
            return capacity;
        }
    private:
        int capacity;
};

#endif

 

 

task4

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

pets.hpp

#ifndef PETS_HPP
#define PETS_HPP
#include<string> using namespace std; class MachinePets{ public: MachinePets(const string s): nickname(s){ } string get_nickname() const; virtual string talk(); private: string nickname; }; string MachinePets::get_nickname() const{ return nickname; } string MachinePets::talk(){ return "hahahha"; } class PetCats: public MachinePets { public: PetCats(const string s): MachinePets(s){} string talk() { return "miao~wu~"; } private: string nickname; }; class PetDogs: public MachinePets { public: PetDogs(const string s): MachinePets(s){ } string talk() { return "wang ~wang ~"; } private: string nickname; }; #endif

 

posted @ 2021-11-30 15:26  yxg208  阅读(54)  评论(3)    收藏  举报