C + + Notes 004:c++ class popular Point said

Source: Internet
Author: User

Core :

C + + 's class is to extend the structure of the language, and C + + structure can contain functions!

- -----------------------------------------------------

we are Learning C + + Why review C language What about the structure?

In the textbook I've read about C + +, the opening is often two words "object"! Because C language is process-oriented, and C + + features are object-oriented! So most of the books are talking about object-oriented things at the very beginning.

But in the course of my initial study I found myself on the term "object" .... Always want to biased, to this word foggy, what call object-oriented Ah, saw a pass did not understand. So let's not delve into what is called object-oriented.

Let's take a look at the structure of the C language first. For this people should be very familiar with, if the C language structure of the concept of vague, please consult the information yourself.

-----------------------------------------------------

C language Structural Body

In C programming, we define our own height, can use int variable, define the height of a family (such as three people), can use int type array.

If we want to show the names, height (cm), weight (in kg) of each character in a game, these three kinds of data are not the same type, then we use the structure.

struct hero

{

unsigned char name[10];

int height;

float weight;

};

struct hero Luban;//define "Luban" variable

struct or structure, just like int, float and other basic types, is also a kind of data type, which allows the user to define it, allowing different types of data items to be stored, like the hero structure above. It's like packaging a package that encapsulates variables that have common characteristics (the height and weight of the same character) inside, and access and modify these internal variables in a certain way.

We don't say much about the knowledge of more structures.

We define some variables within the structure and see the structure of "Luban", and we can find out his height, weight and other data.

----------------------------------------------------

Function

The Luban character has the ability to "puffer fish grenades," like cooking, oil, salt, vegetables, eggs ... Out of the pot! In such a sequence, a meal was completed, and Luban completed the skill in one order. Left-Flash, right-to-hide ... Kill!

This is a function.

A variable defines the character character of a role, and a function defines the process of a role's big strokes! So the process of C language is very well understood.

This luban we can call an Object! The variables we call "attribute" or "state" in his height, weight, and so on, what he can do, what we call "behavior".

Like Luban, this character has a name, gender, height, weight, and so on these "quantities", which is the variable = attribute = state; he can eat, sleep, play strange, these behaviors = functions. So, we call the variable in C as "attribute" or "state", and the function in C is called "behavior".

------------------------------------------------------

from structure to class

In the C language structure definition, we just put the variables in the structure, and C+ + class is the C language of the structure of the extension , c+ + The struct can contain functions!

Give me a chestnut:

struct hero

{

Property

unsigned char name[10];

int height;

float weight;

Behavior

void Eat ()

{Eat meat! }//Only instructions

void Sleep ()

{Stand and sleep! }

void Killmonsters ()

{Use a Blowfish grenade to play the blame! }

};  // semicolon

struct hero Luban;//define "Luban" variable

Call:

void Main ()

{

luban.height=150;

luban.weight=50;

Luban.killmonsters ();

}

The "role" of the struct member, and C language, we have both variables and functions, variables and functions belong to the structure, variables called member variables, functions called member functions. The structure of C language intelligently describes an object's properties and cannot describe the behavior of an object.

In object-oriented programming, objects have attributes (states) and behaviors, attributes are persisted in member variables, and behaviors are implemented through member methods (functions). What are the member methods?

In fact, methods and functions can be understood as the same thing. Only in C + + "functions" are called "methods".

-----------------------------------------------------

C the definition of the + + class

See in front of, this place does not speak more!

will be the keyword struct in the struct definition is changed to class, and you get C the definition of class in + +.

Class Hero//name used to capitalize the first letter

{

Property

unsigned char name[10];

int height;

float weight;

Behavior

void Eat ()

{Eat meat! }//Only instructions

void Sleep ()

{Stand and sleep! }

void Killmonsters ()

{Use a Blowfish grenade to play the blame! }

};  // semicolon

with class defines the Just call object.

H Ero Luban; //Definition the One H objects of the Ero class L Uban

C + + Notes 004:c++ class popular Point said

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.