实验四 继承

任务1

期中练习1

#include<iostream>
#include<map>
#include<string>
int main()
{
    using namespace std;
    map<int,char> letter{{0,'a'},{1,'b'},{2,'c'},{3,'d'},{4,'e'},{5,'f'},{6,'g'},{7,'h'},{8,'i'},{9,'j'}};
    string stunum;
    while(cin>>stunum)
    {
        int i;
        for(i=0;i<stunum.size();i++)
        {
            cout<<letter[stunum[i]-'0'];
        }
        cout<<endl;
    }
    return 0;
}

 

 
期中练习2
#include<iostream>
#include<string>
#include<map>
using namespace std;
void d2n(int num,int n=2)
{
    map<int,char> letter{{10,'A'},{11,'B'},{12,'C'},{13,'D'},{14,'E'},{15,'F'}};
    if(n==2)
    {
        string ans;
        int i=0;
        while(num!=0)
        {
            ans[i]=(num%2)+'0';
            num=num/2;
            i++;
        }
        cout<<"Default:";
        for(int j=i-1;j>=0;j--)
        {
            cout<<ans[j];
        }
        cout<<endl;
        cout<<"Dec:\t";
        for(int j=i-1;j>=0;j--)
        {
            cout<<ans[j];
        }
        cout<<endl;
    }
    if(n==8)
    {
        string ans;
        int i=0;
        while(num!=0)
        {
            ans[i]=(num%8)+'0';
            num=num/8;
            i++;
        }
        cout<<"Oct:\t";
        for(int j=i-1;j>=0;j--)
        {
            cout<<ans[j];
        }
        cout<<endl;
    }
    if(n==16)
    {
        string ans;
        int i=0;
        while(num!=0)
        {
            if((num%16)>9)
            {
                ans[i]=letter[num%16];
            }
            else
            {
                ans[i]=(num%16)+'0';
            }
            num=num/16;
            i++;
        }
        cout<<"Hex:\t";
        for(int j=i-1;j>=0;j--)
        {
            cout<<ans[j];
        }
        cout<<endl;
    }
}
int main()
{
    int num;
    while(cin>>num)
    {
        d2n(num);
        d2n(num,8);
        d2n(num,16); 
    }
}

 

任务2
没有添加virtual
#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);
}

 

添加virtual:
#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);
}

 

归纳总结:
同名覆盖原则:

派生类与基类中有相同成员时,若未强行指名,则通过派生类对象使用的是派生类的同名成员。

二元作用域分辨符:
当派生类与基类中有相同成员时,如果要通过派生类对象访问基类中被隐藏的同名成员,可以用基类名和作用域分辨符来限定。

类型兼容原则:

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

派生类的对象可以隐含转换为基类对象;

派生类的对象可以初始化基类的引用;

派生类的指针可以隐含转换为基类的指针;

在替代之后,派生类的对象就可以作为基类的对象使用,但只能使用从基类继承的成员。
 
battery.hpp
#ifndef BATTERY_HPP
#define BATTERY_HPP
#include<iostream>
using namespace std;
class Battery
{
    public:
        Battery(int a=70) : capacity{a} {}
        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 a, string b, int c, int d=0) : maker{a}, model{b}, year{c}, odometers{d} {}
        void info()
        {
            cout<<"maker:\t\t"<<maker<<endl
                <<"moedl:\t\t"<<model<<endl
                <<"year:\t\t"<<year<<endl
                <<"odometers:\t"<<odometers<<endl;
        }
        void update_odometers(int e)
        {
            if(e<odometers)
            {
                cout<<"Wrong!Please update again!"<<endl;
            }
            else
            {
                odometers=e;
            }
        }
    private:
        string maker;
        string model;
        int year;
        int odometers;
};
#endif

 

electriccar.hpp

#ifndef ELECTRICCAR_HPP
#define ELECTRICCAR_HPP
#include<iostream>
#include"car.hpp"
#include"battery.hpp"
using namespace std;
class ElectricCar:public Car
{
    public:
        ElectricCar(string a, string b, int c, int d=0, int e=70) : Car(a, b, c, d), battery{e} {}
        void info()
        {
            Car::info();
            cout<<"battery:\t"<<battery.get_capacity()<<"-KWh"<<endl;
        }
    private:
        Battery battery;
};
#endif

 

task3.cpp

#include <iostream>
#include "electricCar.hpp"

int main()
{
    using namespace std;

    // test class of Car
    Car oldcar("Audo", "a1", 2021);
    cout << "--------oldcar's info--------" << endl;
    oldcar.update_odometers(35000);
    oldcar.info();

    cout << endl;

    // test class of ElectricCar
    ElectricCar newcar("Tesll", "model s", 2021);
    newcar.update_odometers(3500);
    cout << "\n--------newcar's info--------\n";
    newcar.info();
}

 

pets.hpp
#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() const
        {
            return nickname;
        }
        virtual string talk() {}
    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);
}

 

posted @ 2021-11-29 22:17  过路游人XYZ  阅读(18)  评论(3编辑  收藏  举报