实验任务二

源码

#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,当Graph类型的指针ptr(虽然这个指针本身可能不是Graph类型,但是由于向上造型的原则,在fun函数中此指针被当作是一个Graph类型的指针)想要调用方法draw()时,从调用父类的方法变成了调用子类的方法

virtual的作用便是:给父类的方法加上virtual()意为在调用时,它的优先级被降低了,系统会先去看看子类中有没有可以被调用的,如果有就不会调用父类中的方法

何谓向上造型(upcast)

假设有一个父类people和一个子类student

那么一个people类型的指针*p可以指向一个student类型的类

实际上,任何一个student类型的对象都可以看作是一个people类型的对象

student people

student* people*

student& people&

因为在一个student类中其实是这样的:

class people
{
public:
int age;
}

class student : public people
{
public:
int score;
}

image.png*p是people类型,所以指向的是student类中people的部分

但为什么叫向上造型呢?

向上:因为一般我们把父类画在子类的上面

造型:(cast)在c语言中cast我们一般用在强制类型转换上,但为什么我们不叫向上造型为向上转换呢?

造型和转换是不一样的,转换代表一定会造成改变,比如一个double我们转换成int,那么小数部分的数据我们就损失了,但造型不是,当我们把一个student看作people,没有任何的数据被改变,成员变量score依然存在。

运用这样的特性,我们可以绕过"C++名字隐藏"的机制,访问那些被隐藏的方法

C++的向下造型

有向上造型就有向下造型,向上造型是把student看作people,那向下造型就是把people看作student

要注意的是,把student看作people是一定正确的,但是把people看作student却是一件有风险的事

所以向上造型是一定成功的,而向下造型是不一定的

若一个子类的父类中有一对overload函数,而子类中新定义了与父类中overload函数中的一个完全相同的函数,则父类中所有的同名的overload函数全部隐藏,不可使用,如需使用,则必须重写所有的同名overload函数

所有oop语言中只有c++这么做

为什么?

因为c++认为子类中与父类同名的函数他们之间没有关系,正因为没有关系,所以父类中所有同名函数都得消失,不然逻辑就乱了

实验任务三

task3.cpp

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


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

#pragma
#include <string>
#include <iostream>
using namespace std;
class Car
{

public:
Car(string newMaker,string newModel,int newYear)
:maker(newMaker),model(newModel),year(newYear),odometers(0){}
void info();
void update_odometers(int);
private:
string maker;
string model;
int year;
int odometers;
};

void Car::info()
{
using namespace std;
cout << "The information of the car:" << endl;
cout << "maker: " << maker << endl;
cout << "model: " << model << endl;
cout << "year: " << year << endl;
cout << "odometers: " << odometers << endl;
}

void Car::update_odometers(int newmeter)
{
if (newmeter < odometers)
{
std::cout << "Error! Incorrect update value" << std::endl;
return;
}
odometers = newmeter;
}

electricCar.hpp

#pragma
#include "battery.hpp"
#include "car.hpp"
using namespace std;

class ElectricCar :public Car
{
public:
ElectricCar(string newMaker, string newModel, int newYear, int bcapacity=70)
:Car(newMaker, newModel, newYear), battery(bcapacity) {}
void info();
private:
Battery battery;
};
void ElectricCar::info()
{
Car::info();
std::cout << "capacity: " << battery.get_capacity() << "-kWh" << std::endl;
}

battery.hpp

#pragma
class Battery
{
public:
Battery(int i = 70) :capacity(i) {}
int get_capacity() { return capacity; }
private:
int capacity;
};

截图

实验任务四

pets.hpp

#pragma
#include <iostream>
#include <string>
using namespace std;
class MachinePets
{
public:
MachinePets(const string s)
:nickname(s) {}
string const get_nickname()
{
return nickname;
}
string virtual talk() = 0;

private:
string nickname;
};

class PetCats:public MachinePets
{
public:
PetCats(const string s)
:MachinePets(s) {}
string talk()
{
return "miaomiao!";
}
};

class PetDogs :public MachinePets
{
public:
PetDogs(const string s)
:MachinePets(s) {}
string talk()
{
return "wangwang!";
}
};

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

截图