其他分享
首页 > 其他分享> > 【实验四】继承

【实验四】继承

作者:互联网

目录

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

运行结果:
image-20211125135146269

对代码稍作改动,在基类Graph的成员函数draw()前面加一个关键字virtual,重新编译运行程序

// definitation of Graph
class Graph
{ 
public:
	// 声明时加了关键字virtual
	virtual void draw() { std::cout << "Graph::draw() : just as aninterface\n"; }
};

运行结果:
image-20211125135422888

加 virtual 之前, 派生类中与基类的同名函数实行的是覆盖隐藏, 但依旧可以通过二元作用域分辨符访问到基类的函数;
加 virtual 之后, 派生类中实现基类的同名函数定义, 此时即使是通过二元作用域分辨符访问基类的函数, 函数体也依旧是派生类中实现的.+66+

任务3

使用类的组合和继承模拟简单的车辆信息管理。
问题场景描述如下:
为了对车量基本信息进行管理,对现实世界车量基本信息抽象后,抽象出Car类、ElectricCar类、Battery类,它们之间的关系描述如下:ElectricCar类公有继承自Car类,ElectricCar中新增数据成员为Battery类对象。
类图简化模型如图所示。

image-20211126101517540

Car.hpp

#include <iostream>
#include <string>

using namespace std;

class Car
{
private:
	string maker;  //制造商
	string model;  //型号
	string year;   //生产年份
	int odometers; //行车里程数
public:
	Car(){};
	Car(string maker, string model, string year) : maker(maker), model(model), year(year), odometers(0){};

	void info()
	{
		cout << "制造商: " << maker << endl
			 << "型号: " << model << endl
			 << "生产年份: " << year << endl
			 << "行车里程数: " << odometers << endl;
	}

	void update_odometers(int odometers){
		this->odometers = odometers;
	}
};

Battery.hpp

#include <iostream>

using namespace std;

class Battery
{
private:
	int capacity; // (kWh)
public:
	Battery(){};
	Battery(int capacity): capacity(capacity){};
	
	int get_capacity(){
		return capacity;
	}
};

ElectricCar.hpp

#include "Car.hpp"
#include "Battery.hpp"

class ElectricCar: public Car
{
private:
	Battery battery;
public:
	ElectricCar(){};
	ElectricCar(string maker, string model, string year, Battery battery): Car(maker, model, year), battery(battery){};

	void info(){
		Car::info();
		cout << "电池: " << battery.get_capacity();
	}
};

test.cpp

#include "ElectricCar.hpp"

int main(){
	string maker = "SONATA";
	string model = "GB9417-88";
	string year = "2021.11.24";

	Battery bettery(150000);

	ElectricCar electricCar(maker, model, year, bettery);
	electricCar.info();

	return 0;
}

运行结果:
image-20211126112447003

任务4

使用类的继承,模拟简单的机器宠物。
问题场景描述如下:
对机器宠物进行抽象后,抽象出三个简单的类:机器宠物类MachinePets、宠物猫类PetCats、宠物狗类PetDogs。
它们之间的关系如UML类图所示:

image-20211126112845236

MachinePets.hpp

#include <string>
#ifndef MACHINEPETS_hpp
#define MACHINEPETS_hpp
using namespace std;

class MachinePets
{
private:
	string nickname;
public:
	MachinePets(){};
	MachinePets(const string nickname): nickname(nickname){};

	const string get_nickname(){
		return nickname;
	}

	virtual string talk(){
		return "none";
	};
};

#endif

PetCats.hpp

#include "MachinePets.hpp"
#ifndef PETCATS_HPP
#define PETCATS_HPP
class PetCats: public MachinePets
{
public:
	PetCats(){};
	PetCats(string cat_name): MachinePets(cat_name){};

	string talk(){
		return ("i am a cat, my name is" + get_nickname() + " miao~");
	}
};
#endif

PetDogs.hpp

#include "MachinePets.hpp"
#include <iostream>
#ifndef PETDOGS_HPP
#define PETDOGS_HPP
class PetDogs: public MachinePets
{
public:
	PetDogs(){};
	PetDogs(string dog_name): MachinePets(dog_name){};

	string talk(){
		return ("i am a dog, my name is" + get_nickname() + " wang~");
	}
};
#endif

test.cpp

#include <iostream>
#include "MachinePets.hpp"
#include "PetCats.hpp"
#include "PetDogs.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);
}

运行结果:
image-20211126161752719

标签:string,继承,Car,hpp,Battery,实验,MachinePets,include
来源: https://www.cnblogs.com/batianhan/p/15608384.html