directory

  1. Important knowledge of classes and objects
  2. Operator overloading
  3. inheritance
  4. polymorphism
  5. template

First, the important knowledge of class and object

1.1 Deep and Shallow Copy

Shallow copy: a simple copy assignment operation. Copy construction deep copy: a copy operation is performed by reapplying for space in the heap

1.2 this pointer

The this pointer points to the object that the called member function belongs to. The essence of a pointer is a pointer constant (that is, the point of a pointer cannot be changed, but the value of the point can be changed)

use

  1. When a parameter and a member variable have the same name, we can distinguish them by the this pointer
  2. Returns the object itself in a nonstatic member function of the classReturn * this returns a reference to the object, achieving chained programming

1.3 Const decorates a member function

Often function:

  1. Const; const; const; const; const; const; const
  2. Member attributes cannot be modified in ordinary functions
  3. Member attributes can be modified in constant functions by adding the keyword to their declarations

Often objects:

  1. Eg: Const Object obj;
  2. A constant object can only call a constant function.

practice

#include <iostream> using namespace std; class temp{ public: temp(){ this->k = 10; } // void setValue(int k) const{// this->k = k; //} void setValue(int k) const{// This ->k = k; } int getValue() const{ return this->k; } private: mutable int k; }; int main(void) { temp t; temp *p = &t; cout<< "t.getValue()="<<t.getValue()<<endl; / / "." Cout << "p->getValue()="<<p->getValue(); // "->" is the address operator used to refer to member variables and functions; return 0; }Copy the code

1.4 a friend

The goal is to have a function or a class access a private member friend in another class and use the keyword friend to represent three implementations of that friend

  1. Global functions are friends
  2. Do a friend
  3. Member functions are friends

practice

#include <iostream> using namespace std; class temp{ friend int main(void) ; Public: temp(){this->k = 10; } int getValue() const{ return this->k; } private: void setValue(int k) { this->k = k; } int k; }; int main(void) { temp t; cout<< "t.getValue()="<<t.getValue()<<endl; t.setValue(100); // Since setValue is a private function, if it is normally inaccessible outside the class, access the function by declaring it a friend in the class. return 0; }Copy the code

Operator overloading

Operator overloading is simply a “syntactic convenience”; it is just another way to call a function, essentially a function (member function or global function), and the syntactic sugar effect of operator overloading is simply omitted.

Operator overloading can be done in two ways:

  1. A member function
  2. Global function operator overloading can also occur function overloading cannot change the operators of built-in data types and can only be used for user-defined types. Do not misuse operators to overload common operators such as “+”, “<<“, “++”, “=” < >! = = =”

Add two objects by overloading the “+” operator.

#include <iostream>

using namespace std;

class temp{

public:
   temp(){
       this->k = 10;
       this->t = 100;
   }

    int k;
    int t;
};

temp operator+(temp param1,temp param2){

   temp a;
   a.k = param1.k+param2.k;
   a.t = param1.t+param2.t;
   return a;
}

int main(void) {
    
    temp t1;
    temp t2;

    cout<<(t1+t2).k<<endl;
    cout<<(t1+t2).t<<endl;

    return 0;
}
Copy the code

Three, inheritance,

Definition syntax for derived classes when inheritance is single

