43. Concept and Significance of Inheritance

Keywords: less

1, thinking

Is there a direct association between classes?

2. Combinatorial Relations: The Relation between Whole and Part

#include <iostream>
#include <string>
using namespace std;

class Memory
{
public:
	Memory()
	{
		cout << "Memory()" << endl;
	}
	~Memory()
	{
		cout << "~Memory()" << endl;
	}
};
class CPU
{
public:
	CPU()
	{
		cout << "CPU()" << endl;
	}
	~CPU()
	{
		cout << "~CPU()" << endl;
	}
};
class Disk
{
public:
	Disk()
	{
		cout << "Disk()" << endl;
	}
	~Disk()
	{
		cout << "~Disk()" << endl;
	}
};
class Mainboard
{
public:
	Mainboard()
	{
		cout << "Mainboard()" << endl;
	}
	~Mainboard()
	{
		cout << "~Mainboard()" << endl;
	}
};
class Computer
{
	Memory mMemory;
	CPU mCPU;
	Disk mDisk;
	Mainboard mMainboard;
public:
	Computer()
	{
		cout << "Computer()" << endl;
	}
	void power()
	{
		cout << "power()" << endl;
	}
	void reset()
	{
		cout << "reset()" << endl;
	}
	~Computer()
	{
		cout << "~Computer()" << endl;
	}
};
int main()
{
	Computer c;
	return 0;
}


This procedure represents the concept of combinatorial relationship, which means coexistence of life and death.

The characteristics of combinatorial relationships:
1. Use other class objects as members of the current class
2. The life cycle of the object and member objects of the current class is the same
3. The usage of member objects is exactly the same as that of ordinary objects.

3. Inheritance: father-son relationship

  • Inheritance in object-oriented refers to the parent-child relationship between classes
    - Subclasses have all the attributes and behaviors of the parent class
    The child class is a special parent class.
    - Subclass objects can be used as parent objects
    - Subclasses can add methods and attributes that the parent class does not have

  • C++ describes inheritance relationships in the following way: colons denote inheritance relationships

class Parent
{
	int mv;
public:
	void method()
	{
	}
};
class Child:public Parent		//Describing inheritance relationships
{
};
#include <iostream>
#include <string>
using namespace std;

class Parent
{
	int mv;
public:
	Parent()
	{
		cout << "Parent()" << endl;
		mv = 0;
	}
	void method()
	{
		cout << "mv = " << mv << endl;
	}
};
class Child : public Parent
{
	int i;
public:
	Child()
	{
		cout << "Child()" << endl;
		i = 1;
	}
	void hello()
	{
		cout << "It's a Child Class" << endl;
	}
};
int main()
{
	Child c;
	c.hello();
	c.method();
	return 0;
}


Program analysis: When we create a subclass object, it calls the constructor of the parent class first, and then the constructor of the subclass. And subclass objects can call all member functions in the parent class. (Inheritance has the function of code reuse)

  • Important rules:
    The child class is a special parent class.
    - Subclass objects can initialize parent objects directly
    - Subclass objects can be assigned directly to parent objects
#include <iostream>
#include <string>
using namespace std;

class Parent
{
	int mv;
public:
	Parent()
	{
		cout << "Parent()" << endl;
		mv = 0;
	}
	void method()
	{
		cout << "mv = " << mv << endl;
	}
};
class Child : public Parent
{
	int i;
public:
	Child()
	{
		cout << "Child()" << endl;
		i = 1;
	}
	void hello()
	{
		cout << "It's a Child Class" << endl;
	}
};
int main()
{
	Child c;
	Parent p1 = c;
	Parent p2;
	c.hello();
	c.method();
	p2 = c;
	return 0;
}


Line 36 is to initialize the parent object p1 with the subclass object c. The subclass object is a special parent object, so it can be initialized and assigned. I began to think that I had called a constructor less than once, and then I found out that one of them was calling a copy constructor, so _________.

  • The Meaning of Inheritance
    Inheritance is an important means of code reuse in C++. Through inheritance, all functions of the parent class can be obtained, and existing functions can be overwritten in subclasses, or new functions can be added.

Posted by kr4mer on Sat, 05 Oct 2019 17:42:03 -0700