#include <iostream.h>
class POINT
{
public:
int x;
int y;
POINT()//构造函数
{
x=0;
y=0;
}
void output()
{
cout<<x<<endl<<y<<endl;
}
};
void main()
{
POINT pt;//在定义类的变量时自动调用构造函数
pt.output();
}
1.类里面默认的成员为私有,结构体里面的成员变量为公有
2.构造函数,在main函数定义类变量时,自动调用构造函数
3.析构函数,不允许带参数,并且一个类中只能存在一个析构函数
4.函数的重载,我们希望在构造pt这个对象的同时,传递x坐标和y坐标的值.可以再定义一个构造函数.两个构造函数
#include <iostream.h>
class POINT
{
public:
int x;
int y;
POINT()//构造函数
{
x=0;
y=0;
}
POINT(int a,int b)
{
x=a;
y=b;
}
void output()
{
cout<<x<<endl<<y<<endl;
}
};
void main()
{
POINT pt(5,5);//在定义类的变量时自动调用构造函数
pt.output();
}
C++编译器将根据参数的类型和参数的个数来确定执行哪一个构造函数,如上执行POINT(int a,int b)
重载的条件:函数的参数类型,参数个数不同,才能构成重载 只有返回类型不同是不能构成重载的
this 指针 它是一个隐含的指针,在调用对象时,成员函数除了接受2个实参外,还接受了pt对象的指针
#include <iostream.h>
class POINT
{
public:
int x;
int y;
POINT()//构造函数
{
x=0;
y=0;
}
POINT(int a,int b)
{
x=a;
y=b;
}
void