C + + and objects (1)

Source: Internet
Author: User
Tags function prototype

In the next phase, I will explain the object-oriented parts of C + + and the most important part of C + + for the improvement of the language. Previously, C + + was also called "C with Class". Today, we mainly talk about the composition of classes, the definition and use of member functions and objects.

1. In fact, this section, for the development of C # People, is simply very familiar with AH. C + + class composition, I want to start with the C structure. Structure in C I think everyone is familiar with it.

1 #include "stdafx.h"
2 #include <iostream>

4 UsingNamespace std;

6 struct KID
7 {
8 int age;
9 Char*name;
Ten char*sex;
11};

int main ()
14 {
KID1 Kid;
kid1.age=10;
kid1.name= "Rookie_j";
kid1.sex= "Male";
cout<< "Name:" <<kid1.name<<endl<< "Age:" <<kid1.age<<endl<< "Gender:" <<kid1.sex<<endl;
Return0;
21}

But there are drawbacks to structs in C: Any assignment statement in the 1.main function can access members in the struct, but in real life it is not possible to have any data that can be accessed arbitrarily, so the data of the struct in C is unsafe; 2. The data in the struct is separated from the operation of the data, not a encapsulated whole, Therefore, it is difficult to reuse the program, which affects the efficiency of software production, so the concept of class is introduced in C + +.

The general format of a class in C + + is:

Class Kid

{

Private

int age; Private members

Char *name;

Char *sex;

Public://Members

void Setkid (int age,char *name,char *sex);

void Showkid ();

};

In C + +, the default is private if the members of the class are not privately owned, protected by protected, or publicly public. For C + + structs, members can be private, protected, or public, but the default is public; It is also important to note that data members cannot be assigned values in the declaration of a class, such as:

Class Kid

{

Private:

int age=10;

Char *name= "Rookie_j";

Char *sex= "male";

};

In general, data members of a class should be declared private, and member functions declared as shared. In this way, the internal data is hidden in the class and cannot be accessed directly outside the class, so that the data is effectively protected. The public member function becomes an interface that communicates with the outside of the class.

There are two types of member functions in 2.c++, one for normal member functions:

1 class Kid
2 {
3 Private:
4 int age;
5 Char*name;
6 char*sex;
7 Public:
8 void Setkid (int age,char*name,char*sex);
9 void Showkid ();

11};


void Kid::setkid (int age,char*name,char*sex)
15 {
Kid::age=age;
Kid::name=name;
Kid::sex=sex;
19}

$ void Kid::showkid ()
22 {
cout<< "Name:" <<name<<endl<< "Age:" <<age<<endl<< "Gender:" <<sex <<endl;
24}

Some points to note: 1. The scope operator "::" should be added between class name and function name to declare which class the member function belongs to, if there is no class name before the function name, or there is no class name and no scope operator "::", such as:: Showkid () or Showkid (), Then this function does not belong to any class, not the member function, but the ordinary function; 2. In the declaration of a class, the parameter table of the member function prototype can not describe the name of the parameter, but only its type, but when it is defined outside the class, it must explain both the parameter type and the parameter name;

The other is an inline member function, which is also explicitly declared and implicitly declared:

Implicit declaration:

1 class Kid
2 {
3 Private:
4 int age;
5 Char*name;
6 char*sex;
7 Public:
8 void Setkid (int age,char*name,char*sex)
9 {
Ten Kid::age=age;
One by one kid::name=name;
Kid::sex=sex;
- }
void Showkid ()
{
cout<< "Name:" <<name<<endl<< "Age:" <<age<<endl<< "Gender:" <<sex <<endl;
+ }

19};

Because inline member functions of this definition are not declared with the keyword inline, they are called implicit definitions;

Explicit declaration:

1 class Kid
2 {
3 Private:
4 int age;
5 Char*name;
6 char*sex;
7 Public:
8 inline void setkid (int age,char*name,char*sex);
9 inline void Showkid ();
10};


