C++ Classes and Objects (Part 1)

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right

Article directory

Table of contents

foreword

1. What are object-oriented and process-oriented?

1. Process-oriented

2. Object Oriented

Second, the introduction of the class

There are two ways to define a class:

method one

Method Two

3. Class access qualifier and encapsulation

1. Access qualifiers

2. Encapsulation

3. Scope

Summarize



foreword

Share a sentence:

The UNIX operating system is simple and consistent, but only geniuses (programmers at least) can grasp and appreciate its simplicity.

                                                                                                           ——Dennis Ritchie


提示:以下是本篇文章正文内容,下面案例可供参考

1. What are object-oriented and process-oriented?

1. Process-oriented

The C language is process-oriented , focusing on the process , analyzing the steps to solve the problem, and solving the problem step by step through function calls.

How to understand this sentence?

for example:

I want to eat braised pork today, so I have to prepare the ingredients

Steps in making:

Step 1: Clean the pork belly, put the whole strip in boiling water and scald for 30 seconds;

Step 2: Take it out and rinse it under the tap until it cools down, cut into thick pieces, put it in a boiling water pot and blanch it; take it out again, put it under the tap and rinse it until it is cool, put it in a boiling water pot and blanch it again, remove it Drain water;

Step 3: Put oil in the pan, add cinnamon bark, bay leaves and star anise to the cold oil, and stir-fry over low heat to produce fragrance;

Step 4: Add the pork belly and stir-fry together to make the fat flow out and reduce the greasy taste;

Step 5: Stir-fry the pork belly until both sides are slightly yellow;

Step 6: Add rock sugar or brown sugar, straw mushroom soy sauce and light soy sauce, and salt;

Step 7: Stir fry evenly, so that each piece of meat is colored;

Step 8: Add hot boiling water that has not covered all the ingredients, do not cover the pot, boil on high heat, and remove the foam;

Step 9: Add ginger slices and jujube, boil on high heat, then turn to low heat and simmer for 40 minutes to 1 hour;

The tenth step: the fire collects the juice;

Step 11: Stir-fry while collecting juice;

Step 12: until the sauce is thick and every piece of pork belly is covered with the sauce.

2. Object Oriented

C++ is based on object-oriented, focusing on objects, dividing one thing into different objects, and relying on the interaction between objects to complete.

If you want to eat braised pork, go directly to the restaurant and order a piece of braised pork

There are three objects: people, money, and restaurants.

The whole process is: people go to the restaurant to order food, and then pay.

The whole process does not require you to understand how braised pork is made.

Second, the introduction of the class

The structure of C can only define variables. In C++, the structure can not only define variables, but also define functions.

The code is as follows (example):

struct Student
{

	char _name[10];
	int _age;
	int _id; 
	void INT(const char* name,int age,int id)
	{
		strcpy(_name, name);
		_age = age;
		_id = id;
	}
	void print()
	{
		cout << "姓名: " << _name << endl;
		cout << "年龄: " << _age << endl;
		cout << "ID: " << _id << endl;
	}
};

But in C++ we prefer to use class to define

The code is as follows (example):

class MyClass
{
    //类体:成员函数与成员变量
};

class is the keyword to define the class, MyClass is the name of the class, and {} is the body of the class. Note that the semicolon after the end of the class definition
cannot be omitted.
The contents of the class body are called members of the class: variables in the class are called attributes or member variables of the class; functions in the class are called methods or member functions of the class.

There are two ways to define a class:

method one

1. The declaration and definition are all placed in the class body. Please note: If the member function is defined in the class, the compiler may treat it as an inline function. 

class MyClass
{
private:
	int _a;
	int _b;
public:
	int sum(int a, int b)
	{
		return a + b;
	}
	void print()
	{
		cout << _a << endl;
		cout << _b << endl;
	}
};

Method Two

2. The class declaration is placed in the .h file, and the member function definition is placed in the .cpp file. Note: the class name needs to be added before the member function name::

.h files

.c files

3. Class access qualifier and encapsulation

1. Access qualifiers

There are 3 types:

public

protected

private

Explanation of access qualifier]
1. Members modified by public can be directly accessed outside the class
2. Members modified by protected and private cannot be directly accessed outside the class (here protected and private are similar)
3. The scope of access rights is from The position where the access qualifier appears begins until the next access qualifier appears.
4. If there is no access qualifier behind, the scope will go to }, that is, the end of the class.
5. The default access permission of class is private, and struct is public (because struct is compatible with C)

2. Encapsulation

In the class and object stage, it is mainly to study the encapsulation characteristics of the class, so what is encapsulation?
Encapsulation: organically combine the data and the method of manipulating the data, hide the properties and implementation details of the object, and only expose the interface to interact with the object. Encapsulation is essentially a management that makes it easier for users to use classes.

Encapsulation in the C++ language can organically combine data and methods of manipulating data through classes, hide internal implementation details of objects through access rights, and control which methods can be used directly outside the class.

3. Scope

A class defines a new scope, and all members of the class are in the scope of the class. When defining members outside the class, you need to use the :: scope operator to indicate which class domain the member belongs to.

code show as below:

class Stack
{
private:
	int* _a;
	int _top;
	int _capacity;
public:
	void Init();
	void Push(int x);
	
};

void Stack::Init()
{
	_a = nullptr;
	_top = _capacity = 0;
}

Summarize

Tip: Here is a summary of the article: that’s all for
today, the content has not been explained yet, waiting for follow-up supplements, thank you

Guess you like

Origin blog.csdn.net/qq_45591898/article/details/129529093