C ++ programming talk: object-oriented

zhaozj2021-02-17  50

Software development is an extremely complex process, and a small code we can quickly and accurately, but when you face a huge software system, do you have a feeling of understanding? In the era of using C, programming thoughts are structured. Your C language may constantly teach you how to use structured methods to complete a program, and you may also know an important in software development. Law (WIRTH law): Program = Algorithm data structure in structured design people usually look at this law: Program = (algorithm) (data structure) For example: (I still use the C standard library to write below Code, convenient and subsequent code comparison) #include

Using namespace std;

Typedef struct student

{

CHAR STRNAME [50]; // Name

INT MATH; / / Mathematics score

Int chinese; // language

Int total; / / total points

}.

Void InitData (Studient * Some)

{

//

//Initialization data

Some-> strname [0] = '/ 0';

Some-> math = 0;

Some-> chinese = 0;

Some-> Total = 0;

}

Void InputData (Studient * Some)

{

///

// Get input

Cout << "Enter Name" <

> Some-> Strname;

COUT << "Enter Math" <

> Some-> Math;

COUT << "Enter Chinese" <

> Some-> Chinese;

//

// Computing total

Some-> Total = Some-> Math Some-> CHINESE

}

Void PrintData (Student Some)

{

Cout << Some.Strname << "'s Total Mark IS:" << Some.Total << Endl;

}

// The above part can be placed separately in one head and CPP

Main ()

{

Student SomeOne;

InitData (& Someone);

InputData (& Someone);

PrintData (someone);

}

So far, the division of the program becomes relatively simple, and the development of the team is possible. In the object-oriented, the Wirth law exists is very similar to the above structure: Object = (Algorithm Data Structure) Program = Object Object Surface seems to be too big, but this difference is us Brought huge change. There is such a sentence: "Programming is reflected in the computer", I think it is also the best in the object-oriented (Object-Oriented), for example, in the previous example, the data we designed The structure is a student performance performance, while the operation of the data structure (function) is separated, although these operations are generated for this data structure. In order to manage a large amount of data, we have to use them carefully. An important concept for object-oriented is it is a class (Class) C is it. The biggest difference between classes and struct data structures is to provide limited level (visibility) protection - let's go to the complex inheritance and polymorphism. It is through this way, we can provide very effective protection for data members while making the data structure more in line with the real line. In the object-oriented concept, the previous data structure is represented as follows: #includeusing namespace std;

Class Student

{

Private:

//Attributes

CHAR STRNAME [50]; // Name

INT MATH; / / Mathematics score

Int chinese; // language

Int total; / / total points

PUBLIC:

//method

Char * getname () {return strname;

INT getMath () {return math;};

INT getCHINESE () {Return Chinese;};

INT gettotal () {return total;

STUDENT ();

Void InputData ();

Void PrintData ();

}

StudENT :: Student ()

{

//

//Initialization data

STRNAME [0] = '/ 0';

Math = 0;

CHINESE = 0;

Total = 0;

}

Void Student :: InputData ()

{

///

// Get input

Cout << "Enter Name" <

> strname;

COUT << "Enter Math" <

> math;

COUT << "Enter Chinese" <

> Chinese;

//

// Computing total

Total = Math Chinese;

}

Void Student :: PrintData ()

{

COUT << Strname << "'s Total Mark IS:" << Total << Endl;

}

int main ()

{

Student SomeOne;

Someone.inputdata ();

Someone.printdata ();

}

We don't care about the implementation details of the class, now compare the calling process in the main function, each method and the corresponding data structure are connected together, for external calls, we don't need to care about data structure and corresponding algorithms The relationship because they have been associated. Perhaps this example is not enough, maybe my explanation is unclear, but you imagine: If we model an object, such as a dog, we need to determine its properties (color, size) and behavior (run, call), We must hope that these are associated with the dog: Class Dog (attribute)

Color

Size

(behavior)

Run

Yelp

If it is a structure:

Struct dog

Color

Size

(Dependency)

Run (DOG Somedog) Yelp (Dog Somedog)

Obviously, the subsequent structure is more complicated, not to lack effective member properties protection. Another power-oriented power is inheritance and polymorphism, the next one will discuss again. I hope that my previous expression is enough. Object-oriented is not clear, I can describe it here, I just describe a one-plane development process. I hope it can be a group that can provide some help for beginners.

转载请注明原文地址:https://www.9cbs.com/read-28734.html

New Post(0)