Learning C++: Classes and Objects

deep copy and shallow copy

  • Shallow copy: Simple assignment copy operation.
  • Deep copy: Re-apply for space in the heap memory and perform a copy operation.

Note that this is different from the deep copy and shallow copy in python.

Classes & Objects

class access modifier

piblic: Members can be accessed both inside and outside the class.

private: Accessible only within the class and friend functions, not outside the class.

protect: Accessible within the class and subclasses, not accessible outside the class.

Member attributes are recommended to be encapsulated as private and exposed through public methods.

Inheritance method:

describe

constructor and destructor

Constructor: It is used for the initialization of member properties, and is automatically called by the system when an object is created.

Destructor: The system performs some cleanup work before the object is destroyed.

  • Constructors and destructors do not return any value, nor do they return void.
  • Constructors can have parameters and can be overloaded.
  • The constructor has the same name as the class.
  • The destructor cannot have parameters and cannot be overloaded.
  • The destructor has the same name as the class, preceded by~

Types of constructors:

  • Divided according to parameters: construction with parameters and construction without parameters

  • Divided by type: ordinary construction and copy construction

Note: Do not initialize anonymous objects with a copy constructor

Constructor overloading:

The constructor can be overloaded in various ways, just be careful not to cause ambiguity. For example, in the following situation, an error will occur during initialization. The second constructor with parameters is given a default value, so it can be called without passing any parameters. This is ambiguous with the no-argument constructor:

class Student
{
    
    
    public:
        string name;
        int age;

        Student();
        Student(string, int);
};

Student::Student()
{
    
    
    cout << "无参构造函数" << endl;
}

Student::Student(string name="xiaoming", int age=18)//这里全部加默认参数会和无参构造函数出现歧义
{
    
    
    cout << "有参构造函数" << endl;
    this->name = name;
    this->age = age;
}

Various initialization methods of objects

  1. bracketing

    Student p1;
    Student p2("xiaoming", 18);
    Student p3(p2);
    
  2. display method

    Student p1;
    Student p2 = Student("xiaoming", 18);
    Student p3 = Student(p2);
    
  3. Implicit conversion method (not intuitive enough)

    Student p4 = {
          
          "xiaoming", 18};
    Student p5 = p4;
    

Full example:

#include <iostream>
#include <string>

using namespace std;

class Student
{
    
    
    public:
        string name;
        int age;

        Student();
        Student(string, int);
        Student(const Student & p);
        ~Student();

        void set_age(int age);
        int get_age();
};

Student::Student()
{
    
    
    cout << "无参构造函数" << endl;
}

Student::Student(string name, int age)
{
    
    
    cout << "有参构造函数" << endl;
    this->name = name;
    this->age = age;
}

Student::Student(const Student & p)
{
    
    
    cout << "拷贝构造函数" << endl;    
}

Student::~Student()
{
    
    
    cout << "析构函数" << endl;
}

void Student::set_age(int age)
{
    
    
    this->age = age;
}

int Student::get_age()
{
    
    
    return this->age;
}

int main()
{
    
    
    //方法1
    Student stu1; //注意stu1不能加括号,否则编译器会将其看成函数声明
    //方法2
    Student stu2("xiaoming", 10);
    //方法3
    Student stu3 = Student();
    //方法4
    Student(); //匿名对象,当前行结束后,系统回收内存
    //方法5 new方法在堆内存创建对象
    Student * student = new Student("xiaoming", 20);
    delete student;
	//方法6
	Student stu6 = {
    
    "小明", 20};

    system("pause");
    return 0;
}

When to call the copy constructor:

  • Initialize a new object using an already created object
  • Passing parameters to functions by value
  • returns the local object by value

Constructor calling rules

  • By default, when creating a class, the c++ compiler will add at least three functions to each class
    • default constructor (empty implementation)
    • destructor (empty implementation)
    • copy constructor (value copy)
  • If you write a parameterized construction yourself, the compiler will no longer provide a default construction, but will provide a copy constructor .
  • If we write a copy constructor, the compiler doesn't provide any constructor anymore.

Note: In the copy constructor, if the attribute is opened in the heap area, you must provide the copy constructor yourself to prevent problems caused by shallow copy.

friend function

keywords:frind

  • Can access private members of the class

Three implementations:

  1. Global functions as friends
  2. class as friend
  3. member function as friend

This pointer

This points to the object to which the called function belongs.

  • resolve name conflicts
  • Return the object itself with*this

static member

Static member variable:

  • All objects share the same data
  • Allocate memory during compilation
  • In-class declaration, out-of-class initialization

Static member function:

  • All objects share the same function
  • Static member functions can only access static member variables
  • Static member function does not have this pointer
  • It can also be used when not creating an object

references

おすすめ

転載: blog.csdn.net/xuyangcao123/article/details/124794214