Experiment 4 Inheritance

task2

//origin

#include<iostream>
#include<typeinfo>

using namespace std;

//definition og Graph
class Graph
{
public:
    //Add keywords when declaring virtual
     void draw() { cout << "Graph::draw(): just as an interface\n"; };
};

//definition of Rectangle, derived from Graph
class Rectangle :public Graph
{
public:
    void draw()
    {
        cout << "Rectangle::draw(): programs of draws a rectangle\n";
    }
};

//definition of Circle, derived from Graph
class Circle :public Graph
{
public:
    void draw()
    { 
        cout << "Circle::draw() : programs of draws a rectangle\n";
    }
};

//definition of fun():as a call interface
void fun(Graph* ptr)
{
    //typeid: Used to get runtime type information
    cout << "Pointer type: " << typeid(ptr).name() << "\n";
    //Runtime Type Info Abbreviation, runtime type information.
    cout << "RTTI typde: " << 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();

    cout << endl;

    //call by object name, and using the scope resolution operator::
    r1.Graph::draw();
    c1.Graph::draw();

    cout << endl;

    //call by pointer to Base class
    fun(&g1);
    fun(&r1);
    fun(&c1);
}

 

//after

 

Override by the same name principle:
The visibility principle for identifiers declared by different scopes:

If there are two or more scopes with inclusion relationships, the outer layer declares an identifier and the inner layer does not declare the same name identifier again, the outer identifier will still be visible inside. If the inner layer declares the same name identifier, the outer identifier will not be visible inside,.

The phenomenon that an internal identifier hides an external identifier with the same name is called a hiding rule.

  
Binary Scope Discriminator:
The scope operator (:) functions:
(1) Access global variables when there are local variables with the same name.
(2) Define class-related functions outside of classes.
(3) Access static variables of the class.
(4) In the case of multiple inheritance, the scope operator can be used to distinguish between two base classes if they have the same variable name.
(5) Qualify the class to which member functions belong.
 
 
Type Compatibility Principle
(1)     Objects of derived classes can be assigned to base class objects

(2)     Objects of derived classes can initialize references to base classes.

(3)     The address of the derived class object can be assigned to the pointer of the base class.

task 3

//battery.hpp

#ifndef BATTERY_HPP
#define BATTERY_HPP

#include<iostream>
#include<string>

using namespace std;

class Battery {
public:
    Battery(string cap = "70-kWh") :capacity(cap) {}
    string get_capacity(){
        return capacity;
    }
private:
    string capacity;
};

#endif

//car.hpp

#ifndef CAR_HPP
#define CAR_HPP

#include<iostream>

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: " << maker << endl
             << "model: " << model << endl
             << "year: " << year << endl
             << "odometers: " << odometers << endl;
    }
    void update_odometers(int n)
    {
        if (n < odometers)
        {
            printf("Wrong Value!\n");
            return;
        }
        else
        {
            odometers = n;
        }
    }
private:
    string maker;
    string model;
    int year;
    int odometers;
};

#endif

//electricCar.hpp

#ifndef ELECTRIC_HPP
#define ELECTRIC_HPP

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

using namespace std;

class ElectricCar :public Car{
public:
    ElectricCar(string maker, string model, int year, int odo = 0, string b = "70-kMh") :
        Car(maker, model, year, odo), battery(b) {}
    void info()
    {
        Car::info();
        cout << battery.get_capacity() << endl;
    }
private:
    Battery battery;
};


#endif

//main.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,250,"250-kMh");
    newcar.update_odometers(25000);
    cout << "\n--------newcar's info--------\n";
    newcar.info();
}

 

 

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

    string get_nickname()
    {
        return nickname;
    }

    virtual string talk()
    {
        return "sdfaf";
    }
private:
    string nickname;
};

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


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

#endif

//main.cpp

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

void play(MachinePets* ptr)
{
    std::cout << ptr->get_nickname() << " says "
        << ptr->talk() << std::endl;
}

int main()
{
    PetCats cat("duo duo");

    PetDogs dog("da huang");

    play(&cat);
    play(&dog);
}

 

Summary:

1. Class Inheritance:

The constructor directly writes the name of the inherited class

2. Combination of classes:

Adding members of types of other classes to data members

3. Same name:

(1) If a new function with the same name as a member function of the base class is declared in a derived class, all overloaded forms of functions with the same name inherited from the base class will be hidden, even if the parameter table of the function is different.

(2) If more than one base class of a derived class has a member with the same name, and a derived class adds such a member with the same name, the derived class will hide the members with the same name for all base classes.

*   Uniquely identifies and accesses new members of derived classes:

Object name. Derived class or object pointer name - >member name  

The name of a class inherited by a function with the same name as the inherited class:

4.VS

If the virtual function in VS has a return value, it needs to return a value of the corresponding type

Posted on Wed, 24 Nov 2021 13:07:45 -0500 by snowman2344