A The essential __c++ of class objects in C + + language

Source: Internet
Author: User
/********************************************************************************************* program Function: C + + On the---of the essence of the object of class implementation results: 1--experimental results show that class objects in C + + and class objects in the Java language----not the same thing 2--java class objects in C + + are reference types---are equivalent to a pointer, and the class object is not a pointer type location Time: Shaanxi Normal University 2016.4.21 author information: September ****************************************************************************

/#include <iostream> using namespace std;
    The definition class person{Public:person (int id,int age) for the "1" person class;
~person ();
    Private:int ID;
int age;
};
    Implementation of a member function in the "2" Person class::P erson (int id,int age) {this->id=id;
    this->age=age;
    cout<< "I am a constructor for the person class" <<endl;
    cout<< "Age:" <<this->age<<endl;
cout<< "Name:" <<this->id<<endl;} Person::~person () {cout<< "I am a destructor of the person class and I invoke it at the end of the class object lifecycle!"
<<endl; int main (int argv,char** argc) {//"1" Object pointer variable--xiaoming This is a pointer variable that has only 4 bytes of storage space (32-bit machine) to store the first address of the space opened by the New keyword person*Xiaoming=new person (1990,20);
    Cout<<sizeof (xiaoming) << "bytes" <<endl;
    "2" Class object variable, tired is equivalent to a data type, and wangming is a type variable, its size by the data members in this class space to determine the size of the person Wangming=person (199,25);
    cout<< "Because there are only two shape variables in the class of person, the size of its class object memory space should be 8 bytes" <<sizeof (wangming) << "byte" <<endl;
    System ("pause");
return 0;
 }
The results of the program's operation are as follows:

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.