"C + +" C + + Learning Journey (7): class type--Definition of class

Source: Internet
Author: User

New Year's first Bo, Happy New Year!

Never understand the object of the face of what the meaning of the "object", can not imagine, when learning oc Strange Grammar, let the program "self" run; the EDA salon mentions the class type, but I don't know what the hell it is. Finally the answer to these questions to be my own self-taught, haha ha! Class Type I'm coming!

Class is a custom data type, a collection of objects that have the same properties and behaviors, and is one of the most important concepts in object-oriented. There are variables in class types that store properties and data, called member variables or data members , and functions that manipulate these data, called member functions .

The role of the class type:

      • Implementing encapsulation
      • Data hiding
      • Inheritance and derivation polymorphism
      • ......

When using a class, the user does not need to know the specific implementation of the class, only the members of the class can be used with specific access rights through the external interface. PS: A variable declared by a class type is called an object, which means that the object is actually a variable.

There are a lot of things that can not be understood, slowly learn should be able to understand.

The declaration of a class type is in the Class keyword, in the same format as a struct. Full definition format for class type:

1 classclass name2 {3  Public:4 data type member variable name;5 function type member function name (formal parameter list);6     ......7 Private:8 data type member variable name;9 function type member function name (formal parameter list);Ten     ...... One protected: A     ...... -};//";" Don't forget it!  -  the //definition of member functions - function Type class Name:: member function name (formal parameter list) - { - function body; +}

As you can see, there are three types of access rights in the class type: Public access, private access, and protected access (protected).

① Public access rights

member variables and member functions in public access permissions can be accessed from any external function, such as d1.year. Public member functions generally become public interfaces.

② Private access rights

member variables and functions can only be accessed from within the class, not through small dots "." Access from external functions can only be accessed through a common interface defined within the class, which embodies the first feature of C + + object-oriented programming: encapsulation features.

In the definition of a member function, all member operations on the class do not have to use the dot operator, which is cin>>year; instead of cin>>d1.year.

③ protect access (not yet)

A member function of a derived class of a class can directly access the protected member variable that inherits the class, and cannot inherit access to the private member variable, which is different from the private access permission.

If the member variable is private, then a public interface is required to input and output the data, which requires the definition of the accessor, the assignment function. It says a chase, including how to use the value assignment function? Instance:

1 //class Type Preliminary: Date comparison2#include <iostream>3 using namespacestd;4 classDate//Define class type, date is type name5 {6  Public:7     voidInput ();//the public interface of the input8     voidOutput ();//common interface for output9     intGetYear ();//Accessor value functionTen     intGetMonth ();//Accessor value function One     intGetday ();//Accessor value function A Private: -     intYear//Private member Variables -     intmonth; the     intDay ; -}D1,D2;//D1,d2 are objects that can be defined differently, such as date d1,d2; - voiddate::input () - { +cout<<"Please enter the year, month and day:"<<Endl; -cin>>year>>month>>day;//direct access to private variables such as year, month, day, etc. +cout<<"input Complete! "<<Endl; A } at voiddate::output () - { -cout<<year<<"years"<<month<<"Month"<<day<<"Day"<<Endl; - } - intdate::getyear () - { in     returnYear ; - } to intDate::getmonth () + { -     returnmonth; the } * intDate::getday () $ {Panax Notoginseng     returnDay ; - } the intCompare () + { A     if(D1.getyear () ==d2.getyear () &&d1.getmonth () ==d2.getmonth () &&d1.getday () = =d2.getday ())//Compare the data of D1 D2 respectively the     { +cout<<"two dates are the same! "<<Endl; -         return 0; $     } $     Else -     { -cout<<"two different dates, they are:"<<Endl; thecout<<"D1:"; - d1.output ();Wuyicout<<"D2:"; the d2.output (); -     } Wu } -  About intMain () $ { -cout<<"class Type preliminary: comparison date! "<<Endl; - d1.input (); - d2.input (); A compare (); +}

From the above, you can see how it's used. This program runs: Prompt for two dates, and then compare two dates are the same, the same output, not the same output and add the respective date.

Inline member functions are used for short, simple but frequent use of member functions (such as value assignment functions), with explicit and implicit declaration of two types:

1 //an explicit inline function2 classCount3 {4     intT;5  Public:6     voidoutput ();7 };8 inline voidcount::output ()9 {Tencout<<"Little Tao is the most handsome!"<<Endl; One } A  - //implicitly-inline functions - classCount the { -     intT; -  Public: -     voidoutput () +     { -cout<<"Little Tao is the most handsome!"<<Endl; +     } A};

Inline functions perform faster than normal functions, but if the function body is too large, the general compiler discards the inline and calls the function in a general way.

Another this pointer, it refers to the current call of the object , generally write code when omitted, in fact, is actually this->year = y;

The scenario used:

1 void Date::setdate (intintint  d)2{3     (*this  //(*this). Year is a data member Year,year is a formal parameteryear4 }

"C + +" C + + Learning Journey (7): class type--Definition of class

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.