实验四 继承
2.实验任务2
#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);
}
运行截图

改动代码后的运行截图

同名覆盖原则
派生类与基类中有相同成员时,若未强行指名,则通过派生类对象使用的是派生类的同名成员。
二元作用域分辨符
用法为类名::成员,用来访问基类中的同名成员。
类型兼容原则
在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。通过公有继承,派生类得到了基类中除构造函数、析构函数之外的所有成员。
3.实验任务3
Battery.hpp
class Battery
{
public:
Battery(int capa = 70) :capacity{ capa } {}
int get_capacity()
{
return capacity;
}
private:
int capacity;
};
Car.hpp
#include<iostream> #include<string> #include<iomanip>
class Car { public: Car(string Maker, string Model, int Year, int Odometers = 0) : maker{ Maker }, model{ Model }, year{ Year },odometers{Odometers}{} void info() { const int N = 18; cout << setw(N) <<left<< "maker:" << maker << endl << setw(N) << left << "model:" << model << endl << setw(N) << left << "year:" << year << endl << setw(N) << left << "odometers:" << odometers << endl; } void update_odometers(int odom) { if (odom < odometers) { cout << "wrong info" << endl; return; } odometers = odom; } private: string maker; string model; int year; int odometers; };
ElectricCar.hpp
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class ElectricCar :public Car
{
public:
ElectricCar(string Maker,string Model,int Year,int Capacity=70):Car(Maker,Model,Year),battery{Capacity}{}
void info()
{
Car::info();
cout << setw(18) << "capacity:" << 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("Audi", "a4", 2018);
cout << "--------oldcar's info--------" << endl;
oldcar.update_odometers(11451);
oldcar.info();
cout << endl;
// test class of ElectricCar
ElectricCar newcar("Tesla", "model s", 2016);
newcar.update_odometers(1145);
cout << "\n--------newcar's info--------\n";
newcar.info();
}
运行截图

4.实验任务4
pets.hpp
#ifndef PETS_H
#define PETS_H
#include<iostream>
#include<string>
using namespace std;
class MachinePets
{
public:
MachinePets(const string s):nickname{s}{}
virtual string talk()
{
return " ";
}
string get_nickname()const
{
return nickname;
}
private:
string nickname;
};
class PetCats :public MachinePets
{
public:
PetCats(const string s):MachinePets(s){}
string talk()
{
return "mao 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 on 2021-12-22 15:37 19物联网一班李大伟 阅读(22) 评论(0) 收藏 举报

浙公网安备 33010602011771号