The first chapter is an overview of object-oriented programming

Object-oriented language:


class: watches and clocks. (Essential characteristic: It all depends on time)


Object (instance of a class): A particular piece of clock. (Colours, shapes, etc may be different)


class: The main basis of classification: abstraction. For example, stones, cars, trees, and so on are all abstracted concepts from people’s long-term production and life.


Relationships between classes and objectsAn example of an object belonging to a class is called that class, as is the relationship between a mold and a casting.


encapsulation: Keep internal details as simple as possible. It forms a boundary with only a limited number of interfaces so that it can react with the outside. For example, sort is a wrapped function that sorts an array by typing the address.


inheritanceA special class has all the attributes and services of a general class. We call this the inheritance of a special class from a general class. Steamships are general, passenger ships are special. (A is a general class. If B has all the attributes of A, and on top of that, b has special attributes, then B is special.)


Source program: Untranslated code. (Machine cannot recognize)


The target program: source program A translated program that can be executed directly.


translator:


The development process of the program(C++ as an example)


Chapter two is the basis of object-oriented programming


Cin and COUT basic inputs and outputs: getline(cin, STR) : Enter a string with Spaces.


Definition of a constant variable: const variable type variable name =x (defined as a constant)


Quotation and frequently-quoted(Repeat the name of a block of address)

    int n = 10;
    int &a = n;       / / reference
    const int &b = n; / / often quoted
    cout << a << ' ' << b << endl;
Copy the code

Inline function: The system directly replaces the place where the inline function is called with an equivalent statement, reducing the save and recovery time of the running environment. (A function name preceded by inline is an inline function, but inline functions are only suitable for functions with small functions, such as a calculation formula.)


A function with default values: specifies the parameter value in advance. If no argument value is given, the parameter value is used.




Function overloading: Multiple functions with different parameter numbers or parameter types The function names are the same


Dynamic memory allocation(Note: return the first address of the allocated space.)

  • Memory allocation for individual elements:

   int *p, *q;
   p = new int(3);// Initialize the space with a value of 3
   q = new int;
   delete[] p;
   delete[] q;
Copy the code
    int *p, *q;
    p = new int[10]; // Allocate 10 elements of type int and assign the first address of the memory to p
    q = new int;
    delete[] p;
    delete[] q;
Copy the code

Dynamic memory release: delete

    int *p, *q;
    p = new int[10];
    q = new int;
    delete[] p; // Dynamic memory release for multiple elements
    delete q;   // Dynamic memory release for a single element
Copy the code

Chapter 3 Classes and Objects (I)

C++ also uses the concept of classes and objects for object programming, and simulates the state and behavior of objects through the following methods:

  • 1. The status of an object is described by its attribute data.
  • 2. Behavior of objects: Define a set of functions, and call corresponding functions to indicate that the object completes a behavior.

class: consists of member variables (data members) and member functions

Object definition: Class name The name of the object, such as int A and double a.

Object access:

Code examples:

	class student
	{
	public:
	    int score;
	    string name;
	    void change_score(int a)
	    {
	        score = a;
	    }
	    void change_name(string a)
	    { name = a; }};int main(a)
	{
	    student a;
	    a.change_score(100);
	    a.change_name("hesor");
	    cout << a.name << ' ' << a.score << endl;
	    return 0;
	}
/*
Output:
hesor 100
*/
Copy the code

The constructor: primarily used to initialize the data members of an object. Constructors can be overloaded. The constructor has no function return type and the function name is the same as the class name.

Code sample:

class student
{
public:
    int score;
    string name;
    student()
    {
        score = 10;
        name = "orz";
    }
    student(int s, string n)
    {
        score = s;
        name = n;
    }

    void change_score(int a)
    {
        score = a;
    }
    void change_name(string a)
    { name = a; }};int main(a)
{
    // Call the no-argument constructor note: not student a()
    student a;
    student b(100."hesor");
    cout << a.name << ' ' << a.score << endl;
    cout << b.name << ' ' << b.score << endl;
    return 0;
}
/*
Output:
orz 10
hesor 100
*/
Copy the code

Class access control:


