UE4-C++ core programming interface---interface in C++

The appearance of interface has made object-oriented a big step forward, and at the same time, many requirements have become predictable, especially the combination of interface and inheritance makes the program have the ability to "foresee the future".

---Mantra

What is an interface?

An interface is an abstraction provided by itself to the outside world, which is used to separate the method of external communication from the internal operation, so that it can be modified internally without affecting the way other entities interact with the outside world.

Many programming languages ​​provide support for interfaces, but unfortunately, C++ itself does not support interfaces. But we can implement the interface by combining multiple inheritance and pure virtual functions in C++.

Specific operation method:

The theory is omitted here and directly on the brick (code). First define the IPerson interface and define some behaviors (pure virtual functions)

#pragma once

#include <string>

class IPerson
{
public:
	IPerson () {};
	// It is better to specify a pure virtual function here, so as to ensure that the subclass can correctly call the destructor
	virtual ~ IPerson () = 0 {};

	// The following are the defined interface functions, which are convenient for external calls
	virtual void SetName(const std::string& strName) = 0;

	virtual const std::string GetName() = 0;

	virtual void Work() = 0;
};
Next, we define two classes that implement the IPerson interface respectively.

First define the Teacher class (the following are the .h and .cpp files respectively):

#pragma once
#include <string>
#include "IPerson.h"



class Teacher:public IPerson
{
public:
	Teacher();
	Teacher(std::string name);
	virtual ~Teacher();

	// inherit from IPerson
	virtual void SetName(const std::string & strName) override;
	virtual const std::string GetName() override;
	virtual void Work() override;

protected:
	std::string strName;
};

#include "Teacher.h"
#include <iostream>


Teacher::Teacher()
{
}

Teacher::Teacher(std::string name)
{
	strName = name;
}


Teacher::~Teacher()
{
	std::cout << "Delete Teacher: " << this->strName << std::endl;
}

void Teacher::SetName(const std::string & strName)
{
	this->strName = strName;
}

const std::string Teacher::GetName()
{
	return this->strName;
}

void Teacher::Work()
{
	std::cout << "I'm teaching Student" << std::endl;
}

Next, define and implement the Student class, which also implements the IPer'son interface

#pragma once

#include "IPerson.h"
#include <string>

class Student: public IPerson
{
public:
	Student();
	Student(std::string name);
	~Student();

	// inherit from IPerson
	virtual void SetName(const std::string & strName) override;
	virtual const std::string GetName() override;
	virtual void Work() override;

protected:
	std::string strName;
};


#include "Student.h"
#include <iostream>


Student::Student()
{
}

Student::Student(std::string name)
{
	strName = name;
}


Student::~Student()
{
	std::cout << "Delete Student: " << this->strName << std::endl;
}

void Student::SetName(const std::string & strName)
{
	std::cout << "Change Student name from " << this->strName << " to "<< strName<<std::endl;
	this->strName = strName;
}

const std::string Student::GetName()
{
	std::cout << "You wana get the student name? " << std::endl;
	std::cout << "But do you have the right...." << std::endl;
	std::cout << "Em... You have" << std::endl;
	std::cout << this->strName << std::endl;
	return this->strName;
}

void Student::Work()
{
	std::cout << "I'm a goodboy, and I have to study." << std::endl;
	std::cout << "So my work is Studying" << std::endl;
}
Next, test the program we wrote in the main function (headers.h contains the necessary header files)

#include <iostream>
#include "Headers.h"

using namespace std;

intmain()
{
	cout << "Hello World" << endl;

	// Test the interface written by yourself (using polymorphism)
	IPerson* Person = new Teacher("Mantra");
	std::cout << Person->GetName() << std::endl;
	Person->SetName("MantraGuo");
	std::cout << Person->GetName() << std::endl;
	Person->Work();

	std::cout << "====================================" << std::endl;
	std::cout << std::endl;
	std::cout << "ornate dividing line" << std::endl;
	std::cout << "====================================" <<std::endl;
	std::cout << std::endl;


	Person = new Student("NaDaCi");
	std::cout << Person->GetName() << std::endl;
	Person->SetName("MantraGuo");
	std::cout << Person->GetName() << std::endl;
	Person->Work();


	return 0;
}

Everything is ready, the next step is to test, the results are as follows:


Finally, attach the VS project for testing. Link: http://pan.baidu.com/s/1i4YMsJj Password: 21fl.





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325457471&siteId=291194637