C ++ pen training Example 6

Source: Internet
Author: User

# Include <iostream>
Using namespace STD;
Class C
{
Public:
C () {cout <"chuangjiang C" <Endl ;}
C (const C & Other) {cout <"Coping C" <Endl ;}
C & operator = (const C & Other) {cout <"= C" <Endl ;}
};
Class B
{
Public:
B () {cout <"chuangjiang B" <Endl ;}
B (const B & Other) {cout <"Coping B" <Endl ;}
B & operator = (const B & Other) {cout <"= B" <Endl ;}
Int B;
};
Class
{
Public:
A () {cout <"chuangjinag a" <Endl ;}
A (const B & other): BB (other) {cout <"chuangjinag A with B" <Endl ;}
A (int I): B (I) {cout <"chuangjian A with int" <Endl ;}
A (const A & Other) {cout <"Coping a" <Endl ;}
A & operator = (const A & Other) {cout <"= A" <Endl ;}
Int B;
C cc;
B BB;
Void usestatic (){
Static B;
Cout <B. B <Endl;
}
};

A & getrefence (A & ){
Return;
}

A getvalue (A & ){
Return A; // return A ();
}

Int main (){
// Non-static member variables are first initialized and then run the constructor,
A aa (2 );
Cout <& aa <Endl;

// Difference between the return value and the return reference
A bb;
Cout <"difference between return value and return reference:" <Endl;
BB = getvalue (AA );
Cout <& BB <Endl;
BB = getrefence (AA );
Cout <& BB <Endl;

// Reference variables. The variables declared in Java are basically referenced variables.
Cout <"reference variable:" <Endl;
A & CC = getrefence (AA );
Cout <cc. B <"end" <Endl;

// Test when the initialization table is executed. You can change the CC and BB positions for the test.
// The result shows that initialization is performed based on the position of the member variables from top to bottom, whether or not in the initialization table
B;
Cout <"Create a with B:" <Endl;
A AB (B );

// A c = A (1 );
// A d [2] = {A (1), a (1 )};
// A * E = new A [2];
// Cout <E [1]. B <Endl;

// Test the local static variable
Cout <Endl <"test usestatic:" <Endl;
AA. usestatic ();
AA. usestatic ();

Cout <"sizeof B:" <sizeof (B) <Endl;
Cout <"sizeof A:" <sizeof (a) <Endl;

System ("pause ");
}

 

I tested the constructor and did not test the destructor.

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.