Thinking about a program 5 (c + + various function calls)

Source: Internet
Author: User

/**********************************************************************
* Main.cpp
* compiler:gcc,vs,vc6.0 Win32
* AUTHOR:WK
* TIME:2015 3 29
************************************************************************/
#include <iostream>
using namespace Std;
Class Coord
{
Public
Coord (int a=0,int b=0);
Coord (const Coord &p);
~coord ();
void print ();
int Getx ();
int gety ();
Private
int x, y;

};
void Coord::p rint ()
{
cout<<x<< "<<y<< ' \ n ';
}
int Coord::getx ()
{
return x;
}
int Coord::gety ()
{
return y;
}
Coord::coord (int a,int b)
{
X=a;
Y=b;
cout<< "Here just to verify that the constructor is running \ n";

}
Coord::~coord ()
{
cout<< "Here just to verify that the destructor is running \ n";
}
Coord::coord (const Coord &p)
{
x=p.x;
Y=P.Y;
cout<< "Here just to verify that the copy constructor is running \ n";

}

Common functions
Coord Fun (Coord p)
{
cout<< "Here just to verify that the fun () function has run \ n";//5. Print out
int A, B;
A=p.getx (+10);
B=p.gety (+20);

Coord R (A, b);//6. CallCustom ConstructorsAllocates memory to and initializes data members of object R
return R; 7. Call the value of R hereCustom Copy ConstructorsThe Nameless function assigned to the system is used to return

}
8. Call when exiting the fun () functionCustom destructorRelease formal parameter P
9. Function callCustom destructorReleasing objects defined in the function body R
10. CallCustom destructorFrees a nameless object generated by the system itself


int main ()
{
Coord P1 (30,40);//1. CallingCustom ConstructorsAllocates memory to and initializes the data member of the object P1
Coord P2; 2. CallCustomizing default constructorsAssigning memory to an object P2 data member and initializing it to a default value
Coord P3 (p1); 3. CallCustom Copy ConstructorsInitialize P3 with P1 and allocate memory to P3 object data members
P2=fun (p3); 4. Enter the fun () function to first call the argument P3 objectCustom Copy ConstructorsInitialize the formal parameters of fun ()
Here P2 accepted the value returned by the Nameless object.
P2.print ();//11. Output
return 0;

}//exits the main () function and then, in turn, invokes the custom destructor to release the P3,P2,P1 separately.




Thinking about a program 5 (c + + various function calls)

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.