你这个程序中有struct
point{
void
main()
{...}};中的函数名main不对,应该是display(),即:
#include
using
namespace
std;
struct
point{
void
setxy(double
a,double
b)
//你这里有打错字了是void不是viod
{x=a;y=b;}
void
display(
)
{cout<
x,y;
};
void
main(){
point
a;
a.setxy(10.6,18.5);
//
少了分号
a.display(
);
cout<
#include
是包含头文件是用的,这句话就表明这个程序中包含
using
namespace
std;:是使用(using)命名空间(namespace)std(标准库中的东西都放在std中,这句话表明可以使用std中的一切东西(本程序中只用了std::cout,
std::endl
两个);
struct
结构体(在C++中它等同于class<类>,有一个小小的区别就是,不说明访问权限时struct默认的是public,class
默认的是private),这个结构体的名字是point,point中定义了两个成员函数:一个名为setxy,无返回值的函数;一个名为display,无返回值的函数;两个函数数据成员:一个是double
x,一个是double
y;
接下来的是主函数mian(),主函数中定义了一个point类型的自定义变量a;
a.setxy(10.6,
18.5);表示a调用(point类型对象特有的)函数setxy,从而把10.6赋值给a.x(a的x值),18.5赋值给a.y;
a.display();表示a调用display(),从而把a.x,"\t",
a.y,
endl输出:即,在黑屏中显示10.6
18.5
cout<
还有,程序的执行入口时main()函数,所以最先执行的是point
a;而在这句中用用到point类型,于是就执行struct
point类来为a分配内存,是a有效;下来执行a.setxy(10.6,
18.5);这句,上面说了意思了!因为里边没有循环,判断语句,所以他们的执行顺序是从上到下!
注意一点:
不管是什么什么程序都是从main开始的...
从main开始阅读程序。
struct
point{
viod
setxy(double
a,double
b)
{x=a;y=b;}
void
main(
)
{cout<
x,y;
};
void
main(){
//从这里开始
point
a;
//申请了一个point的类,也可以说是一个结构体,上面的代码//中有,struct
point
,它的里面是一个函数叫setxy();
a.setxy(10.6,18.5)
//a是point的实体,函数的引用。
a.display(
);
//引用point里的display().
cout<
执行主函数main>>point
a>>读取point结构体>>a.setxy(10.6,18.5)>>调用setxy函数>>a.display()>>调用显示函数(你在point结构体里写的main()应该是display(),你写错了)>>cout<