Preliminary exploration of C + +: qt application

C++

object-oriented

suffix .cpp
 compile: g++ xxx.cpp

**Encapsulation: * * the abstract process of encapsulation, which describes the characteristics of a class of things
**Class: * * a general term for a class of things
**Object: * * a special / individual of something

How to describe a class of things: data members, member methods.

Instantiated object: int i;
Example: A a1; "Instantiate an object of type A a1"

Inheritance: core, adding new features / functions to the original features / functions.
Polymorphism: Yanshen, the same command has different display effects on different objects.

1, Input and output

1.cin (input) – it is linked to the keyboard by default and accepts the data information entered from the keyboard.
Syntax: CIN > > variable;
2.cout – the default link to the screen to display data on the screen.
Syntax: cout < < variable;

If no namespace is specified: using namespace std;
Domain resolution operator

#include <iostream>
using namespace std;

int main()
{
    int i;
    char str[32];

    cout << "Hello World!" << endl;
    cin >> i;
    cout << "i = " << i << "    &i = " << &i << endl;  //C + + can automatically identify variable types
    cin >> str;
    cout << "str = " << str << endl;

    return 0;
}

Note: when creating a variable, C + + must give the variable an initial value, otherwise an error will be reported

2, Encapsulation

1.1 encapsulation definition - properties and behaviors

Meaning of encapsulation:

  • Take attributes and behaviors as a whole to express things in life
  • Control attributes and behaviors with permissions

Syntax: class name {access rights: attribute / behavior};

class Clock
{
public:
    void ShowTime(); 
    void SetTime(int h,int m,int s);
private:
    int hour;
    int minute;
    int second;
};

1.2 access rights

There are three types of access rights:
Public authority  public        ==Can be accessed inside the class, and can be accessed outside the class==
Protection Authority  protected     ==It can be accessed inside the class, but not outside the class==
Private rights  private       ==It can be accessed inside the class, but not outside the class==

Example:

#include <iostream>
me/xiaolei/teacher/example/c++/hello.cpp' '
using namespace std;

//The inline function inline 1 is implemented within the class 2. It is declared within the class with the inline keyword. The implementation can be written outside the class
//public protected private

class Clock
{
public:
    void ShowTime(); 
    void SetTime(int h,int m,int s);
private:
    int hour;
    int minute;
    int second;
};

void Clock::ShowTime()
{   cout << hour << ":" << minute << ":" << second << endl;  }

void Clock::SetTime(int h,int m,int s)  
{   hour = h; minute = m; second = s;   }

int main()
{   
    Clock c1;
    
    c1.ShowTime();
    
    return 0;
}

Inline inline function, macro with parameters, suggestive mechanism
Macros take up compilation time, not running time

To calculate the size of a class, you can only calculate the size of the data type, and the function does not include the space size

3, Special functions in classes

3.1 constructor

Object initialization and cleanup are also two very important security issues
An object or variable has no initial state, and the consequences of its use are unknown
Similarly, after using an object or variable, it will also cause some security problems if it is not cleaned up in time

Constructor function: initialization, which is implicitly called when an object is generated

There is a system generated constructor by default. If we implement the constructor ourselves, the system default constructor will no longer be generated
Appearance: the function has the same name as the class and does not need to return a value. It can pass parameters (1 or more) or no parameters

Constructor syntax: class name () {}

  1. Constructor, no return value and no void
  2. The function name is the same as the class name
  3. Constructors can have arguments, so overloading can occur
  4. When the program calls the object, it will automatically call the construct without manual call, and it will only be called once

Code demonstration:

#include <iostream>
using namespace std;
/*Special functions in classes*/
/*
Constructor: function: initialization, which is implicitly called when an object is generated
 There is a system generated constructor by default. If we implement the constructor ourselves, the system default constructor will no longer be generated
 Appearance: the function name has the same name as the class. There is no need to return a value. It can pass parameters (one or more) or no parameters
 Multiple constructors can exist at the same time
*/

class Clock
{
public:
    Clock();      //There can be more than one constructor
    Clock(int h);
    void ShowTime();
    void SetTime(int h,int m,int s);
private:
    int hour;
    int minute;
    int second;
};
Clock::Clock()
{
    cout << "Clock()" << endl;
    hour = minute = second = 0;
}
Clock::Clock(int h)
{
    cout << "Clock(int)" << endl;
    hour = h ;      minute = second = 0;
}
void Clock::ShowTime()
{   cout << hour << ":" << minute << ":" << second << endl;  }
void Clock::SetTime(int h,int m,int s)
{   hour = h; minute = m; second = s;   }

