C + + / subclass introduction / access level / homonymy of member function

concept

There is a hierarchical relationship between classes, including father class and child class
Parent class: base class, superclass
Child class: subclass, derived class
Inheritance: there are fathers and children, which constitute a hierarchical relationship

(object oriented has three cores: inheritance, encapsulation and polymorphism)

inherit

This inheritance simply means that a parent class needs to be defined first. Some common member variables and member functions are defined in the parent class. We construct a new class, that is, a subclass, by inheriting the parent class;

Therefore, when writing code, you only need to write some content related to subclasses

Example

"Human.h"

#ifndef  __HUMAN__
#define  __HUMAN__

#include <iostream>
using namespace std;

class Human
{
public:
	Human();
	Human(int);
	
private:
	int m_Age;
	char m_Name[100];

};

#endif // !__HUMAN__

"Human.cpp"

#include "Human.h"

Human::Human()
{
	cout << "Constructor executed Human::Human()" <<endl;
}

Human::Human(int)
{
	cout << "Constructor executed Human::Human(int)" << endl;
}

"Man.h"

#ifndef  __MEN__
#define  __MEN__

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

using namespace std;

class Men : public Human  //Indicates that Men is a subclass of Human
{
public:
	Men();

};
#endif

"Man.cpp"

#include "Men.h"

Men::Men()
{
	cout << "Constructor executed Men::Men()" << endl;
}

"Main"

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

int main()
{
	Men men;		//Constructor Human::Human() executed
							//Constructor Men::Men() executed
	//When defining a subclass object, it is necessary to call the constructor of the parent class and subclass. Moreover, the function body of the constructor of the parent class is executed first, and the function body of the constructor of the subclass is executed later

	return 0;
}


The running result of the whole program is that the function body of the parent class constructor is executed first, and the function body of the child class constructor is executed;

Access level

Three access rights

public: can be accessed by any entity
protected: only member functions of this class or subclass are allowed to access
private: only member functions of this class are allowed to access

Inheritance mode

As you can see from the example just now, you can inherit the parent class through public. Of course, you can also use these three methods

class Men : public Human
class Men : protected Human
class Men : private Human

Permissions available to subclasses:

Add some test codes to the example to test these permissions. The following are examples:
Note: the subclass inheritance method is public

//"Human.h"
public:
	int m_Age;
	char m_Name[100];

	void funcpub() {};

protected:
	int m_pro;
	void funcpro() {};

private:
	int m_priv;
	void funcpriv() {};
int main()
{
	Men men;		
	men.m_Age;  //Allow, parent class, public, any entity access
	men.m_pro;  //No, protected only allows member functions of this class or subclass to access\
				   Here is main function,Obviously not;  Can again Men Access in member functions in
	men.m_priv;  //Not allowed. The access permission is not enough. Only the member function * of the parent class is allowed to access
	
	return 0;
}

If the subclass inheritance method is protected, the parent class public will also become protected (subclasses can only be accessed in member functions if they want to access) (see table for details)

Function masking problem

Parent class function

//This is the parent class Human
public:
	void sameFunc();
	void sameFunc(int);

Subclass function

//Here is the subclass men
public:
	void sameFunc(int);

main

//Call test
	Men men;//Instance a subclass
	men.sameFunc(123);  //The sameffunc (int) function of the subclass is called instead of the function of the parent class
	men.sameFunc();     //An error is reported. The function with the same name without parameters in the parent class cannot be called

It can be seen from the example that when there is a member function with the same name in the subclass and the parent class, the function with the same name in the parent class cannot be called through the subclass, because the function in the subclass masks the function with the same name in the parent class
The solution to this problem is:
(1) call in the subclass member function, but note that in the subclass member function, call is added by adding the name of the parent class: the member function name (...).
Example: in subclass construction

Man::Man()
{
	Human::sameFunc();     //Can call
	Human::sameFunc(123);  //With parameters can also be called
	//Both functions successfully call the function with the same name of the parent class. Human:: is the key
}

(2) If Men inherits from the Human parent class in public, in the main function, the member function with the same name of the parent class can also be called in the way of "subclass object name. Parent class name:: member function name (...)"

	Men men;//Instance a subclass
	men.Human::sameFunc(123);   //The member function with the same name as the parent class parameter is called here
	men.Human::sameFunc();  	//Without parameters, it is also a parent class

(3) You can declare through using

public:
 using Human::samenamefunc; //Only function name can be specified, without formal parameter list, and the parent class must be public or\
 								protected(Can only be used in subclass member functions)

Of course, even if using is declared, in the calling process, we call children with the same name function, or call the subclass first, and the parent class will still be overwritten.
Do you want to continue calling the parent class, or add Human::sameFunc() parent class name:: function with the same name (...)

summary
Although a subclass can call a function with the same name as the parent class, it may have little practical significance. If the subclass overrides the parent class, it generally means that the subclass object does not want to call the parent class, otherwise it can be written with a different name

Tags: C++

Posted on Tue, 23 Nov 2021 07:11:28 -0500 by kinadian