[C++]学习札记2015-08-21

来源:互联网 发布:网络诈骗安全选择题 编辑:程序博客网 时间:2024/05/21 09:45

cint, cout可以自动判定数据类型;

C++中结构体可以包含函数;

结构体(struct)是一种特殊的类(class),前者默认为public,后者默认为private。

#include <iostream>
using namespace std;
//struct Point
class Point
{
public:
int x;
int y;
//void init()
//{
// x=0;
// y=0;
//}
Point()//构造函数,用来创建对象,分配内存。
{
x=0;//可带参数,初始化。
y=0;
}
Point(int a,int b)//函数的重载。
{
x=a;
y=b;
}
~Point()//析构函数,释放内存。
{
}
void output()
{
cout<<x<<endl<<y<<endl;
}
void output(int x,int y)
{
this->x=x;//每当产生一个对象,都产生一个隐含指针,
this->y=y;//指向这个对象。
cout<<x<<endl<<y<<endl;
}
};


void main()
{
Point pt(3,3);
pt.output(5,5);
//  cout<<pt.x<<endl<<pt.y<<endl;
// getchar();
}

0 0