Chicken Peck Rice: C + + programming 13 learning the same as object, class declaration, access control for members

Source: Internet
Author: User
Tags types of functions

1. This study of Chicken peck Rice Course 13th, the more important to record the study to urge themselves to learn better. Recommend their Web site to learn: http://www.jizhuomi.com/school/c/97.html

2. In the process-oriented design, the module of the program is composed of functions, and the object-oriented design of the program module is a class, the function is only statements and data encapsulation, and the class is a function and data encapsulation, after a period of C + + to understand the meaning of this sentence. The class is actually a custom data type. A variable of a class type can be defined, a variable defined by a class is called an object of the class, and the process of defining an object is called instantiation.

3. But there is always a doubt that functions can be in the class? Like the C language, does this function belong to any class?

4. Declarations of classes

1 classClock2 {3          Public:                                                                              4                     voidSetTime (intNEWH,intNEWM,intNewS); 5                     voidShowTime (); 6         Private:                                                              7                     intHour,minute,second; 8};

5. The function is declared inside the class, and then the concrete implementation is placed outside the class, to add a:

1 voidClock::settime (intNEWH,intNEWM,intNewS)2 {3Hour=NEWH;4Minute=newm;5Second=NewS;6 }7 voidclock::showtime ()8 {9cout<":"<<Minute<<":"<<Second<<Endl;Ten}

6. Not sure about the << grammar above? Go and search.

cout<< "hello! \ n "; is a statement, and each statement ends with a semicolon. Cout is a predefined object in the C + + standard library, and its information is placed in the previously included header file Iostream.h, which is an output stream object, using the operator << to output the following string to a standard output device, typically a display.

7. Access control for class members

The control of a class member's access rights is achieved by setting the access control properties of the member. There are three types of access control properties: Public, private, and protection (protected).

The public type declares the outer interface of the class. Public-type members are declared with the Common keyword. External access must be done through an external interface. For example, for the clock class, external want to view or change time can only be achieved by SetTime and Showtime two public types of functions.

The private type member of the class is declared after private. If the access control attribute is not indicated, the default is private. For example, in the class clock declaration, if the public is not there, then both the SetTime and Showtime functions are private by default. Private type members can only be accessed by member functions in this class and cannot be accessed externally. Hour, Minute, and second are all private type members in the Clock class.

8. A simpler member function of a class can also be declared as an inline function, which, like a normal inline function, inserts the function body of an inline function into each place where it is called. There are two ways to declare an inline function: implicit and explicit declaration. It is implicitly declared that the function body is placed directly within the body of the class. In fact, it is more strange, inside the class is the inline function? Inline functions should consume more space.

9. Open the VS2010 test.

Chicken Peck Rice: C + + programming 13 learning the same as object, class declaration, access control for members

Related Article

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.