注:最后有面试挑战,看看自己掌握了吗
结构体和类
- C++结构体中可以有函数。
- 称为成员函数
#include <iostream>
struct point
{
int x;
int y;
void output()
{
std::cout<<x<<std::endl<<y;
}
};
void main()
{
point pt;
pt.x=0;
pt.y=0;
pt.output();
}
- C++中,结构体使用关键字struct声明的类
- 差异:结构体默认成员公有public
- 类:默认成员为private
构造函数
- 构造函数名字一定和类名相同
#include <iostream>
using namespace std;
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();
point cc
C++基础与面向对象特性详解

最低0.47元/天 解锁文章
4162

被折叠的 条评论
为什么被折叠?



