#include <bits/stdc++.h>
using namespace std;
class Student {
private:
int id;
string name;
double score;
public:
Student(int d, string ame, double core): id(d), name(ame) //不同的方式。
{
score = core;
}
void print() {
cout << "学号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "分数:" << score << endl;
}
};
int main () {
Student s(2021, "FH", 90);
s.print();
return 0;
}
常规点
#include <bits/stdc++.h>
using namespace std;
class Student {
private:
int id;
string name;
double score;
public:
Student(int d, string ame, double core)
{
id=d;
name = ame;
score = core;
}
void print() {
cout << "学号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "分数:" << score << endl;
}
};
int main () {
Student s(2021, "FH", 90);
s.print();
return 0;
}
如果不是拷贝构造函数好像会出错。