C++基础二

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/daividtu/article/details/78319260

继承

Plane.h

//一个类中有纯虚函数,那么这个类就是抽象类 抽象类不能够实例化
//一个类中只有纯函数,那么它是一个接口
class Plane
{
public:
	Plane();
	Plane(std::string name, int year);
	~Plane();

	virtual void fly();

	virtual void land();

	void printf();
	//纯虚函数
	//virtual void test() = 0;
protected:
	std::string name;
	int year;
};
Plane.cpp

#include <Plane.h>
#include <iostream>

using namespace std;


Plane::Plane() :name("湾流"), year(1991) {

	cout << "Plane  无参构造函数" << name << year << endl;

}

Plane::Plane(std::string name, int year):name(name),year(year) {

	cout << "Plane 构造函数" << name<< year<<endl;

}

Plane::~Plane()
{
	cout << "Plane 析构函数"  << endl;
}

void Plane::fly() {
	cout << "Plane fly" << endl;
}

void Plane::land() {
	cout << "Plane land" << endl;
}

void Plane::printf() {
	cout << "Plane printf " << "name = "<< name << " year = "<< year << endl;
}

Jet.h

#pragma once

#include <Plane.h>

//如果子类没有实现纯虚函数,那么它也是个抽象类,反之,就不是抽象类
class Jet : public Plane
{
public:
	Jet();
	Jet(std::string name, int year);
	~Jet();

	 void fly();

	 void land();
	 void test(); 
}
Jet.cpp

#include <Jet.h>
#include <iostream>

using namespace std;

Jet::Jet(){

	cout << "Jet  无参构造函数" << name << year << endl;
}

Jet::Jet(std::string name, int year) {

	cout << "Jet  构造函数" << name << year << endl;
}


Jet::~Jet(){
	cout << "Jet 析构函数" << endl;
}

//重写
void Jet::fly() {
	cout << "Jet fly" << endl;
}

void Jet::land() {

	cout << "Jet land" << endl;

}

void Jet::test() {

}
Copter.h

#pragma once
#include <Plane.h>

class Copter : public Plane
{
public:
	Copter();
	Copter(std::string name, int year);
	~Copter();

	void fly();

	void land();

};
copter.cpp
#include <Copter.h>
#include <iostream>

using namespace std;

Copter::Copter() {

	cout << "Copter  无参构造函数" << name << year << endl;
}

Copter::Copter(std::string name, int year) {

	cout << "Copter  构造函数" << name << year << endl;
}


Copter::~Copter() {
	cout << "Copter 析构函数" << endl;
}

//重写
void Copter::fly() {
	cout << "Copter fly" << endl;
}

void Copter::land() {

	cout << "Copter land" << endl;

}
main.cpp
void Funx(Plane &plane) {
	plane.fly();
	plane.land();//加了虚函数 执行子类的land() 没有执行父类的
}

void main() {
	Jet jet  = Jet("波音707",1997);
	Funx(jet);
	Copter copter = Copter("绝影", 2005);
	Funx(copter);
	system("pause");
}
多继承的二义性
class A
{
public:
	string name;
};

class A1:virtual public A
{	 
};

class A2 : virtual public A
{
};

//多继承的二义性 :只能显示使用根属性
// 虚继承 : 解决路径不明确的问题,使多个继承的同名成员时候 只有一份拷贝
class B : public A1, public A2
{


};

void main() {
	B b;
	b.A1::name = "Tim"; //只能显示调用
	b.A2::name = "AV";
	b.A::name = "ricky";

	b.name = "David";//非要b.name 虚继承
	system("pause");
}
//作业 :解决以下内存泄漏的问题

void zipFun() {

	//用父类的指针来出事话一个子类的对象
	Plane *jet = new Copter();
	//调用了子类中的虚函数
	jet->land();

	//回收 //这里子类的虚构函数没有执行 有可能造成内存泄漏  只需要把父类的虚构函数 加上 virtual关键字 即可决解此问题
	delete jet;
	jet = nullptr;   //这种情况存在内存泄漏的可能
}

class Plane
{
public:
	Plane();
	Plane(std::string name, int year);
	virtual ~Plane();//决解作业内存泄漏问题

	virtual void fly();

	virtual void land();

	void printf();
	//纯虚函数
	//virtual void test() = 0;
protected:
	std::string name;
	int year;
}






猜你喜欢

转载自blog.csdn.net/daividtu/article/details/78319260