inline void kid::setkid (int age,char*name,char*sex)
14 {
Kid::age=age;
Kid::name=name;
Kid::sex=sex;
18}

inline void Kid::showkid ()
21 {
cout<< "Name:" <<name<<endl<< "Age:" <<age<<endl<< "Gender:" <<sex <<endl;
23}

The invocation of the inline function is the extension of the code, not the operation of the call of the general function, but note that inline functions defined using inline must have both the declaration of the class and the definition of the inline member function in the same file, or the code will not be replaced at compile time;

3. In C + +, the relationship between a class and an object can be likened by the relationship between the data type int and the shaping variable i. int types and class types represent an abstraction, while shaping variables and classes of objects represent specific things. C + + refers to the class's variable as the object of the class, and the object is also known as an instance of the class;

1 class Kid
2 {
3 Private:
4 int age;
5 Char*name;
6 char*sex;
7 Public:
8 inline void setkid (int age,char*name,char*sex);
9 inline void Showkid ();
Ten}kid1,kid2;

It can also be declared after the class is used in the definition object: Kid kid1,kid2; (after declaring a class, it does not accept and store specific values, only as a "template" to generate concrete objects, only after the object is defined, the system allocates storage space for the object to hold the members of the object);

Access to the members of the object can be: 1. Object name. data member name/object name. member function name (parameter), such as Kid1.setkid ("Rookie_j", "male"); 2. Pointers access the members of the object, such as: Class Kid{public:int Age ;}; Kid kid,*ptr; ptr=&kid;cout<<ptr->age//cout<< (*ptr). Age;3. Remember the references (reference) mentioned in the previous section, and you can also access the members of the object by reference: Class Kid{public:int age;}; Kid Kid; Kid &ptr=kid; cout<<ptr.age//cout<<kid.age;

4. Finally, let's use an example to summarize what we're talking about today (development tools: VS2010):

1 #include "stdafx.h"
2 #include <iostream>
3
4 UsingNamespace std;
5
6 struct Struct_kid//structural body
7 {
8 int age; Default public
9 Char*name;
Ten char*sex;
One by one}kid1;
12
Class Kid
14 {
int age; Default Private
16
+ Private://Privately
Char*name;
Char*sex;
20
Public://publicly
inline void setkid (int age,char*name,char*sex);//Display inline
23/*{
Kid::age=age;
Kid::name=name;
Kid::sex=sex;
27}*/
-void Showkid ()//implicit inline
29 {
cout<< "class:" <<endl<< "Name:" <<name<<endl<< "Age:" &LT;&LT;AGE&LT;&LT;ENDL&LT;&L t; " Gender: "<<sex<<endl;
31}
32
}kid2;//Direct Definition Objects
34
35
Kid::setkid-inline void (int age,char*name,char*sex)
37 {
Kid::age=age;
Kid::name=name;
Kid::sex=sex;
41}
42
int main ()
44 {
45//Structural body
kid1.age=10;
Kid1.name= "Rookie_j";
kid1.sex= "Male";
cout<< "Structure:" <<endl<< "Name:" <<kid1.name<<endl<< "Age:" <<kid1.age< <endl<< "Gender:" <<kid1.sex<<endl;
50
Wuyi cout<< "--------------------" <<endl;
52
53//Class
Kid3,*ptr Kid;
Kid &kid4=kid3;
ptr=&kid2;
57
Kid2.setkid (0, "rookie_y", "male");
Kid2.showkid ();
60
cout<< "--------------------" <<endl;
62
63//Pointer call member function
(*ptr) Setkid ("Rookie_y", "female"),//or Ptr->setkid ("Rookie_z", "female");
Kid2.showkid ();
66
cout<< "--------------------" <<endl;
68
69//Object name call member function
Kid3.setkid (Ten, "rookie_x", "male");
Kid3.showkid ();
72
cout<< "--------------------" <<endl;
74
75//Reference call member function
Kid4.setkid ("rookie_x", "female");
Kid3.showkid ();
78
Return0;
80}

Results:


C + + and objects (1)

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.