Class, struct, pointer, reference

Thoughts from blogger:
The "day shift" blogger who almost became "month shift" has finally updated hhhhhh hhhh
This should be the last article on C + + syntax!
For this account, I intend to use it as a place to record learning, so in the future, in addition to updating the algorithm solution, there may also be CET-6 learning notes, html, etc. (maybe I will update the learning experience of section 3 tomorrow hhh)
Thank you for your attention! (I can't do the daily shift after the school starts, but I try to update it in two or three days)
Thank you!!! Remember to praise!!! Hee hee

Class, struct, pointer, reference

1, Classes and structures:

Class can perfectly package variables, arrays, and functions together -- y total

Class definition:

Class has two keywords: private and public

Private: it means that all functions, variables and other contents behind it are private. They are private member variables and can only be called inside the class, but not outside the class (that is, private is a private thing, and you can't use someone else's)

Public: it means that all the contents behind it are common member variables, which can be called outside the class or inside the class (public is a public thing, which is equivalent to a blackboard eraser in school, and everyone can use it)

#include <iostream>

using namespace std;

//Class - class 
class Person//It is equivalent to defining a new type called Person, which stores all the information of the Person 
{
	private:
		int age,height;//Define a person's age and height (can only be used in class) 
		double money;//Define students' pocket money 
		string books[100];//The name of the classmate's book 
		
		int get_height()//Functions can also be defined in private, such as 
		{
			return height;
		} 
		
	public:
	    string name;//Define the name of the classmate 
		
		void say()
		{
			cout<<"I'm "<<name<<endl;//”Who am I“ 
		}	
		
		int get_age()//Get the age, because age is in this class, so the function can be called 
		{
			return age;
		}
		
		void add_money(double x)//Mom gave the child pocket money! 
		{
			money+=x;
		}
};//Class must be followed by ";" 

In the class, there can be multiple private and public, for example:

#include <iostream>

using namespace std;

//Class - class 
class Person//It is equivalent to defining a new type called Person, which stores all the information of the Person 
{
	private:
		int age,height;//Define a person's age and height (can only be used in class) 
		double money;//Define students' pocket money 
		
		int get_height()//Functions can also be defined in private, such as 
		{
			return height;
		} 
		
	public:
	    string name;//Define the name of the classmate 
		
		void say()
		{
			cout<<"I'm "<<name<<endl;//”Who am I“ 
		}	
		
		int get_age()//Get the age, because age is in this class, so the function can be called 
		{
			return age;
		}
		
		void add_money(double x)//Mom gave the child pocket money! 
		{
			money+=x;
		}
		
	private://Multiple private here hhh
		string books[100];//The name of the classmate's book 
}c,persons[100];//Class must be followed by ";"
//You can define a variable c directly later, or you can define an array later

Note: all variables modified by each private and public refer to all variables after its colon and before the next keyword

Variables and functions in a class are collectively referred to as member variables of the class

Use of class:

#include <iostream>

using namespace std;

//Class - class 
class Person//It is equivalent to defining a new type called Person, which stores all the information of the Person 
{
	private:
		int age,height;//Define a person's age and height (can only be used in class) 
		double money;//Define students' pocket money 
		
		int get_height()//Functions can also be defined in private, such as 
		{
			return height;
		} 
		
	public:
	    string name;//Define the name of the classmate 
		
		void say()
		{
			cout<<"I'm "<<name<<endl;//”Who am I“ 
		}	
		
		int get_age()//Get the age, because age is in this class, so the function can be called 
		{
			return age;
		}
		
		void add_money(double x)//Mom gave the child pocket money! 
		{
			money+=x;
		}
		
	private:
		string books[100];//The name of the classmate's book 
};//Class must be followed by ";" 

int main()
{
	Person c;//Take the name defined above as a new type, and then define a classmate c
	Person persons[1000];//You can also define an array like this
    
	c.name="qf";//Name classmate c as qf. Because name is a common variable, it can be accessed externally (here)
	//c.age=3;
	//Classmate c is 3 years old!!! This is a wrong definition!!! Because age is a private variable, external access will be wrong (how can qf tell others hhh) 
	cout<<c.get_age()<<endl;//Because age is not assigned a value, it outputs a random value 
	c.add_money(1000000);//qf's mother gave qf so much pocket money (I was dreaming). add_money is a common function that can be called externally 
	 
	return 0;
}

Structs work the same as classes, except that:

If you do not add private or public to the variables in the class, the default is private

If private or public is not added in front of the variable in the structure, it is a public common variable by default

(it is customary to define structures with less data and package class es with longer code)

struct Person
{
	private:
		int age,height;
		double money;
		string books[100];
		
	public:
		string name;
		
		void say()
		{
			cout<<"I'm "<<name<<endl;
		}
		
		int set_age(int a)
		{
			age=a;
		}
		
		int get_age()
		{
			return age;
		}
		
		void add_money(double x)
		{
			money+=x;
		}
}person_a,person_b,persons[100];
There are constructors in classes and structs:
#include<iostream>

using namespace std;

struct Person
{
		int age,height;
		double money;
		
		//A constructor can be defined. The constructor has no type. The name must be exactly the same as the name of the structure. Parameters can be written in the constructor 
	
	    Person(int _age,int _height,double _money)//Constructor 
	    {
	    	age=_age;//Several variables of Person can be assigned values inside the function
	    	height=_height;
	    	money=_money;
	    }
};

int main()
{
	Person p(3,175,100.0);//Because the function is constructed above, it can call the function directly
	//Person p; error!!! Because a function is defined in this structure, you must provide three parameters 
	 
	return 0;
}
#include<iostream>

using namespace std;

struct Person
{
		int age,height;
		double money;
    
	    Person() {}//Define an empty constructor without parameters 
	    
	    Person(int _age,int _height,double _money)//Constructor 
	    {
	    	age=_age;//Several variables of Person can be assigned values inside the function
	    	height=_height;
	    	money=_money;
	    }
};

int main()
{
	Person p; //An empty constructor is defined above, and no error will be reported here 
	//When we directly define a variable without giving an initial value, it will call the default constructor
	return 0;
}
There are several other ways to initialize a structure:
#include<iostream>

using namespace std;

struct Person
{
		int age,height;
		double money;
	
	    Person() {}//Define an empty constructor without parameters 
	    
        Person(int _age,int _height){}//Manually define the constructor of two parameters to prevent error reporting
    
	    Person(int _age,int _height,double _money)//Constructor 
	    {
	    	age=_age;//Several variables of Person can be assigned values inside the function
	    	height=_height;
	    	money=_money;
	    }
};

int main()
{
	Person p={3,180,100}; //The values will be assigned sequentially in the manner defined above
    Person p={3,180};
    //Because only constructors without parameters and constructors with three parameters are defined above, an error will be reported if only two values are assigned
	return 0;
}
There is also an assignment method, which can make the code run faster:
#include<iostream>

using namespace std;

struct Person
{
		int age,height;
		double money;
	
	    Person(int _age,int _height):age(_age),height(_height) {}
	    
	    Person(int _age,int _height,double _money):age(_age),height(_height),money(_money){}
};

int main()
{
	Person p={3,180,100}; //An empty constructor is defined above, and no error will be reported here 
	 
	return 0;
}

Note: there is also a constructor in class, which is exactly the same as struct, that is, pay attention to adding public:

2, Pointers and references

The pointer points to the value of the stored variable

All programs are a process. Different processes are independent of each other, and the process has a concept of space (generally speaking, space refers to memory, with 8GB, 16GB, etc.). For example, if your computer has 4GB, in your opinion, the computer has a number group from 0 to 4GB, and the address is encoded in hexadecimal, which means that the address is from 0 × 00000000 to 0 × FFFFFFFF. Today's computers are generally 64 bit, which means that the address is 64 bit, that is, the address is from 0 × 0000000000000000 to 0 × FFFFFFFFFFFFFFFF, about memory space, as shown in the figure (stack space)

All function calls operate in the stack space. Local variables are defined in the stack space. Static variables and arrays are all defined in the heap space. Because the variables opened to the stack have no assignment, the value is uncertain, and all variables opened to the heap will be initialized to 0 by default.

For example, a char type variable is opened in the stack, and a character a... Is stored, which is difficult to describe

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
	char c='a';
	cout<<(void*)&c<<endl;//The address of c is output in the form of pointer 
	 
	return 0;
}

