类与对象
类型 = 类型数据 + 类型操作
访问权限
public: 公共访问权限
private:私有访问权限
protected:受保护的访问权限
学习c++的重点:运行时的bug要在编译期找出来
C++语言与C++中的struct
//c语言//
//声明
struct stu
{
...
};
//定义
struct stu student;
//c++//
//声明
struct stu
{
...
};
//定义
1.struct stu student;
2.stu student;
struct 访问权限默认是public
class 访问权限默认是 private
class People{
public:
int x, y;
};
struct People2{
int x, y;
};
如果想在c语言中直接用结构体名定义变量,需要用到 typedef
//typedef的一般用法 typedef type new_type;
特别的当type为用户自定义类型时,type 和 new_type 可以相同。
用于结构体时
typedef struct stu
{
...
}Stu;
//定义
1.Stu student;
2.struct stu student;
第一个Class小例子
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
using namespace std;
class People{
friend int main();//友元#隔壁老宋
int x, y;
public :
void set(int x);
void say();
};
struct People2{
int x, y;
};
void People::set(int x) {
cout << "set functiona : " << this << endl;
this->x = x;
return;
}
void People::say() {
cout << x << " " << y << endl;
return;
}
int main() {
People a;
People2 b;
a.y = 18432;//友元
cout << "a pbject : " << &a << endl;
a.set(3);
b.x = 4;
a.say();
cout << b.x << endl;
return 0;
}