Polymorphism and interface of C + + foundation

Posted by adren on Tue, 28 Dec 2021 07:18:23 +0100

01. Polymorphism (key and difficult points)

1. What is polymorphism:

When the same operation acts on different objects, it can have different explanations and produce different effects. This is polymorphism

class People
{
public:
	//virtual function
	virtual void Mypro()
	{

	}
};

class xishi :public People
{
public:
	//Override the virtual function of the parent class
	virtual void Mypro()
	{
		cout << "Josh" << endl;
	}
};

class wangzhaojun :public People
{
public:
	//Override the virtual function of the parent class
	virtual void Mypro()
	{
		cout << "About Wang Zhaojun" << endl;
	}
};



class diaochan :public People
{
public:
	//Override the virtual function of the parent class
	virtual void Mypro()
	{
		cout << "About Diao cicada" << endl;
	}
};

class yangguifei :public People
{
public:
	//Override the virtual function of the parent class
	virtual void Mypro()
	{
		cout << "About Yang Guifei" << endl;
	}
};


//Same operation
void doLogin(People *pro)
{
	pro->Mypro();//Produce different effects
}

void test()
{
	People *pro = NULL;
	pro = new xishi;
	doLogin(pro);//Different objects
	delete pro;

	pro = new wangzhaojun;
	doLogin(pro);//Different objects
	delete pro;

	pro = new diaochan;
	doLogin(pro);//Different objects
	delete pro;

	pro = new yangguifei;
	doLogin(pro);//Different objects
	delete pro;
}
2. What's the use of polymorphism

1. It can solve the tight coupling problem in the project and provide the scalability of the program

2. The application does not have to write code for the function call of each subclass, but only needs to process the abstract parent class

3. Three conditions for polymorphism

1. There is inheritance. 2. Override the virtual function of the parent class. 3. The parent class pointer points to the child class object

02. Implementation principle of polymorphism (key and difficult points)

class Animal
{
public:
	virtual void speak()
	{
		cout << "Anima speak()" << endl;
	}
	
};
class Dog :public Animal
{
public:
	virtual void speak()
	{
		cout << "Dog speak()" << endl;
	}
};

void test()
{
	cout << sizeof(Animal) << endl;

	Animal *animal = new Dog;
	animal->speak();
}

03. Pure virtual functions and abstract classes (emphasis)

1. Dependency inversion

The business layer depends on the abstract layer, and the implementation layer depends on the abstract layer

//Abstraction layer
class rule
{
public:
	virtual int getnum(int a,int b)
	{
		return 0;
	}
};

//Implementation layer
class plus_rule :public rule
{
public:
	virtual int getnum(int a, int b)//Override the virtual function of the parent class and rely on the abstraction layer
	{
		return a+b;
	}
};




//Business layer
int doLogin(rule *cal)
{
	
	int a = 10;
	int b = 20;
	

	int ret=cal->getnum(a, b);//Dependency abstraction layer

	return ret;

}

void test()
{
	rule *r = NULL;
	r = new plus_rule;
	cout << doLogin(r) << endl;
	delete r;

}

2. Opening and closing principle

Close the modified source code and develop new extended functions

//Abstraction layer
class rule
{
public:
	virtual int getnum(int a,int b)
	{
		return 0;
	}
};

//Implementation layer
class plus_rule :public rule
{
public:
	virtual int getnum(int a, int b)//Override the virtual function of the parent class and rely on the abstraction layer
	{
		return a+b;
	}
};
//Expand new features
class miux_rule :public rule
{
public:
	virtual int getnum(int a, int b)
	{
		return a - b;
	}
};


//Business layer
int doLogin(rule *cal)
{
	
	int a = 10;
	int b = 20;
	

	int ret=cal->getnum(a, b);//Dependency abstraction layer

	return ret;

}

void test()
{
	rule *r = NULL;
	r = new plus_rule;
	cout << doLogin(r) << endl;
	delete r;
	//Added code
	r = new miux_rule;
	cout << doLogin(r) << endl;
	delete r;

}

3. Pure virtual function

class rule
{
public:
	//Pure virtual function
	virtual int getnum(int a, int b) = 0;
};

4. Abstract class

1. Classes with pure virtual functions are called abstract classes and cannot instantiate objects

//Classes with pure virtual functions are called abstract classes and cannot instantiate objects
class rule
{
public:
	//Pure virtual function
	virtual int getnum(int a, int b) = 0;
};

void test02()
{
	//Abstract classes cannot instantiate objects
	//rule r;
}

2. If a subclass inherits an abstract class, the subclass must implement all pure virtual functions of the abstract class, otherwise the subclass will become an abstract class

class Maker
{
public:
	virtual void func1() = 0;
	virtual void func2() = 0;
};

class Son :public Maker
{
public:
	virtual void func1()
	{

	}
	virtual void func2()
	{

	}
};

void test03()
{
	Son s;
}

04. Definition of interface (understand)

1. The so-called interface encapsulates the internal implementation details. External users can use the functions of the interface after using the reserved interface without knowing the internal specific details. In C + +, object-oriented programming is realized through classes, while only the declaration of pure virtual function is given in the base class, and then the interface is realized by realizing the specific definition of pure virtual function in the derived class. Different derived classes implement the interface in different ways

//abstract class
class Father
{
public:
	virtual void func1() = 0;//Declaration of interface
	virtual void func2(int a) = 0;
	virtual void func3(int a,int b) = 0;
};

class Son :public Father
{
public:
	virtual void func1()//Implementation of interface
	{

	}
	virtual void func2(int a)
	{

	}
	virtual void func3(int a, int b)
	{

	}
};