If continuous is defined, the address is also continuous

Address defined in the heap:

To sum up: heap space allocation is from small to large address allocation, and stack space allocation is from large to small address allocation

Some usage of pointers:

A pointer is an array index that stores memory. It points to the address where the value of a variable is stored. Therefore, we can modify the value of the variable through the pointer

#include<iostream>
#include<cstdio>

using namespace std;

char a,b;

int main()
{
	int a=10;//Define a variable a in stack space 
	int* p=&a;//p is the address of a, where "*" means that the defined variable is a pointer type, * has nothing to do with p. put int * together, it is a pointer of int type, and p is the name of the variable 
	
	cout<<*p<<endl;//The "*" here represents an operator, which means to take the address of this p 
	
	return 0;
}
#include<iostream>
#include<cstdio>

using namespace std;

char a,b;

int main()
{
	int a=10;//Define a variable a in stack space 
	int* p=&a;//p is the address of a, where "*" means that the defined variable is a pointer type, * has nothing to do with p. put int * together, it is a pointer of int type, and p is the name of the variable 
	
    //Although P stores an address, P itself has variables, because the place where p is stored actually has an address
    //int** q=&p;
    //cout<<q<<endl; You can also continue to set dolls hhh
    
	cout<<*p<<endl;//The "*" here represents an operator, which means to take the address of this p 
	*p=12;//Assign a value to p 
	
	cout<<*p<<endl;
	cout<<a<<endl;//The value of a also changed accordingly
	 
	return 0;
}

