//构造函数
#include <iostream.h>
class Clock
{
public:
Clock(int NewH,int NewM,int NewS);
Clock();
void SetTime(int NewH=1,int NewM=1,int NewS=1)
{
Hour=NewH;
Minute=NewM;
Second=NewS;
}
void ShowTime()
{
cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
}
private:
int Hour,Minute,Second;
};
//成员函数的具体实现
Clock::Clock(int NewH,int NewM,int NewS)
{
Hour=NewH;
Minute=NewM;
Second=NewS;
}
Clock::Clock()
{}
//主函数
void main()
{
Clock myclock(2,2,2);
myclock.ShowTime();
myclock.SetTime(8,20,20);
myclock.ShowTime();
Clock c;
c.SetTime();
c.ShowTime();
}
****************************************************
//拷贝构造函数
#include <iostream.h>
class point
{
public:
point(int x,int y){X=x;Y=y;cout<<"object constructed!"<<endl;}
point(point &p);
~point(){cout<<X<<","<<Y<<"object destroyed!"<<endl;}
int getx(){return X;}
int gety(){return Y;}
private:
int X,Y;
};
point::point(point &p)
{
X=p.X;
Y=p.Y;
cout<<"copy_constructor called!"<<endl;
}
//型参为point类对象的函数
void fun1(point p)
{cout<<p.getx()<<","<<p.gety()<<endl;}
//返回值为point类对象的函数
point fun2()
{
point A(1,2);
return A;
}
//主函数
void main()
{
point A(4,5);
cout<<A.getx()<<","<<A.gety()<<endl;
point B(A);
cout<<B.getx()<<endl;
fun1(B);
B=fun2();
cout<<B.getx()<<","<<B.gety()<<endl;
}