C + + learning notes

Keywords: C++ OOP

This blog is derived from C + + language learning. Constructors are widely used. Constructors generally need 1. The constructor name is the same as the class name; 2. There is no return value type declaration before the constructor name. 3. Constructor cannot return a value through return. 4. Typically, constructors have a public attribute. Therefore, the main learning modules of the blog are divided into:

1. Default constructor provided by the system

Generally, we do not overload the constructor, so we must use the default constructor, which will not initialize variables. Under the title, take the Car class as the simulation

#include<iostream>
using namespace std;

/*Default constructor provided by the system*/

class Car{
public:
    void disp_mems_value();//Display variable information
private:
    int m_nWheels;//Wheels of a car
    int m_nSeats;//Car seat
    int m_nLength;//Body length

};

void Car::disp_mems_value()
{
    cout << "Wheels =" << m_nWheels << endl;
    cout << "Seats = " << m_nSeats << endl;
    cout << "Length = " << m_nLength << endl;
}

int main()
{
    Car mycar;
    mycar.disp_mems_value();//Display vehicle parameters
    system("pause");//Screen pause
    return 0;

}

There is no constructor defined in the code. It is constructed in the default way. As a result, the running effect will find that the value is not fixed. If we want to initialize when creating a class, we need to customize the parameterless constructor.

2. Custom parameterless constructor

The custom parameterless constructor is an upgraded version of the default constructor, which is to determine the default value. Therefore, the following code will take Car as an example to customize the constructor again

#include<iostream>
using namespace std;


class Car{
public:
    void disp_mems_value();
    Car(){//Custom parameterless construction.
        cout << "Car constructor!" << endl;
        m_nWheels = 4;
        m_nSeats = 2;
        m_nLength = 2;
    }
private:
    int m_nWheels;
    int m_nSeats;
    int m_nLength;

};

void Car::disp_mems_value()
{
    cout << "Wheels =" << m_nWheels << endl;
    cout << "Seats = " << m_nSeats << endl;
    cout << "Length = " << m_nLength << endl;
}

int main()
{
    Car mycar;
    mycar.disp_mems_value();
    system("pause");
    return 0;

}


The only difference between this code and the default constructor is that it initializes the variables in private; The custom parameterless construction is called

3. Custom parameter constructor

When we need to initialize parameters and want to formulate them ourselves, we need to customize the parameter constructor, which is the most common operation, and C + + whistles here because it has several forms. Here we list them. Finally, take Car as an example to simulate the custom parameter constructor again
Form 1

Class name:Constructor name(parameter list):Data member 1:(Parameter 1),Data member 2(Parameter 2),...,Data member n(parameter n){
Constructor body
}

Class name:Constructor name(Type: Eucalyptus 1=Default value,Type parameter 2=Default value)
#include<iostream>
using namespace std;


class Car{
public:
    void disp_mems_value();
    Car(int con_Seats,int con_Wheels,int con_Length=6):m_nWheels(con_Wheels),m_nSeats(con_Seats),m_nLength(con_Length){
        cout << "Car constructor!" << endl;
    }
private:
    int m_nWheels;
    int m_nSeats;
    int m_nLength;

};

void Car::disp_mems_value()
{
    cout << "Wheels =" << m_nWheels << endl;
    cout << "Seats = " << m_nSeats << endl;
    cout << "Length = " << m_nLength << endl;
}

int main()
{
    Car mycar(2,2);//One of the parameters is the default parameter
    mycar.disp_mems_value();
    system("pause");
    return 0;



The function name here is more C + + style. If you don't deliberately learn C + +, you can't understand it at all, but if you see these forms, you will deeply understand and deepen. This code mainly realizes that the main function passes in a two parameters to initialize the user-defined parameter structure

4. Constructor of the class that contains the members of the object

Data members in a class can be objects of other classes. Such members are called child objects or member objects of the class. The following code uses the student class and date class to simulate the constructor of the class containing object members. Understanding that a student has a birthday is the core of the whole code

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

class Date{//Define date class
public:
    Date(int y,int m,int d);//Declare a constructor with parameters
    void show();
private:
    int m_nYear,m_nMonth,m_nDay;


};
Date::Date(int y,int m,int d)//Defines the constructor for the Date class
{
    cout << "Date constructor!" << endl;
    m_nYear = y;
    m_nMonth = m;
    m_nDay= d;
}

void Date::show(){//Define the member function show() to display the date
    cout << m_nYear << "-" << m_nMonth << "-" << m_nDay;

}

class Student {//Define student classes
public:
//Declare a constructor with parameters, con_name is the student's name, con_id is student id, y, m and D are date information
    Student(char *con_name,int con_id,int y,int m,int d);
    void disp_msg();
private:
    Date m_iBirthday; //Data member of type Date·
    char m_gName[20];
    int m_nID;
};
//Define the Student class constructor with parameters. The parameters y, m and D are used for the date class object m_iBirthday initialization
Student::Student(char *con_name,int con_id,int y,int m,int d): m_iBirthday(y,m,d) {
    cout << "Student constructor!" << endl;
    strcpy_s(m_gName,strlen(con_name)+1,con_name);
    m_nID = con_id;
}
//Define member function disp_msg() displays student information
void Student::disp_msg() {
    cout << "std name:" << m_gName << ",id = " << m_nID << ",birthday:";
    m_iBirthday.show();
}

int main()
{
    Student student("xiaoming",1,1998,10,25);//Call the constructor with parameters to define the class object
    student.disp_msg();
    return 0;


The code looks a little long. When defining the class object in the main function, five parameters are provided. The first three birth dates. From the running results, first call the constructor of m-iBirthday, and then call the constructor of Student class

5. Summary & Readers' harvest

After the above learning, readers can not only harvest the four specific different construction parameter methods of constructors, but also run with the code and observe the internal details of the code. There are some grammar, only when you knock it will you feel it.

Posted by kovudalion on Sat, 20 Nov 2021 09:50:45 -0800