实验4 继承
实验任务2
task2.cpp
#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);
}
运行截图
微调代码后的运行截图
归纳总结
同名覆盖原则:
派生类与基类中有相同成员时,若未强行指名,则通过派生类对象使用的是派生类的同名成员;
如果要通过派生类的对象访问基类被覆盖的同名成员,需要加 对象名.基类名::同名成员 来限定
二元作用域分辨符:
用法是:类名::成员
类型兼容原则:
一个公有派生类的对象在使用上可以被当作基类的对象,反之则禁止。
具体表现在:
1.派生类的对象可以被赋值给基类对象。
2.派生类的对象可以初始化基类的引用。
3.基类的对象指针也可以指向派生类。但是如果派生类的指针想要指向基类,则必须要将基类对象的地址强转为派生类类型的。
实验任务3
battery.hpp
#ifndef _BATTERY_HPP_
#define _BATTERY_HPP_
class Battery
{
public:
Battery(int cap = 70) : capacity{cap} {}
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() = default;
Car(string m, string md, int y) : maker{m}, model{md}, year{y}, odometers{0} {}
void info();
void update_odometers(int x);
private:
string maker, model;
int year, odometers;
};
void Car::info()
{
cout << "maker: " << maker << endl;
cout << "model: " << model << endl;
cout << "year: " << year << endl;
cout << "odometers: " << odometers << endl;
}
void Car::update_odometers(int x)
{
if(x > odometers)
odometers = x;
else
cout << "new odometers is less than now! please check before input" << endl;
}
#endif
electricCar.hpp
#ifndef _ELECTRICCAR_HPP_
#define _ELECTRICCAR_HPP_
#include "car.hpp"
#include "battery.hpp"
#include <string>
class ElectricCar : public Car
{
public:
ElectricCar(string m, string md, int y, int b = 70) : Car(m, md, y), battery(b) {}
void info();
private:
Battery battery;
};
void ElectricCar::info()
{
Car::info();
cout << "capacity: " << battery.get_capacity() << "-kWh" << endl;
}
#endif
task3.cpp
#include <iostream>
#include "electricCar.hpp"
int main()
{
using namespace std;
// test class of Car
Car oldcar1("Audi", "a4", 2016);
oldcar1.update_odometers(25000);
oldcar1.update_odometers(10000);
cout << "--------oldcar's info--------" << endl;
oldcar1.info();
Car oldcar2("Audi", "A6 allroad quattro", 2021);
oldcar2.update_odometers(100);
cout << "--------oldcar's info--------" << endl;
oldcar2.info();
cout << endl;
// test class of ElectricCar
ElectricCar newcar1("Tesla", "model s", 2016);
newcar1.update_odometers(2500);
newcar1.update_odometers(10);
cout << "\n--------newcar's info--------\n";
newcar1.info();
ElectricCar newcar2("ZEEKR", "001", 2021, 100);
newcar2.update_odometers(2500);
cout << "\n--------newcar's info--------\n";
newcar2.info();
}
运行截图
实验任务4
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() {return "";}
private:
string nickname;
};
string MachinePets::get_nickname() const {return nickname;}
class PetCats : public MachinePets
{
public:
PetCats(const string s) : MachinePets(s) {}
string talk();
};
string PetCats::talk() {return "miao wu~";}
class PetDogs : public MachinePets
{
public:
PetDogs(const string s) : MachinePets(s) {}
string talk();
};
string PetDogs::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);
}