int main()
{
    Clock c0;
    Clock c1(4);
    Clock c2(14,15);
//  c1.ShowTime();

    return 0;
}

Cannot copy outside class

#include <iostream>
using namespace std;
/*Special functions in classes*/
/*
Constructor: function: initialization, which is implicitly called when an object is generated
 There is a system generated constructor by default. If we implement the constructor ourselves, the system default constructor will no longer be generated
 Appearance: the function name has the same name as the class. There is no need to return a value. It can pass parameters (one or more) or no parameters
 Multiple constructors can exist at the same time
*/

class Clock
{
public:
    Clock(int h=0,int m=0,int s=0);
    void ShowTime();
    void SetTime(int h,int m,int s);
private:
    int hour;
    int minute;
    int second;
};
Clock::Clock(int h,int m,int s)
{
    cout << "Clock(3*int)" << endl;
    hour = h ;
    minute = m;
    second = s;
}
void Clock::ShowTime()
{   cout << hour << ":" << minute << ":" << second << endl;  }
void Clock::SetTime(int h,int m,int s)
{   hour = h; minute = m; second = s;   }

int main()
{
    Clock c0;
    Clock c1(4);
    Clock c2(14,15);
//  c1.ShowTime();

    return 0;
}

3.2 destructor

Destructor function: clean or destroy, which is implicitly called when an object's life cycle is about to end

There is a system generated destructor by default. If we implement the constructor ourselves, the system default destructor will no longer be generated
Appearance: the function has the same name as the class, preceded by ~, and no return value is required

Destructor syntax: ~ class name () {}

  1. Destructor, no return value, no write void
  2. The function name is the same as the class name, and the symbol is added before the name~
  3. Destructors cannot have parameters, so overloading cannot occur
  4. The program will automatically call the destructor before the object is destroyed, without manual call, and will only call it once

Adding braces to a variable is equivalent to a temporary variable

Code demonstration:

#include <iostream>
using namespace std;
/*Special functions in classes*/
/*
Destructor: function: clean or destroy, which is implicitly called when an object's life cycle is about to end
 There is a system generated destructor by default. If we implement the destructor ourselves, the system default destructor will no longer be generated
 Appearance: the function name has the same name as the class, preceded by ~, no return value is required, and no parameters can be passed
*/

class Clock
{
public:
    Clock(int h=0,int m=0,int s=0);
    ~Clock();
    void ShowTime();
    void SetTime(int h,int m,int s);
private:
    int hour;
    int minute;
    int second;
};
Clock::Clock(int h,int m,int s)
{
    cout << "Clock(3*int)" << endl;
    hour = h ;
    minute = m;
    second = s;
}
Clock::~Clock()
{
    cout << "~Clock()  " << hour <<  endl;
}

void Clock::ShowTime()
{   cout << hour << ":" << minute << ":" << second << endl;  }
void Clock::SetTime(int h,int m,int s)
{   hour = h; minute = m; second = s;   }

int main()
{
    Clock c0;
    {
    Clock c2(14,15);
    }
    Clock c1(4);

    return 0;
}

3.3 copy constructor

Copy constructor function: initialization. When an object is generated and initialized with an existing object of the same type, the copy constructor is called

By default, there is a copy constructor generated by the system. If we implement the copy constructor ourselves, the default one will not be generated
Appearance: the function has the same name as the class, does not need to return a value, and can pass a parameter (the parameter is a constant reference).

Code demonstration: constant reference

”The same storage space has the same name“

#include <iostream>
using namespace std;
/*Special functions in classes*/
/*
Copy constructor: function: initialization. When an object is generated and initialized with an existing object of the same type, the copy constructor is called 
By default, there is a copy constructor generated by the system. If we implement the copy constructor ourselves, the default one will not be generated
 Appearance: the function name has the same name as the class, no return value, and the parameter is often referenced
*/

