On C + + class and Object < a >

Source: Internet
Author: User

Classes and objects are a feature of the C + + language, which is an abstraction of an object that is an instance of a class.

A class is an abstraction that takes up memory, while an object consumes memory when it is instantiated.

Here's an example:

Class Time//define classes {int hour;    Defines the data member int min;    Defines the data member int sec; Define data members}; Time t;

Look at this structure is not a bit familiar with the C language learned the application of the struct is very similar, but there is a difference between the two: if there is no special declaration in the struct, the members of the struct are public members, and class is private privately member by default.

Class out of the data members generally also have its member functions, when it comes to member functions we first think of the constructor, the existence of the constructor is to let us initialize the object (of course, we can also cout<< some statements, convenient for us to debug).

Speaking of initialization, the initialization we've seen in the C language before is this:

int i = 0;

But if you initialize objects like this in C + +, that's absolutely impossible.

Class Time{hour = 0;//initialization of the error min = 0;     Initialization of the error sec = 0; Initialization of the error};

So be sure to use the constructor that we just mentioned.

The function name of the constructor is the same as the class name , which is necessary, necessary, necessary (important thing to say three times), It has no return value and no type , its functionality is user-defined, But can not be called by the user, if the user does not define a constructor, then the compiler will automatically generate a constructor, but the resulting constructor does not actually work, its function body is empty.

class time  //define class {Public:time ()    //define constructors, The function name is the same as the class name {hour = 0;   //uses constructors to initialize data members min = 0;    // Use constructors to initialize data members sec = 0;    //use constructors to initialize data members}void set_time (void);   //function declaration void show_time (void); //  function declaration private:int hour;   //define data member int  min;    //define data members int sec;    //define data members};void time::  set_time (void)   //defines the definition of a member function, setting the time {cin >> hour;cin >> min;cin  >> sec;} Void time::show_time (void)//  member function definition, display time {cout << hour <<  ":"   << min <<  ":"  << sec << endl;} time t;     //The object that defines the class 

The definition of a constructor, in addition to the parameterless constructor in the example above, has a constructor with parameters, and its general format is:

Constructor name (type name 1, parameter 1, type 2, Parameter 2 ...) )

The above example can be rewritten as

Time (int, int, int); Constructors with parameters//define constructors outside of class time::time (int h, int m, int s) {hour = H;min = M;sec = s;}

C + + also provides another way to initialize the data, that is, the parameterized table, the initialization of the above example can be written like this:

Time::time (int h, int m, int s): Hour (h), Min (M), SEC (s) {}

corresponding to the constructor function is the destructor, the function of the destructor is to undo the object occupied by the memory to complete some cleanup work, but not delete the object. Its usage is to precede the class name with an inverse symbol "~", and the contents of the function can be empty.

The destructor for the above example can be written as

~time () {cout << "~time ()" << Endl;}

a A class can have multiple constructors, but there can be only one destructor.

The order of calling constructors and destructors is equivalent to a stack, the first constructs the post-destructor, and the latter constructs the first destructor.

This article from the "10954937" blog, reproduced please contact the author!

On C + + class and Object < a >

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.