C++ Learning Notes

Object-oriented

Object-oriented is the combination of data and manipulation of data as an interdependent whole, that is, objects. To abstract the commonalities of similar objects, that is, classes, most of the data in a class, can only be processed by the methods of this class. Classes interact with the outside world through some simple external interfaces, and objects communicate with each other through messages.

//int a;
//Class object

 

basic feature

The object-oriented approach first layers the needs rationally, then builds relatively independent business modules, and finally integrates each module to achieve a high cohesion and low coupling effect to meet customer requirements.

Specifically, it has three basic features: encapsulation, inheritance, and polymorphism.

(1) Encapsulation refers to the abstraction of objects into classes, each of which protects its own data and methods. Classes can hide untrusted processes by allowing only trusted classes or objects to operate on their own data and methods.

(2) Inheritance can use all the functions of an existing class without rewriting the original class for code reuse and polymorphism support. It generally takes three forms: implementation inheritance, visual inheritance, and interface inheritance. Implementing inheritance refers to the ability to use the properties and methods of a base class without additional coding. Visual inheritance refers to the use of the parent form's appearance and actual code by child forms. Interface inheritance uses only attributes and methods, lagging behind subclass implementations.

(3) Polymorphism refers to the fact that the same entity has multiple forms at the same time, which is mainly reflected in the inheritance system of classes. Simply put, it allows pointers of subclass types to be assigned to pointers of parent types, and then the parent object can operate in different ways according to the characteristics of the child objects currently assigned to it.

 

Inheritance and Derivation:

 

Inheritance and derivation are the same process from different perspectives:

The process of maintaining the properties of an existing class to form a new class is called inheritance

  The process of creating a new class by adding your own attributes to an existing class is called derivation

 

When a child class inherits a parent class, it can inherit the parent class in the public, private, and protected ways, each of which differs as follows:

1) public Inheritance

  • All public members in the base class are public attributes in the derived class;
  • All protected members in the base class are protected attributes in the derived class;
  • All private members in the base class are not accessible in derived classes.

2) protected Inheritance

  • All public members in the base class are protected attributes in the derived class;
  • All protected members in the base class are protected attributes in the derived class;
  • All private members in the base class are still inaccessible in the derived class.

3) private Inheritance

  • All public members in the base class are private s in the derived class.
  • All protected members in the base class are private s in the derived class.
  • All private members in the base class are inaccessible in the derived class.

 

Either way of inheritance, private members of the parent class are inaccessible, and only indirectly (through functions) can the values of private members be obtained through public members.

Derived class objects have access/modifiable privileges to members of the public in the base class only when public is derived, and the rest are inaccessible/unmodifiable.

 

#include <iostream>
#include <String>

using namespace std;

// base class
class student
{
public:
	string name;
protected:
	int age;
private:
	char sex;

public:
	void showStu()
	{
		cout << this->name << endl; // 1. Public data members that have access to the class in this class
		cout << this->age << endl;  // 1. Protected data members that have access to the class in this class
		cout << this->sex << endl;  // 1. Private data members that have access to the class in this class
	}
};

// Derived Class - public Inheritance
class public_Sub : public student
{
public:
	void show()
	{
		cout << this->name << endl; // 2. Public inheritance, access to public data members of base classes in derived classes
		cout << this->age << endl;  // 2. public inheritance, protected data members that have access to the base class in derived classes
		//Cout << this->sex << endl; // Error:2. Private data members of base classes cannot be accessed in class c
	}
};

// Derived Class - protected Inheritance
class protected_Sub : protected student
{
public:
	void show()
	{
		cout << this->name << endl; // 3. protected inheritance, which provides access to public data members of the base class in derived classes
		cout << this->age << endl;  // 3. Protected inheritance that provides access to protected data members of the base class in derived classes
		//Cout << this->sex << endl; // Error:3. Private data members of base classes cannot be accessed in derived classes
	}
};

