【发布时间】:2021-11-12 01:12:59
【问题描述】:
我的问题是: 编写一个程序,该程序有一个 Student 类来存储该类中学生的详细信息。从 Student 派生另一个班级 Toppers,仅存储班级前 3 名学生的记录
在这里,在继承的类 toppers 中,我获取了派生类 (Toppers) 的数据成员 (recordTop[3]) 并在 top3(toppers a[], int n) 函数中对其进行了初始化,但它给了我对 `toppers::recordTop' 的未定义引用错误。
#include<iostream>
#include<string>
using namespace std;
class students
{
protected:
string name;
int total;
public:
void input_students()
{
cout << "Enter your Name : ";
cin >> name;
cout << "Enter your total marks : ";
cin >> total;
}
void output_students()
{
cout << "Name : " << name << endl;
cout << "Total Marks : " << total << endl;
}
};
class toppers : public students
{
protected:
static toppers recordTop[3];
public :
friend void sort(toppers a[], int n)
{
for (int i = 0 ; i < n ; i++)
{
for (int j = i ; j < n ;j++ )
{
if(a[j].total > a[i].total)
{
toppers temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
}
void top3(toppers a[], int n)
{
recordTop[0] = a[0];
recordTop[1] = a[1];
recordTop[2] = a[2];
}
void output_toppers()
{
for (int i = 0; i<3 ; i++)
{
cout << "Details of RANK " << i + 1 << " are : " << endl;
recordTop[i].output_students();
}
}
};
int main()
{
int n;
cout << "Enter number of students : ";
cin >> n;
toppers st[n];
for (int i = 0; i < n ; i++)
{
st[i].input_students();
}
sort(st,n);
st[0].top3(st, n);
st[0].output_toppers();
return 0;
}
如果可能,请指出我的错误?
【问题讨论】:
标签: c++ oop undefined-reference