2.1从结构到类
//结构体的定义
#include<iostream.h>
struct point
{
int x;
int y;
};
void main()
{
point pt;
pt.x=0;
pt.y=0;
cout<<pt.x<<endl<<pt.y<<endl;
}
/cin和cout不必考虑输入和输出数据类型,比scanf和printf简单多。因为用到了c++标准输入和输出流,所以要用到iostream.h头文件。endl在输出流中插入一个换行,并刷新输出缓冲区。
//类的定义
struct point
{
int x;
int y;
void output()
{
cout<<pt.x<<endl<<pt.y<<endl;
}
};
void main()
{
point pt;
pt.x=0;
pt.y=0;
pt.output;
}
//在C语言结构中,不能有函数。C++可以有,并称之为成员函数。
class point
{
int x;
int y;
void output()
{
cout<<pt.x<<endl<<pt.y<<endl;
}
};
//结构体默认情况下,其成员是公有的;类默认情况下,其成员是私有的,不能访问和赋值