// Derived Class - private Inheritance
class private_Sub : private student
{
public:
	void show()
	{
		cout << this->name << endl; // 4. private inheritance, which provides access to public data members of the base class in derived classes
		cout << this->age << endl;  // 4. Private inheritance, which provides access to protected data members of the base class in derived classes (even private inheritance)
		//Cout << this->sex << endl; // Error:4. Private data members of base classes cannot be accessed in derived classes
	}
};

int main()
{
	student stu;
	cout << stu.name << endl;   // 5. Public data members of classes that are accessible outside the class
	//Cout << stu.age << endl; // Error, 5. Protected data members of classes that are not accessible outside the class
	//Cout << stu.sex << endl; // Error, 5. Private data members of classes that are not accessible outside the class

	return 0;
}

 

 

Virtual function:

Pointers pointing to base classes, when operating on their polymorphic class objects, can call their corresponding functions based on the different class objects they point to, which are virtual functions.

Function of virtual functions: After virtual functions are defined in the base class, they can be redefined in derived classes, and they can be dynamically selected to call functions of the same name in the base class and in different derived classes during the run-time of the program through a base class pointer or reference. (If the virtual function is not redefined in the derived class, it inherits the virtual function of its base class.)

 

What is polymorphism?

Polymorphism means "one interface, many implementations". To summarize a sentence is:

The function in the base class is preceded by the virtual keyword, overridden in the derived class, and called at run time according to the actual type of object. If the object type is a derived class, the function of the derived class is called; If the object type is a base class, the function of the base class, runtime binding (dynamic binding), is called.

(Base class virtual function->Subclass override->Base class pointer to subclass object)

 

Principle?

The compiler maintains a virtual function table for each class containing virtual functions, and each object has a virtual pointer (first address saved), which points to a virtual function table, and there is a virtual table (vtable) between objects.

Virtual tables can be inherited. After a subclass inherits a base class virtual table, the virtual table is identical to the parent virtual table (with different addresses), except that the virtual pointer of the object points to the virtual table of the class.

 

The virtual function pointer points to their virtual tables, and the virtual table address points to each function. When the parent class is inherited, when the function is overridden, the virtual pointer of the subclass points to the same virtual table address as the parent class, and the derived class function is invoked at run time by updating the address based on the object.

Others that are not inherited still point to the same address as the parent virtual pointer. If the virtual keyword is not added, the parent function is still called.

 

#include <iostream>
using namespace std;
//int a;
//Class object

class tran {
public:
	virtual void fun() {
		cout << 3 << endl;
	}
	virtual void fun2() {
		cout << 33 << endl;
	}
	virtual void fun3() {
		cout << 333 << endl;
	}
	int x;
};
class plane : public tran {
public:
	void fun() {
		cout << 7 << endl;
	}
	int y;
};
void add(int a, int b) {
	cout << a + b << endl;
}
int main() {
	void(*pp)(int, int);
	pp= add;
	pp(1, 2);
	typedef void(*tp)(void);
	tran a; //3
	plane b; //7
	tran* p;
	cout << sizeof(a) << endl << sizeof(b) << endl;
	p = &a;
	p->fun();
	p = &b;
	p->fun();
	tp** p1 = (tp**)&a;
	cout << p1[0][0] << '\t' << p1[0][1] << '\t' << p1[0][2] << endl;
	p1 = (tp**)&b;
	cout << p1[0][0] << '\t' << p1[0][1] << '\t' << p1[0][2] << endl;

}
//Class object

 

 

What is the difference between struct and class in C++?

Specifically, in C++, there are only two differences between class and struct when defining a type:

(1) The default inheritance rights are different. class inheritance defaults to private inheritance, while struct inheritance defaults to public inheritance;

(2) class is also used to define template parameters, like typename, but the keyword struct is not used to define template parameters.

 

Posted on Thu, 02 Dec 2021 13:38:35 -0500 by kpulatsu