Inheritance of C/C++ classes

1. Class inheritance
 

class Shape{
};
class Circle : public Shape{
};

grammar:

class B : public A{}


B inherits from A, class A is the parent class, and B is a derived class (subclass).
When B inherits from A, it automatically inherits all public members in the parent class, and cannot inherit private ones.

New modifier: protected
(1) This member cannot be accessed externally, such as private
(2) This member can be inherited by subclasses, such as public

Describe the relationship between the parent class and the subclass in memory: the memory of the parent class is in the front, and the
private member variables of the parent class will also appear in the memory, but the compiler restricts access

#include<stdio.h>
#include<stdlib.h>
#include<string.h> 


class Shape{
	public:
		char name[16];
		char size[16];
	public:
		void Show()
		{
			printf("%s  ,%s  \n",name,size);
		}
	protected:
	    int abc = 10; 
};

class Circle : public Shape
{
	//Circle自己的一些函数。
	public:
	    void Play()
		{
			printf("abc = %d\n",abc);
		 } 
};


int main()
{
	Circle c;
	strcpy(c.name,"xigua");
	strcpy(c.size,"big");
	c.Show();
	c.Play();
	
	return 0; 
}

2. Virtual inheritance

1. Function rewriting


class Parent{
	public:
		void test()
		{
			printf("Parent.....\n"); 
		}
	
}; 
class Child:public Parent
{
	public:
		void test()
		{
			Parent::test();
			printf("Child.....\n"); 
		}
	
};

3. The pointer of the parent class can point to the object of the subclass

class Parent{
    public:
        int a ;     
}; 
class Child:public Parent
{
    public:
        int b;
    
};  
int main()
{
    Child c;
    c.a = 10;
    c.b = 20;    
    Parent* p = &c;
    printf("%d \n",p->a);
    
    return 0;    
}

4. The introduction of the question:
Parent* p = new Child();
p->test();
Whose test does it point to?

class Parent{
	public:
		void test()
		{
			printf("Parent.....\n"); 
		}
	
}; 
class Child:public Parent
{
	public:
		void test()
		{
			printf("Child.....\n"); 
		}
	
};

int main()
{
	Child ch;
	Parent* p = &ch;
	p->test();
	
	return 0;
}

When a member function is to be overridden by a subclass, the parent class should declare it as virtual.
If the parent class function is defined as vitural, it will call the subclass's

class Parent{
	public:
		virtual void test()
		{
			printf("Parent.....\n"); 
		}
	
}; 
class Child:public Parent
{
	public:
		void test()
		{
			printf("Child.....\n"); 
		}
	
};

 

3. Let’s talk about construction and destruction
When creating a subclass object, when
constructing, first call the constructor of the parent class, and then call the constructor of the subclass
When destructing, call the destructor of the subclass first, and then call the parent class constructor of

If the parent class has multiple constructors, one of them can be called explicitly.
If there is no explicit call, the default constructor is called by default.

what is a constructor

Vision: The design and manufacture of anything has the vision and expectation before it was born. The good expectation of the constructor is to initialize the object at the beginning of the object's creation. During initialization, there are function calls and some functions need to be completed. .

Procedural things are precise and standardized things, so their names often have precise meanings. Most of the meaning of naming is for a simple nickname. The invention of great and meaningful things lies in the carrying There are many meanings. The same name is just a code name that can identify you from a certain group. You can also incorporate good expectations for life into the name when naming it, just like many parents name their children. With the expectation of the little life, it also brings the parents' perception and blessing of life as a human being.

The name of the constructor is the same as the name of the defined class.

Since it is a category that belongs to the whole category, each of us is born different, and each individual has its own unique characteristics, some are beautiful to suffocate, some are so deep that they are addictive, and some So tender it hurts. Countless differences form an endless universe like the vast stars. We are not different because of a little difference, but every little bit of each of our little worlds is different.

The constructor is subordinate to the function, but it is a function that does not return a value.

one and infinity

Many things carry countless reveries and guesses from the beginning of their birth, but smart humans can construct a lot in their brains, whether it is infinite or 1, they can be used. When facing the vast sea of ​​stars, People's hearts are full of shock and grandeur. When faced with daily necessities, rice, oil and salt, they can also cook realistically.

People are different because of the oneness and infinity of the number. The lone star is different from the star sea. The countless fetters between the star sea itself will have a lot more beautiful reverie than the lone star. Pegasus meteor, August lion, time and space travel, and star sea .

There can be multiple constructors with empty parameters.

Rationality between feed and table of contents

The arrangement of affairs in the world itself is to perform their own duties, and each is free and unrestrained in his own small world. If there is ambition, people themselves can also work from the bottom up. There are too many trivial things in the lives of little human beings, so the catalog summary is to remind us to return to the standard, and another is the systematicness of quantitative changes leading to qualitative changes.

All classes have constructors:

If no constructor is defined, the C++ compiler will generate a default constructor with no parameters and an empty function body.

When defined, the no-argument constructor is automatically called

When a class is inherited, the destructor should be considered virtual
:
Parent * p = &ch;
delete p;
may cause the program to crash

class Parent{
	public:
		Parent()
		{
			printf("Parent: 创建\n");
			a = b = 0;
			
		}
		Parent(int x,int y)
		{
			printf("Parent: 创建  参数:%d, %d\n",x,y);
			a = x;
			b = y;
		}
		~Parent()
		{
			printf("Parent: 销毁\n");
		}
	private:
		int a,b;
}; 
class Child:public Parent
{
	public:
		Child():Parent(1,2)//显式的调用 
		{
			printf("Child: 创建\n");
			
		}
		~Child()
		{
			printf("Child: 销毁\n");
		}
}; 

int main()
{
	Parent* p= new Child();
	
	delete p;
	
	return 0;
}

**//改成virtual ~Parent** 

Four. Multiple inheritance
Father, Mother represents two classes

class Child:public Father, public Mother{
};

The problem of multiple inheritance: When multiple parent classes have the same variable, an error will occur, so there are fewer places to use it, and the pure virtual function will be involved later.

Guess you like

Origin blog.csdn.net/u013590327/article/details/123189363