The destructorDestructors are similar to constructors, except that constructors are called automatically when objects are created and destructors are called automatically when objects are destroyed. But destructors need to be preceded by ~, and they take no arguments (so they cannot be overloaded).


Copy constructor: a special constructor that uses an existing object to initialize a new object being created.


Class inheritanceClass A inherits class B. B is called the base class, and A is called the derived class (also called the parent and child classes).

class student : public person
{
};
Copy the code

The above is the public inheritance of the Persoon class as the base class and the Student class as the derived class, which automatically inherits all the attributes and methods in the Person class (you can add attributes and methods to the derived class as needed).


Derived class constructor:

class person
{
public:
    string name;
    int high;
    person(string a, inth) { name = a; high = h; }};class student : public person
{
public:
    int score;
    // student(int a) //1
    // If the constructor of a derived class is 1, the base class defaults to using the no-argument constructor to initialize the members inherited from the base class
    student(int a) : person("hesor".170) // Derived class constructor{ score = a; }};main()
{
    student a(100);
    cout << a.name << ' ' << a.score << ' ' << a.high << endl;
    return 0;
}
/*
Output:
hesor 100 170
*/
Copy the code

Derived class constructorThe derived destructor is called when the base destructor is called.

class person
{
public:
    string name;
    int high;
    person(string a, int h)
    {
        name = a;
        high = h;
    }
    ~person()
    {
        cout << "Base class end... ..."<< endl; }};class student : public person
{
public:
    int score;
    // student(int a) //1
    // If the 1 constructor is used, the base class initializes its member variables using the no-argument constructor by default
    student(int a) : person("hesor".170) // Derived class constructor
    {
        score = a;
    }
    ~student()
    {
        cout << "Derived class end... ..."<< endl; }};main()
{
    student a(100);
    cout << a.name << ' ' << a.score << ' ' << a.high << endl;
    return 0;
}
/* Output: hesor 100 170 derived class end... . Base class for the end... . * /
Copy the code

Function redefinition: Derived class redefines functions inherited from the base class (the return value, parameter number, and parameter type of the function must be the same).

class person
{
public:
    void f(a)
    {
        cout << "Base class"<< endl; }};class student : public person
{
public:
    void f(a)
    {
        cout << "Derived class"<< endl; }};main()
{
    student a;
    person b;
    a.f(a); b.f(a);return 0;
}
/* Output: derived base class */
Copy the code

Definition of derived classes under multiple inheritance:

class student
{
public:
    void f_student(a)
    {
        cout << "I'm a student"<< endl; }};class teacher
{
public:
    void f_teacher(a)
    {
        cout << "I'm a teacher."<< endl; }};class student_teacher : public student, public teacher
{
};
main()
{
    student_teacher a;
    a.f_student(a); a.f_teacher(a);return 0;
}
/* Output: I am a student
Copy the code

Note that in inheritance, the constructors of the base class are always called before the constructors of the derived class, and in multiple inheritance, the constructors are called in the order in which the base class is inherited. Destructors are called in exactly the opposite order to constructors.


Ambiguity in multiple inheritance: Because multiple inheritance has multiple base classes, the attribute names and method names of the base classes may be repeated, so ambiguity can occur in derived classes. For example, in the following figure, if A has A member variable name, BC will have A variable name after inheriting from A, and then D will have two name variables, which will generate ambiguity when called.

The solution:

class person
{
public:
    int NUM = 123456;
};
class student : public person
{
};
class teacher : public person
{
};
class student_teacher : public student, public teacher
{
};
main()
{
    student_teacher a;
    cout << a.teacher::NUM;
    return 0;
}
/*
Output:
123456
*/
Copy the code
  • Virtual inheritance: Add the virtual keyword to define class A in the figure as A virtual base class. The members of the virtual base class will be inherited only once in the class inheritance relationship.

class person
{
public:
    int NUM = 123456;
};
class student : virtual public person
{
};
class teacher : virtual public person
{
};
class student_teacher : public student, public teacher
{
};
main()
{
    student_teacher a;
    cout << a.NUM;
    return 0;
}
/*
Output:
123456
*/
Copy the code

Update ing… …