Class and object beginner

Posted by Dark-Hawk on Sun, 23 Jan 2022 12:13:51 +0100

Class is like a container, putting the same transaction in the same place.

Equivalent to Xiao Ming, Xiao Hong, etc. all belong to the category of people. Xiao Ming is also called object in c + +.

Take Xiao Ming as an example:

Xiao Ming has his own height, weight and age. These are the attributes that Xiao Ming has called objects. When Xiao Ming introduces himself, he needs to say his relevant information, saying that this action is called behavior, which is realized through functions in the program.

Therefore, the class consists of two parts: object properties and object behavior (also known as member properties and member functions).

The approximate code is as follows (forgot to add public)

By creating an object in the main function, it is similar to a structure

class person student//class can save
student.name="xiaoming"
student.weight=90.5;
student.age=18;

When it comes to structure, you will find that the usage of class and struct are very similar. The most essential difference between them is that the permissions are different.

Class has three permissions:

Public: public (accessible both inside and outside the class)

Private: private (it can be accessed inside the class but not outside the class)

Protected: protected (it can be accessed inside the class, but not outside the class)

(the difference between private and protected is whether the subclass can inherit the parent class)

struct is public and class is private by default.

So you can also use struct to build classes.

We usually change the value of our protection or privacy function (also known as encapsulation) by exposing the function as an interface to improve the security of the code.

#include<iostream>
#include<string>
using namespace std;
class person{
   public:
       void input_weight();
       void input_name();
    private:
        double weight;
        string name;     
};
void person::input_weight(){
    int x;//Temporary storage
    cout<<"Enter weight:  "<<endl;
    cin>>x;
    weight=x;

}
void person::input_name(){
    string x;//Temporary storage
    cout<<"Enter name:   "<<endl;
    cin>>x;
    name=x;
    
}

Functions within a class can be defined outside the class, but the function should be declared within the class.

: is the class to which the surface belongs. In a team, it is inevitable that the variable names are the same. Use:: to represent the content belonging to a certain part.

2. Initialization, deletion and copying of objects

Constructors and destructors are responsible for clearing objects before initialization and release.

As long as you create an object, there must be destructors, copy functions, and constructors

Constructor (initialization function)

class person(){
    person()//After (), you can add: to initialize object properties
    {
        cout<<"Initialization succeeded"<<endl;
    }    
};
/*person():weight(10),high(20)
    {
    }
10 And 20 can also use variables, but in this way, the variable name should be added in parentheses*/

This is a function declared within the class and must be kept open before you can use the constructor you write. Otherwise, it is the default empty function of the system (only the empty shell has no content)

Destructor (before releasing object)

class person(){
    public:
    ~person(){
        cout<<"Delete succeeded";
   }
}

Parentheses in destructors cannot take arguments. Constructors can take parameters and can be overloaded (if constructors take parameters, the created object should also have parameters)

(main function)

Operation result diagram

Now let's talk in detail about the use of destructors (copy constructors).

In general, there are three ways to use:

1. Bracket representation:

person a;//person a() does not exist; The compiler creates a function for by default

person a(int b);//When a parameter exists

//copy constructor 
person b(a);//Parentheses are also objects

2. Display method

person a;//Same as the first one

//With one parameter

person a=person(10)//Take 10 as an example, but this will create a temporary object, which will disappear after this line of code, and will also run destructors and constructors

//copying functions 

person b=person a;

3. Implicit representation

person a=10;//Equivalent to person a=person(10)

//copying functions 
person a=b;

Next is the copy constructor

It is divided into two categories: deep copy and shallow copy

Shallow copy: This refers to direct assignment. If a member has a copy of pointer type, it will point to the same place.

!!!! In a program, the memory allocation controlled by the programmer is called heap, which should be released at the back of the program. So there is usually code like this in the destructor

delete is similar to free. However, there will be a problem with shallow copy. Because it is completely copied, the same address in the heap will be released twice, causing the program to crash. Deep copy solves this problem.

Deep copy is to apply for another area in the heap area on the basis of shallow copy, which solves the problem of secondary release.

person(const person &a){
    weight=new int(*a.weight);
}

3. Class objects can be other classes

Note here that the class as a class is a large class and the class as an object is a small class.

When using small classes, the method is similar to that of ordinary objects, but the decomposition and construction order of the two classes are different,

Small classes are constructed first, and large classes are destructed first.

4. Static member function

Functions starting with static are called static functions, which are called static member functions in classes. All objects share this function, and all static member functions can only call static member variables.

!!! Static member variables do not count as member objects

5.this pointer

this is the pointer of each object, pointing to the object itself. It is easy to use when there are many variable names.

#include<iostream>
using namespace std;
class person {
	public:
		void glow(int age) {
			this->age=age;
		}
		void add(person &a){
			this->age+=a.age;
			
		}
		int age;
};
int main() {
	person I;
	I.glow(18);
	cout<<"My age is: "<<I.age <<endl;
	
	person You;
	You.glow(10);
	cout<<"Your age is: "<<You.age<<endl;
	You.add(I);
	cout<<"Your age is: "<<You.age<<endl;
	
	
	
	
	return 0;
}
//The results are 18,10,28.

However, the most interesting thing that can be achieved through this is infinite accumulation

#include<iostream>
using namespace std;
class person {
	public:
		void glow(int age) {
			this->age=age;
		}
		person& add(person &a){
			this->age+=a.age;
			return *this;
		}
		int age;
};
int main() {
	person I;
	I.glow(18);
	cout<<"My age is: "<<I.age <<endl;
	
	person You;
	You.glow(10);
	cout<<"Your age is: "<<You.age<<endl;
	You.add(I).add(I).add(I).add(I).add(I).add(I);
	cout<<"Your age is: "<<You.age<<endl;
	
	
	
	
	return 0;
}

By modifying the add function, * this refers to the object itself, so that the add function returns an object, which can be accumulated at the bottom.

6. Constant functions can only call constant objects, and constant objects can only use constant functions.

Objects and functions modified with const are called constant objects and constant functions. If you want to use extraordinary variables in constant functions, you need to use the keyword mutable before declaring variables.

7. Friends

The meaning of friend is almost the same as that of friend. Give others a little more permission, that is, things called friends can access private properties in the class

#include<iostream>
using namespace std;
class xiaoming {
	public:
		xiaoming() {
			weight=80;
			high=180;
		}
		int high;
	private:
		int weight;
};
void ask() {
	xiaoming me;
	cout<<"What's Xiao Ming's height:  "<<me.high<<endl;
}
int main() {
	ask();
	return 0;
}

It's OK to access public properties in the above paragraph, but at this time, if you want the ask function to access weight, you can't realize it. At this time, there is the role of friends.

#include<iostream>
using namespace std;
class xiaoming {
	friend void ask();
	public:
		xiaoming() {
			weight=80;
			high=180;
		}
		int high;
	private:
		int weight;
};
void ask() {
	xiaoming me;
	cout<<"What's Xiao Ming's height:  "<<me.high<<endl;
	cout<<"What's Xiao Ming's weight:  "<<me.weight<<endl;
}
int main() {
	ask();
	return 0;
}

The friend keyword is friend. After the class declaration, it is followed by the friend declaration. Not only can global functions be used as friends and member functions (functions of other classes), but other classes can be declared as friends in a similar way.

If there is anything wrong, please point it out!!!!

Topics: C++ Back-end