类class
类和成员函数(类外定义与类内定义)
#include <bits/stdc++.h>
using namespace std;
class student {
public:
int name = 453;
string c = "ASFASDF";
void display() {
cout << name << endl;
cout << c;
}
int max(int a);
} stu1;
int student::max(int a) {
cout << a + 3 << endl;
}
inline int en(int a) {
return 1;
}
int main() {
stu1.display();
cout << en(4)<<endl;
cout<<stu1.max(stu1.name);
return 0;
}
含有构造函数的类声明,以及定义类对象的注意事项
#include <iostream>
#include <string>
using namespace std;
struct student {
int name = 44;
};
string en(const std::string a) {
return a;
}
class stu {
public:
int id;
int num;
string a;
stu(int , int,string );
};
//含有构造参数的类不能在结束末尾直接定义
//class stu {
//public:
// int id;
// int num;
// string a;
// stu(int , int,string );
//}s1; 错的
stu::stu(int x, int y,string z) {
id = x;
num = y;
a=z;
}
int main() {
const string c = "sdafsa";
stu s1 (12, 45, "fdsaf");
cout << en(c)<<s1.id;
return 0;
}