构造函数的存在意义
#include<iostream.h>
class myclass
{
int a,b;
public:
void my(int x,int y)
{
a=x;
b=y;
}
void show()
{
cout<<"a+b="<<a+b<<endl;
}
};
void main()
{
myclass obj1,obj2;
obj1.my(0,0);
obj1.show();
obj2.my(100,50);
obj2.show();
}
和
#include<iostream.h>
class myclass
{
int a,b;
public:
myclass(int x,int y)
{
a=x;
b=y;
}
void show()
{
cout<<"a+b="<<a+b<<endl;
}
};
void main()
{
myclass obj1(0,0);
obj1.show();
myclass obj2(100,50);
obj2.show();
}
的结果都一样,那还需要构造函数干嘛?构造函数的存在意义又是什么