Quick notes on C + + object-oriented programming
2022-2-3
C + + object-oriented programming B station course
Cherno's course link
Part 1:
- class
- object
- Member data
- Member function (set,
class student: { public: int age; string name; bool set(int a); bool set(string a); };
Object oriented code has at least three parts:
- Class definition
- Definition of member function
- Create object + call method
Part 2:
- Ordinary member function
- Constructor, automatically initializing objects
- Constructor with parameters, dynamically initializing objects
- Destructor, destroy object
- Constant member function, read-only, not write
- A static member function that depends on a class but not on an object
Part 3:
3.1 public and private
- The public part can be used freely. Private functions and attributes need to be called through the public shell inside the class
//Writing method of formal program class definition: class student: { public: bool set(int a); bool set(string a); bool read() const; student(); student(int a,string b); private: int age; string name; };
- Generally, "methods" are placed in public
- Properties are placed in private
- Purpose: to prevent the outside of the class from tampering with data
- When modifying data through functions, there is generally an error prevention mechanism, which can be modified only when the conditions are met. (encapsulation)
3.2 derivation and inheritance of class
Subclasses can obtain the members and methods of the parent class, and can also add their own members and methods.
//Students are divided into undergraduate and graduate student s class undergraduate : public student { public: string course;//This attribute is not in the student class, such as the CET4 score of undergraduates } class psotgraduate : public student { public: string research;//This attribute is not in the student class, such as the scientific research achievements of graduate students }
3.3 inheritance of classes in different situations
public,private,protect
- Replace the private defined by the parent student class with protect
- There is no change to the student itself
- At the same time, the protect part of student can be integrated into the subclass of student
3.4 execute different contents with the same function name
- Method is specific to a class. Less precise example: the same is "+", but in C is the addition of ASCI I codes, but in python is the splicing of strings.
- There are three ways to implement the same function name and execute different contents in C + +
- heavy load
- hide
- A function study() is defined in both the parent class student and the child class postgenerate, which hides the study() in the parent class
- Qiao Ji: rebellious son doesn't listen to his father
- Override
- Add virtual when defining a class
3.5 polymorphism
- Add virtual to all function declarations in the parent and child classes, and define the parent class pointer
- As the pointer points to different objects, the program will automatically find methods with the same name in different classes
3.5.1 review: structure pointer
Class pointers are similar to struct pointers when inheritance is not considered
- Writing method 1:
student *p;//Create a new student class pointer student aa;//New aa object p=&aa;//The p pointer points to the aa object p->name;//Equivalent to AA name p->study();//Equivalent to aa Study(), execute member function on aa
- Writing method 2: more advanced writing method is used in the actual program:
student *p=new student(20,'Zhang San'); delete p;//Call destructor
3.5.2 it will become more complicated after considering inheritance:
- A parent class pointer can point to a child class object, but a child class pointer cannot point to a parent class object
- Although the parent pointer points to the subclass object, you cannot call the proprietary properties and methods in the subclass object, but you can only call the methods in the parent class.
3.5.3 true polymorphic and virtual functions
- Create a method in student, undergraduate and postgraduate, which is called study
The learning content of each study is different - Add the keyword virtual before the parent class study declaration of all subclasses, indicating that this is a virtual function
- Constructors and other attributes are omitted for emphasis
class student //Student class definition { public: virtual void study();//Declare the study method, preceded by virtual, which is a virtual function } void student :: study();//Note that there is no virtual here {cout<<"study hard";} //The following is the definition of graduate class class postgraduate : public student { public://Public members of graduate students virtual void study();//Declare the study method, preceded by virtual, which is a virtual function } void postgraduate :: study();/Notice there's nothing here virtual { cout<<'chip design ';//Graduate learning chip design } //The following is the definition of undergraduate class class undergraduate : public student { public://Public members of Undergraduates virtual void study();//Declare the study method, preceded by virtual, which is a virtual function } void undergraduate :: study(); { cout<<"CET-4"; }) //Virtual functions with the same name are set in the three classes, and there are no parameters (distinguished from overloads, which depend on different parameters). The following is the main function
//Lower principal function student aa; postgraduate bb; undergraduate cc; student *p;//Define parent class pointer p=&aa;//The parent class pointer points to the student object p->study();//The parent class pointer calls the method of the student object and prints out "study hard" p=&bb;//The parent class pointer points to the child class postgenerate object p->study();//The parent class pointer calls the method of the subclass postgenerate to print out the "chip design" p=&cc;//The parent class pointer points to the subclass undergenerate object p->study()//The parent class pointer calls the method of the subclass undergenerate to print out "CET-4"
- The core of C + + polymorphism is to set a parent class pointer at the beginning of the program, and then the pointer can dynamically point to different classes, and the pointer can also dynamically call the methods of different classes, so as to realize that different data classes use the same method.
- Overloading is determined at compile time. During compilation, it determines which study to use
- Polymorphism is determined by the runtime. The compiler forms three kinds of studies into a virtual function table, and the compiler puts three different studies in memory.
3.6 pure virtual functions and abstract classes
3.6.1 definition of abstract class
1 students are a class, with subcategories: graduate and undergraduate
2 if a student objects to Zhang San, Zhang San must be one of graduate students, undergraduates or primary and secondary school students
The object Zhang San, who simply belongs to the student class and does not belong to any subclass, does not exist in real life
4 student classes do not have their own objects, so student classes are called abstract classes
3.6.2 set the abstract class to rely on pure virtual functions, in the following form:
class student { public: student();//Abstract classes can also use constructors student(int a,string b);//Overloading Constructors virtual void study()=0;//Declare the study method, indicating that this is a pure virtual function } //The following is the main function student aa;//An error is reported here. It is not allowed to create abstract class objects student *p;//But you can create abstract class pointers postgraduate bb;//Create subclass object p=&bb;//The parent class pointer points to the child class p->study();//Call the study method of subclass through polymorphism /*After using the abstract class, the parent class student can still use basic members such as age and name, but the parent class does not have its own study method - "study hard". */
When virtual void study()=0 is set; After:
- student is an abstract class
- study() function, pure virtual function, has no definition but only declaration. Its specific content depends on the polymorphism of subclasses
- You can still create a student pointer, but you can't create a student object (see below)
Write it at the back
- Inheritance, encapsulation and polymorphism are the three magic weapons of object-oriented, and they are also common to all programming languages
- The polymorphism of C + + depends on the implementation of pointers, while emerging languages such as java, python, system and SV seem to cancel pointers and liberate programmers. In fact, they hide pointers.
- For example, there is no need to define data types in python. Directly a=10, a = 'aaa', you can assign different types of values to A. in fact, you are assigning pointers to make the pointers point to different areas.