当前位置:网站首页>Classes and objects (1)

Classes and objects (1)

2022-06-11 21:44:00 Code loving students

Catalog

1. C and C++ What's the main difference between ?

2. class

3. The definition of a class

Two methods of defining classes

4.  Class access qualifier and encapsulation

4.1 Access qualifier

4.2  encapsulation

 5. Scope of class

 6. Class instantiation

7. Class size

7.1 Find the size of the class


1. C and C++ What's the main difference between ?

C Language is Process oriented Of , It's about The process , Analyze the steps to solve the problem , Solve the problem step by step through the function call . C++ Is based on object-oriented Of , Focus on the object , Split a thing into different objects , It depends on the interaction between objects .

2. class

stay C In language struct Only variables can be defined , And in the C++ in struct You can define variables , You can also define functions .

And in the C++ I prefer to use class Instead of struct.

3. The definition of a class

class Student {
	
	// The class body : Constructed by member variables and member functions 
};

class yes Class keyword ,student yes Class name ,{ } Inside is The class body , Note that you should add a semicolon after defining the class .

The elements in a class are called members of the class : The data in the class is called Attributes of a class perhaps Member variables ; The functions in the class are called Class method perhaps Member functions .

Two methods of defining classes

1. The declaration and definition are in the class

If you define a function in a class , It is generally considered to be an inline function .

struct Student
{
	void SetStudentInfo(const char* name, const char* gender, int age)
	{
		strcpy(_name, name);
		strcpy(_gender, gender);
		_age = age;
	}

	void PrintStudentInfo()
	{
		cout << _name << " " << _gender << " " << _age << endl;
	}

	char _name[20];
	char _gender[3];
	int _age;
};

2. The statement in .h In file , It's defined in .cpp In file

Generally speaking, the definition is this format , But the scope of the function defined in the class is in the class , When defining outside a class, it should be explained that it is the function in the class , So we need to add Class name ::

  In general, we recommend the second method .

4.  Class access qualifier and encapsulation

4.1 Access qualifier

stay C++ There are three kinds of access qualifiers, namely :1. public 2. protected 3. private, Being modified by different qualifiers has different effects :

1. public Decorated members can be accessed directly outside the class

2. protected and private Decorated members cannot be directly accessed outside the class ( here protected and private It's similar )

3. The access scope starts from where the access qualifier appears until the next access qualifier appears

  If only public A qualifier , From : Until } All are public Embellished

We can see that variables can be used outside the class , Because he is public modification Of

 

  When the member variable is added with private when , It means that the following are private Embellished , Naturally, it is not accessible outside the class .

4. class The default access rights of are private,struct by public( because struct To be compatible with C)

At the beginning , adopt struct Class... Is introduced , that class and struct What's the difference between ?

because C++ Is based on C Generated so C++ Compatibility required C Language , therefore C++ in struct It can be used as a structure . in addition C++ in struct It can also be used to define classes . and class Yes, the definition class is the same , The difference is that struct The default access method for members is public,class Yes, the default access method for members is private.

4.2  encapsulation

There are three characteristics of object-oriented : encapsulation 、 Inherit 、 polymorphic , These three are also the essence of object-oriented .

What is encapsulation ?

encapsulation : Organically combine data and methods of operating data , Hide object properties and implementation details , Only expose interfaces to interact with objects , Encapsulation can help us enhance the robustness of our code , There is no need to consider the technical level of the person using the interface .

Encapsulation is a kind of management , Compare a class to a museum ,public The decoration is what tourists can watch ,private What is embellished is what cannot be seen , We take out what we want others to do , Hide what you don't want others to see or modify .

5. Scope of class

stay C perhaps C++ Variables or functions defined in are found from the bottom up , Do classes also follow this rule ?

I found that if you follow the above rules , that Print The function cannot access the following three variables , But in the class, you can access , The variables and functions defined in the class can be used anywhere in the class .

 6. Class instantiation

When creating objects using class types , It is called instantiation of a class .

1. Class is just a model thing , Defines which members of the class , Defining a class does not allocate the actual memory space to store it .

2. A class can instantiate multiple objects , The instantiated object will use the actual physical space , Store class member variables .

3. Class is an architectural drawing , Creating objects is equivalent to implementing architecture , If you don't create objects , That kind is a model .

7. Class size

7.1 Find the size of the class

A class contains functions and variables , What does the created object contain ?

1. Object contains functions and variables

2. Object contains only variables , Functions are contained in common code snippets

First, let's review the calculation of the size of the structure :

1. The first member is offset from the structure by 0 The address of .

2. Other member variables are aligned to a number ( Align numbers ) An integral multiple of the address of .

Be careful : Align numbers = The compiler defaults to an alignment number and the smaller value of the member size . VS The default alignment number in is 8

3. The total size of the structure is : Maximum number of alignments ( The maximum of all variable types and the minimum of default alignment parameters ) Integer multiple .

4. If the structure is nested , The nested structure is aligned to an integral multiple of its maximum alignment , The overall size of the structure is All maximum alignments ( The number of alignments with nested structures ) Integer multiple .

Calculate the size of the class by calculating the structure , It is found that the same value is obtained

  Through disassembly, we can also see that the function called in the object is also at the same address .

We can find that the created object should only contain variables , The function is contained in a common code segment .

  We find that when there are only function members or no members in the class , The size of the class will not be 0, The compiler will give an empty class a byte to represent the class .


原网站

版权声明
本文为[Code loving students]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112135376719.html