05. Template method mode (key)

1. Determine the calling order of functions in the abstract class

class Drink
{
public:
	//boil water
	virtual void Boil() = 0;
	//Brew
	virtual void Brew() = 0;
	//Import into cup
	virtual void PourInCup() = 0;
	//Add some accessories
	virtual void addSonm() = 0;

	//Template method to determine the order of calling functions
	void func()
	{
		Boil();
		Brew();
		PourInCup();
		addSonm();
	}
};

06. Virtual destructor and pure virtual destructor (emphasis)

1. The virtual destructor is to solve the problem that the base class pointer points to the derived class object and release the derived class object with the base class pointer

class Animal
{
public:
	Animal()
	{
		cout << "Animal Structure of" << endl;
	}

	//The virtual destructor calls the destructor of the subclass
	virtual ~Animal()
	{
		cout << "Animal Deconstruction of" << endl;
	}
};

class Son :public Animal
{
public:
	Son()
	{
		cout << "Son Structure of" << endl;
		pName = new char[64];
		memset(pName, 0, 64);
		strcpy(pName, "Like flowers");
	}

	~Son()
	{
		cout << "Son Deconstruction of" << endl;
		if (pName != NULL)
		{
			delete[] pName;
			pName = NULL;
		}
	}

public:
	char *pName;
};

void test()
{
	Animal *animal = new Son;
	delete animal;
}

2. Pure virtual destructors. Classes with pure virtual destructors are abstract classes and cannot instantiate objects

Note: the pure virtual destructor needs to be implemented outside the class

class Animal
{
public:
	Animal()
	{
		cout << "Animal Structure of" << endl;
	}

	//The virtual destructor calls the destructor of the subclass
	/*virtual ~Animal()
	{
		cout << "Animal Deconstruction of "< endl;
	}*/

	//Pure virtual destructor, which needs to be implemented outside the class
	virtual ~Animal() = 0;
};

//Implement a pure virtual destructor outside the class
Animal::~Animal()
{
	cout << "Animal Pure virtual deconstruction of" << endl;
}

3. Difference between virtual destructor and pure virtual destructor:

1. Classes with pure virtual destructors are abstract classes, which cannot instantiate objects, and should be implemented outside the class

2. The virtual destructor does not need to be implemented outside the class

07. Polymorphic cases_ Zoo (key)

1. Create animal base class

class Animal
{
public:
	virtual void speak() = 0;
};

2. Create animals

class Dog :public Animal
{
public:
	Dog(string name)
	{
		this->name = name;
	}
	virtual void speak()
	{
		cout << "puppy" << name << "Woof, woof" << endl;
	}
private:
	string name;
};

class Dark :public Animal
{
public:
	Dark(string name,string type)
	{
		this->name = name;
		this->type = type;
	}
	virtual void speak()
	{
		cout <<type<<"Branded"<< "duckling" << name << "GA GA" << endl;
	}
private:
	string name;
	string type;
};

class Tiger :public Animal
{
public:
	Tiger(string name,int age)
	{
		this->name = name;
		this->age = age;
	}
	virtual void speak()
	{
		cout << age << "year" << "tiger" << name << "Ow, Ow" << endl;
	}
private:
	string name;
	int age;

};

3. Create a zoo

class Zoo
{
public:
	Zoo()
	{
		mCapacity = 1024;
		mSize = 0;
		//Application space, space for storing Animal *, pointer array
		this->p = new Animal*[mCapacity];
	}

	//Increase animals
	int AddAnimal(Animal* animal)
	{
		if (mCapacity == mSize)
		{
			return -1;
		}
		//Store pointer in pointer array
		this->p[mSize] = animal;
		mSize++;

		return 0;
	}

	void StartSpeak()
	{
		for (int i = 0; i < mSize; i++)
		{
			p[i]->speak();
		}
	}

	//Destructor
	~Zoo()
	{
		//Free the heap space pointed to by the pointer in the pointer array first
		for (int i = 0; i < mSize; i++)
		{
			if (p[i] != NULL)
			{
				delete p[i];
				p[i] = NULL;
			}
		}

		//Release pointer array
		delete[] p;
		p = NULL;
	}


private:
	Animal* *p;
	int mCapacity;//capacity
	int mSize;

};

4. Use

void test()
{
	//Create a zoo
	Zoo *zoo = new Zoo;

	//Add animal
	zoo->AddAnimal(new Dog("Jin San"));
	zoo->AddAnimal(new Dark("Black duck", "Zhou Heiya"));
	zoo->AddAnimal(new Tiger("Three fat", 35));

	zoo->StartSpeak();

	//Burned the zoo
	delete zoo;
}

08. Parent class references child class object

class Animal
{
public:
	virtual void speak()
	{
		cout << "Animal speak()" << endl;
	}
};

class Dog :public Animal
{
public:
	virtual void speak()
	{
		cout << "Dog speak()" << endl;
	}
};

void test()
{
	Animal &animal = Dog();
	animal.speak();

	Animal* dog = new Dog();
	Animal* &an = dog;
	an->speak();
	delete dog;
}

09. Override, overload, redefine (hide)

1. Overloaded functions with the same name in the same scope

2. Rewrite (overwrite):

​ 1. Have inheritance

​ 2. The subclass (derived class) overrides the virtual function of the parent class (base class)

​ 3. The function return value, function name and function parameters must be consistent with the virtual function in the base class

3. Redefine (hide)

​ 1. Have inheritance

​ 2. A subclass (derived class) redefines a member of the parent class (base class) with the same name (non virtual function)

Topics: C++ Back-end