Get the App
SLTechnology News&Howtos  ›  Development  › 

Detailed introduction of objects and classes in C++

Shulou Source: shulou.com Published: 2022-06-03 17:31:09 11月13日 Update

This article introduces the relevant knowledge of "detailed introduction of objects and classes in C++". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

What is the object?

Process oriented vs object oriented

Process oriented

object-oriented

What is a class?

The format of the class

Member functions of a class

Function access permission

Method one

Method two

Method three

Inline member function

What is the object?

Everything is an object, that is, everything in the legend is an object.

Composition of the object:

Data: describing the properties of an object

Function: code that describes the behavior of an object and operates accordingly according to external information

Objects with the same properties and behaviors are abstracted into class

A class is an abstraction of an object

Object is a special case of class.

Process-oriented vs object-oriented process-oriented

Process-oriented design:

Around the function, use a function to implement a function

Program = algorithm + data structure

The algorithm and the data structure are independent and designed separately.

object-oriented

Object-oriented design:

Encapsulate the algorithm and data in one object

Design the required singer classes and objects

Send a message to the object concerned

Object = algorithm + data structure

Program = object * n + message

What is a class?

In C++, classes are used to describe objects. The class belongs to the user-defined data type, and the data of this type has certain behavior ability, that is, the method described in the class. Generally speaking, the definition of a class consists of two parts, one is the properties of the class, the other is the methods owned by the class.

The format of the class

Format:

Class class name {public: / / Public behavior or attribute private: / / Private behavior or property}

Example:

Main.cpp:

# include "Student.h" using namespace std;int main () {Student student1 (1, "Little white",'f'); student1.display (); return 0;}

Student.cpp:

# include "Student.h" # include using namespace std;Student::Student (int n, string p, char g) {num = n; name = p; gender = g;} void Student::display () {cout

Tags: Object function member method data procedure design property program behavior private output algorithm structure result clocked content variable just point to Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux Shulou Tech Info MySQL OPPO Reno