C ++ basic knowledge Summary-class advanced knowledge points

Source: Internet
Author: User

C ++ basic knowledge Summary-class advanced knowledge points
In other words, we continue to learn C ++ in simple code. This section mainly describes the advanced knowledge of the learning class. i. when creating an object, constructor and destructor often need initialization, such as assigning values to data members. To solve this problem, C ++ provides constructors. Constructor is a special member function. It has the same name as the class name and has no returned value. It does not need to be called by users (nor can it be called by users ), it is automatically executed when an object is created. A constructor is used to initialize an object. The most common function is to assign values to member variables. When an object is created, the system automatically calls the constructor for initialization. Similarly, when an object is destroyed, the system automatically calls a function to clear the object (for example, reclaim various resources consumed during object creation). This function is called an destructor. Destructor is also a special member function. It does not return values and does not need to be called. Instead, it is automatically executed when an object is destroyed. Different from the constructor, the name of the constructor is preceded by a Class Name "~ "Symbol.

# Include <iostream> using namespace std; class Student {public: // constructor, use the parameter initialization list to initialize Student (string name1, int age1, float score1 ): name (name1), age (age1), score (score1) {}// destructor ~ Student (); // void say (); private: string name; int age; float score ;}; Student ::~ Student () {cout <name <"goodbye" <endl;} void Student: say () {cout <name <"indicates the age of" <age <", and the score is" <score <endl;} int main () {Student stu1 ("James", 15, 90.5f); stu1.say (); Student stu2 ("Li Lei", 16, 95); stu2.say (); Student stu3 ("Wang Shuang ", 16, 80.5f); stu3.say (); cout <"the main function is about to run." <endl; return 0 ;}

 

II. We know that the reference of a variable is the alias of the variable. Essentially, both the variable name and reference name point to the same memory unit. If the parameter is the reference name of the variable and the real parameter is the variable name, instead of opening up a bucket (often called a copy of a real parameter) for the form parameter, the address of the real parameter variable is passed to the form parameter (reference name), so that the reference name also points to the real parameter variable.
# Include <iostream> using namespace std; class Time {public: Time (int h, int m, int s): hour (h), minute (m), sec (s) {} int hour; int minute; int sec ;}; // void fun (const Time & t) {std: cout <t. hour <t. minute <t. sec;} int main () {Time t1 (10, 13, 56); // directly transfer the object fun (t1); return 0 ;}

 


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.