class Clock
{
public:
    Clock(int h=0,int m=0,int s=0);
    Clock(const Clock &other);
    ~Clock();
    void ShowTime();
    void SetTime(int h,int m,int s);
private:
    int hour;
    int minute;
    int second;
};
Clock::Clock(int h,int m,int s)
{
    cout << "Clock(3*int)" << endl;
    hour = h ;
    minute = m;
    second = s;
}
Clock::Clock(const Clock &other)
{
    cout << "Clock(&)" << endl;
    hour = other.hour;
    minute = other.minute;
    second = other.second;
}
Clock::~Clock()
{
    cout << "~Clock()  " << hour <<  endl;
}

void Clock::ShowTime()
{   cout << hour << ":" << minute << ":" << second << endl;  }
void Clock::SetTime(int h,int m,int s)
{   hour = h; minute = m; second = s;   }

int main()
{
    Clock c1(4);
    Clock c2 = c1;
    c2.ShowTime();

    return 0;
}

4, C + + memory model

During the execution of C + + program, the general direction of memory is divided into four areas

  • Code area: it stores the binary code of the function body and is managed by the operating system
  • Global area: stores global variables, static variables and constants
  • Stack area: automatically allocated and released by the compiler to store function parameter values, local variables, etc
  • Heap area: it is allocated and released by the programmer. If the programmer does not release it, it will be recycled by the operating system at the end of the program

new && delete

Using new operator in C + + to open up data in heap

The data developed in the heap area is manually developed by the programmer, manually released, and released by using the operator delete

Syntax: new data type

For the data created with new, the pointer of the type corresponding to the data will be returned

Example 1: basic syntax

#include <iostream>
using namespace std;

int main()
{
    char *p = new char;
    if(p == NULL)
        return -1;

    *p = 'a';
    cout << *p << endl;

    delete p;  //Using delete to release heap data

    return 0;
}

Example 2: array

#include <iostream>
using namespace std;

/*  new delete */

class Clock
{
public:
    Clock(int h=0,int m=0,int s=0);
    Clock(const Clock &other);
    ~Clock();
    void ShowTime();
    void SetTime(int h,int m,int s);
private:
    int hour;
    int minute;
    int second;
};
Clock::Clock(int h,int m,int s)
{
    cout << "Clock(3*int)" << endl;
    hour = h ;
    minute = m;
    second = s;
}
Clock::Clock(const Clock &other)
{
    cout << "Clock(&)" << endl;
    hour = other.hour;
    minute = other.minute;
    second = other.second;
}
Clock::~Clock()
{
    cout << "~Clock()  " << hour <<  endl;
}

void Clock::ShowTime()
{   cout << hour << ":" << minute << ":" << second << endl;  }
void Clock::SetTime(int h,int m,int s)
{   hour = h; minute = m; second = s;   }

//Heap array
int main()
{
   //When new is executed, it must go through the construction process
    Clock *p = new Clock[5];  //Five spaces of this type are required in succession
    delete []p;

    return 0;
}

Example 3:

#include <iostream>
using namespace std;

class A
{
public:
    A() {   str = new char[1]; *str = '\0'  };
    ~A() { delete []str;    }
    void Show() {   cout << str << endl;    }
private:
    char *str;
};

int main()
{
    A a1;
    return 0;
}

5, Inherit

Inheritance is one of the three characteristics of object-oriented

There are special relationships between some classes, such as the following figure:

We found that when defining these classes, the lower level members not only have the commonalities of the upper level, but also have their own characteristics.
At this time, we can consider using inheritance technology to reduce duplicate code

5.1 inheritance method:

There are three inheritance methods:

  • Public inheritance
  • Protect inheritance
  • Private inheritance
Parent class-Subclass base class-Derived class
        parent->:   public          protected           private
    public:         public          protected           -
    protected:      protected       protected           -
    private:        private         private             -

Example:

#include <iostream>
using namespace std;

class A
{
public:
    A() {cout << "A()" << endl;}
    ~A() {cout << "~A()" << endl;}
    void GetA() {  cout << "A::GetA() " <<  a << endl;}
protected:
    void Show()  {  cout << "A::Show()" <<  a << endl;}
private:
    int a;
};

class B : public A
{
public:
    B() {cout << "B()" << endl;}
    ~B() {cout << "~B()" << endl;}
    void GetShow() { Show();    }
    void GetA() {  cout << "B::GetA() " << endl;}
};

int main()
{
    B tmp;
    tmp.GetA();

    return 0;
}

Tags: C++ Qt

Posted by bogins on Wed, 22 Sep 2021 20:59:28 +0530