Classes in C + +

Source: Internet
Author: User

Summary: When looking at QT, where: in the. cpp file (the source file for the Class)---Implement the slot and declare the slot in the. h file. So I intend to learn as much as possible about the classes in the C + + language in this essay.


I.---classes and objects in section 10.3 of the Wu Guofeng and Wu Shanli Editor in the C + + programming code1. Declaration of class and object definition 1) class

The class is actually a new type of data. It is the combination of data and actions that work on the data (the basic tool for encapsulation and data hiding).

1 //description of a date class2 3 classcdata{4      Public:    5         voidSetDate (Inty, Intm,intd)6         intisleap year ();7         voidprint ();8     Private:9         intYear,month,day;Ten  One     protected: A         ....; -  -};


    • A class is a user-defined data type that does not allow initialization of a defined data member because it does not have storage space later. The data and functions defined in a class are called data members and function members.
    • Access control properties: Define private permissions by default

Public: declares the outer interface of a class, which is a member that an external object of the class can access.

Private: allows access only to members of this class, and any access outside the class is illegal .

Protection (Protected): Similar to private types, the difference is that the new class access rights generated during the inheritance process are different .

2) Definition of object
1#include <iostream>2 classrectangle{3     Private:4         intw,h;5      Public:6         voidInput () {cin>>w,h};7         intArea () {returnh*W};8 };9 Ten voidMainvoid) One { A rectangle a,b,c; -cout<<"Please enter the length and width of the first rectangle:"; - a.input (); thecout<<"Please enter the length and width of the second rectangle:"; - b.input (); -cout<<"Please enter the length and width of the third rectangle:"; - c.input (); +     intsum; -Sum=a.area () +b.area () +C.area (); +count<<"the sum of the area of three rectangles is"sum<<Endl; A}

Note: A. "." The operator is used between the object and the member; the "::" operator is used between the class and its members;

   B. Encapsulation of a class---public, protected members in an object are not allowed to be accessed directly by non-member functions.

2. Constructors and destructors
1       classClock2         {3          Public:4Clock (intNEWH,intNEWM,intNewS);//constructor Function5                   voidSetTime (intNEWH,intNEWM,intNewS);6                   voidShowTime ();7         Private:8                   intHour, Minute, Second;9         };Ten  One the implementation of the constructor: A  -Clock::clock (intNEWH,intNEWM,intNewS) -         { theHour=NEWH; -Minute=newm; -Second=NewS; -         } +  - constructor functions when an object is created: +  A         intMain () at         { -Clock C (0,0,0);//implicitly calls the constructor and takes the initial value as an argument.  - (); -                 return 0; -}

Description: The constructor----initializes the object with the given value when the object is created. The function that the system calls automatically when it is created.

    • A function member with the same name as a class is called a---constructor, and its purpose is to initialize the object.
    • Cannot specify return type (including void type)
    • A class can define multiple constructors, with parameters or without parameters;
    • If there is no constructor in the class, then the compiler will automatically create a parameterless constructor;
    • The constructor is automatically called when the object is created


is a function that is automatically called by the system when an object is disposed.  function--- do a series of cleanup work before releasing an object.  Usually used to do the cleanup work of the Undo object. such as releasing memory allocated by the constructor

Classes in C + +

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: 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.