The operation results show that:

Array names are special pointers. Pointers can do operations

code:

#include<iostream>
#include<cstdio>

using namespace std;

char a,b;

int main()
{
	int a[5]={1,2,3,4,5};
	
	cout<<a<<endl;
	 
	return 0;
}

The operation results show that:

code:

#include<iostream>
#include<cstdio>

using namespace std;

char a,b;

int main()
{
	char c;
	int a[5]={1,2,3,4,5};
	
	int *p=a;
	cout<<*p<<endl;//The output is a[0] 
	
	return 0;
}

The operation proof results are as follows:

code:

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
	int a[5]={1,2,3,4,5};
	
	for(int i=0;i<5;i++)
	{
		cout<<*(a+i)<<endl;//For example, a[1] is equal to a+1
	}

	return 0;
}

The operation results show that:

A reference is similar to a pointer and is equivalent to giving an alias to a variable
#include<iostream>
#include<cstdio>

using namespace std;

char a,b;

int main()
{
	int a=10;
	int* p=&a;//The writing method of c language defines a pointer p, which is the address of A 
	int& p=a;//c + + writing method, reference and alias, defines a variable p,p and a, and stores them in an address 
	
	
	return 0;
}

3, Linked list

Linked list definition: defines a structure in which two values are stored, one is val and the other is the next pointer

#include <iostream>

using namespace std;

struct Node
{
	int val;
	Node* next;//When defining a structure, you can define its pointer inside the structure, but you cannot define variables. For example, Node next will report an error (because it is defined recursively) 
	
	Node(int _val):val(_val),next(NULL){}
};

int main()
{
   Node* p=new Node(1);
   Node* q=new Node(2);
   Node* o=new Node(3);
   
   p->next=q;//p stores the address of node 1, q stores the address of node 2, and o stores the address of node 3 
   q->next=o;//No. 1 points to No. 2, No. 2 points to No. 3, and No. 3 points to empty
   
   //This structure is called single linked list
   //Generally, the address of the first node is called the head node. The address of the head node should be saved and stored in the head 
   
   Node* head=p;
    
   //Add node in linked list
   Node* u=new Node(4);
   u->next=head;
   head=u;
   
   //Delete node (it means that you can't reach this point during traversal)
   head->next=head->next->next;
   
   //Traversal mode of linked list 
   for(Node* i=head;i!=0;i=i->next)
   {
   	cout<<i->val<<endl;
   } 
   
   return 0;
	 
}

Tags: Algorithm

Posted on Sun, 19 Sep 2021 18:29:28 -0400 by nmarisco