Class Derived class name: inherited base class name {member declaration; Class Derived class name: Inheritance type 1 Base class name 1, Inheritance type 2 Base class name 2,... {member declaration; }Copy the code

There are three types of inheritance: common inheritance; Private inheritance; Protect the inheritance

CPP allows multiple inheritance, but it is not recommended to use multiple inheritance in practical development, because the same name attributes or methods may occur in the parent class and need to be scoped. Diamond inheritance can be solved by virtual function virtual inheritance, that is, late binding, which we will see more about in the next section of polymorphism.

The private members of the parent class still occupy storage space in the child class, but they are not directly accessible.

Four, polymorphism

Polymorphism provides a layer of separation between the interface and the implementation, improving the organization and readability of code and facilitating maintenance and extension

Dynamic polymorphism satisfies the condition

  1. Have a hereditary relationship
  2. A subclass overrides the virtual function of its parent class

Dynamic polymorphism scenarios: Dynamic polymorphism occurs when a pointer or reference to a parent class points to a subclass object.

Address early binding, at compile time to determine the function address. Use the Visual virtual function keyword to tell the compiler that the function address cannot be bound in advance and needs to be bound at run time.

VFPTR: Virtual Function Pointer VfTable: Virtual function table records the address of the virtual function.

When a subclass overrides a virtual function of its parent class, the internal virtual table of the subclass is replaced with the virtual address of the subclass

Function call bundling

Associating a function body with a function call is called a binding. When a bundle is completed before the program runs (with a compiler and connector), it is called early biding. It is at runtime that the type of binding is determined to be late dbinding. For certain functions, CPP requires that the virtual keyword be used when the function is declared in the base class in order to cause late binding. To create a virtual member function, you can simply prefix the function declaration with the keyword virtual. The key value virtual is used only when the declaration is used, but not when it is defined. If a function is declared virtual in the base class, it is virtual in all derived classes.

Pure virtual functions vs. abstract classes At design time, compare the extended design time base class as only an interface to its derived class, that is, you do not want the user to create a base object. You can do this by adding at least one pure virtual function to the base class, making it abstract. Pure virtual functions use the keyword virtual and append the function with =0. Equivalent to the Java abstract prefix

Void f() = 0; void f() = 0;

  1. Abstract classes cannot instantiate objects that are Java consistent.
  2. A subclass of an abstract class must override the pure virtual functions of its parent class — same as Java

Constructors cannot be virtual, but destructors can and often must be virtual

Virtual and pure virtual destructors: This problem can be solved by virtual destructor, which does not call the child class’s destructor during the parent class pointer’s destructor. Pure virtual destructors are both declared and implemented.

practice

#include<stdio.h> #include<iostream> using namespace std; class BaseClass { public: BaseClass(){ cout<< " BaseClass construct"<<endl; } // Add the virtual keyword to tell the compiler that, with late binding, a VFPRT will be created after compilation, pointing to vfTABLE, Virtual void print(){cout << "BaseClass print"<<endl; } ~BaseClass(){ cout<< " BaseClass destruct"<<endl; } private: void innerMethod(){ cout<< " BaseClass inner"<<endl; }}; class BaseClass2 { public: BaseClass2(){ cout<< " BaseClass2 construct"<<endl; } virtual void print(){ cout << " BaseClass2 print"<<endl; } ~BaseClass2(){ cout<< " BaseClass2 destruct"<<endl; } private: void innerMethod(){ cout<< " BaseClass2 inner"<<endl; }}; class ChildClass : public BaseClass, private BaseClass2 { public: ChildClass(){ cout<< " ChildClass construct"<<endl; } void print() { BaseClass::print(); // BaseClass::innerMethod(); BaseClass2::print(); BaseClass2::print(); Cout << "ChildClass print"<<endl; } ~ChildClass(){ cout<< " ChildClass destruct"<<endl; }}; int main() { BaseClass tmp; ChildClass childClass; Cout <<"size of BaseClass"<<sizeof(TMP)<<endl; cout<< sizeof BaseClass <<sizeof(TMP)<<endl; cout<<"size of childClass"<<sizeof(childClass)<<endl; BaseClass *baseclass = &childClass; // Create a BaseClass class that points to childClass BaseClass ->print(); BaseClass print return 0; BaseClass print return 0; }Copy the code

Five, the template

Inheritance and composition provide a way to reuse object code, while CPP’s template features provide a way to reuse source code to create generic templates that greatly improve reuse.

It’s a bit like a generic method in a utility class, but there’s more to it than that. Generics provide more generality, and there are not only function templates, but also class templates.

Features:

  1. A template is just a framework and cannot be used directly
  2. A template is not a panacea

Templates are usually declared and defined in header files, which seems to violate the “header file should not put anything that allocates memory storage space”, but templates are defined in a special way, in template<… The code after > means that the compiler does not allocate storage at the time, but only when it is used.

5.1 Function Templates

Usage scenario: Generic programming

Syntax Template <typename T> Function declaration or definition template -- declaration creates a template. Typename --> indicates that the following symbol is a data type. It can also be class instead of T --Copy the code

Automatic type derivation using function templates – > must be derived to display the specified type

Function considerations Automatic type derivation, must derive a consistent data type T can be used. The template can be used only after the data type of the initial T is determined

The difference between ordinary functions and function templates whether automatic type conversion can occur (implicit type conversion)

General functions can, function template automatic derivation can not, display specified yes. It is recommended that the function template be invoked by displaying the specified type

Call rules for ordinary functions and function templates

  1. If both the function template and the normal function can be implemented, the normal function is called first
  2. You can force a function template to be called through an empty template argument list
  3. Function templates can also be overloaded
  4. If the function template provides a better match, it is better not to provide a normal function, otherwise it is prone to ambiguity.

practice

#include<stdio.h> #include<iostream> #include<string> using namespace std; template<typename T> int add(T a,T b){ cout <<"template T method"<<endl; return a + b; } template<typename T, typename S> int add(T a,S b){ cout <<"template S T method"<<endl; return a + b; } int add(int a,int b){ cout <<"normal method"<<endl; return a + b; } int main() {cout <<"add "<< add(1,1)<< endl; Cout <<"add "<< add<>(1,1)<< endl; Cout <<"add "<< add(1.0,2.0)<< endl; Cout <<"add "<< add(1.0,3)<< endl; / / template support overloading cout < < < < "add" to add < int, int > (1.0, 3) < < endl; // cout <<"add "<< add("ab","ca")<< endl; // return 0; }Copy the code

5.2 class template

Template <class T> class template -- declares the creation template class --> indicates that the following symbol is a data type, which can also be typename T --. The name can be substituted, usually in uppercase lettersCopy the code

Difference between a class template and a function template

  1. Class templates do not have automatic type inference and can only use display to specify the type
  2. Class templates can have default parameters in the template parameter list

When a member function is created in a class template

  1. Member functions in ordinary classes can be created from the start
  2. Member functions in a class template can only be created when called

Class template objects take function arguments in three ways

  1. Specify the type passed in – displays the data type of the object directly
  2. Parameter template – The parameters of an object are passed as templates
  3. Whole class template – Pass this object type template

practice

#include <iostream> using namespace std; Template <class T> class templateclass {public: templateclass(T k = 10) {this->k = k; cout<< "templateclass construt k = "<<this->k <<endl; } void setValue(T k){ this->k = k; } T getVaule() const { return k; } private: T k; }; Void print(templateclass<int> &params) {cout << params.getvaule () << endl; } int main(void) { // templateclass<> p(100); Templateclass <int> k(100); cout << k.getVaule() << endl; Keith etValue (1000.5); // k.setValue(" a "); // k.setValue(" a "); / / error: print (k); return 0; }Copy the code

Six, data

The Tinking in c + + [dark horse programmers originality work | c + + tutorial from 0 to 1 introduction to programming (www.bilibili.com/video/BV1et)…

Seven, harvest

The study reviewed the important knowledge points of CPP,

  1. Deep copy and shallow copy,
  2. The definition and use of the this pointer
  3. Const modifies the role of a member function
  4. Meaning of friend functions
  5. Operator overload and meaning
  6. Inheritance virtual base class multiple inheritance, inheritance of three ways to use
  7. The definition of polymorphism has been understood using the scene virtual function pure virtual function abstract class
  8. The use of template functions and template classes

This article study review of the knowledge point is still a lot, do not add practice solid foundation.

Thank you for reading

Next we learn to practice CPP STL container and algorithm, welcome to pay attention to the public account “audio and video development journey”, learn and grow together.

Welcome to communicate