gpt4 book ai didi

c++ - 如何在 C++ 中访问派生类中的变量?

转载 作者:行者123 更新时间:2023-11-28 02:23:21 25 4
gpt4 key购买 nike

我有几个不同类数据类型的 vector ,我正在尝试打印派生类变量。

这是类图

enter image description here

我已经实现了图表。我正在尝试打印评分类作业类的分数。

错误在 friend ostream& operator<<(ostream& os, const CourseWork& dt)函数中。

这是我的类(class)

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iomanip>
using namespace std;


/* -------------------------------------------------------- */
/* ---------------------- Grading Class ------------------- */
/* -------------------------------------------------------- */
class Grading
{
public:
string name;
int percent;

void get_raw_score();
void get_adj_score();
};

/* -------------------------------------------------------- */
/* ---------------------- Assignment Class ---------------- */
/* -------------------------------------------------------- */
class Assignment : public Grading
{
protected:
int score;
};

/* -------------------------------------------------------- */
/* ---------------------- Exam Class ---------------------- */
/* -------------------------------------------------------- */
class Exam : public Grading
{
protected:
int score;

Exam(string n, int g, int s) {
name = n;
percent = g;
score = s;
}
};



/* -------------------------------------------------------- */
/* ------------------- Project Class ---------------------- */
/* -------------------------------------------------------- */
class Project : public Assignment
{
public:

string letter_grade;

Project(string n, int g, string l_g) {
name = n;
percent = g;
letter_grade = l_g;
}
};

/* -------------------------------------------------------- */
/* ---------------------- Quiz Class ---------------------- */
/* -------------------------------------------------------- */

class Quiz : public Exam
{
public:

string letter_grade;

Quiz(string n, int g, string l_g) : Exam(n, g, score)
{
name = n;
percent = g;
letter_grade = l_g;
}
};

/* -------------------------------------------------------- */
/* ---------------------- CourseWork class ---------------- */
/* -------------------------------------------------------- */

class CourseWork {

public:
CourseWork() {}

void push_back(Quiz * a) {
work.push_back(a);
}

void push_back(Exam * a) {
work.push_back(a);
}

void push_back(Project * a) {
work.push_back(a);
}

// print the data and sort by name
void sort_name() {
for (int i = 0; i < (int)work.size(); i++)
cout<< work.at(i)->name <<endl;
}

void sort_score() {

}

friend ostream& operator<<(ostream& os, const CourseWork& dt) {

cout << "Grading" << std::setw(20) << "Percentage" << std::setw(20) << "Raw-Score" << endl;
for (int i = 0; i < (int)dt.work.size(); i++) {
// cout << dt.work.at(i)->name << std::setw(20) << dt.work.at(i)->percent << dt.work.at(i)->score <<endl;

os << dt.work.at(i)->name << std::setw(20) << dt.work.at(i)->percent << dt.work.at(i)->letter_grade;
}

return os;
}


private:
vector<Grading*> work;
};

/* -------------------------------------------------------- */
/* ---------------------- MAIN ---------------------------- */
/* -------------------------------------------------------- */

int main () {
CourseWork c;

c.push_back(new Quiz("Quiz", 5, "B-"));
c.push_back(new Quiz("Quiz", 5, "C+"));
c.push_back(new Quiz("Quiz", 5, "A"));
// c.push_back(new Exam("Midterm", 10, 50));
// c.push_back(new Exam("Final", 30, 85.5));
// c.push_back(new Project("Project", 5, "A-"));
// c.push_back(new Project("Project", 15, "B-"));
// c.push_back(new Project("Project", 15, "B-"));
// c.push_back(new Project("Demo", 10, "C"));

cout << "** Showing populated data..." << endl;
cout << c << endl << endl;;

// c.sort_name();
// c.sort_score();

return 0;
}

最佳答案

您正在将 Grading* 对象存储在您的 CourseWork 对象中:

vector< Grading* > work;

所以不能通过基类的指针访问派生类的成员。你应该在你的基类中引入一个新的(纯)虚函数,它应该打印派生类的参数。

class Grading
{
public:
virtual ~Grading() {}

virtual print() const = 0;

// ...
}

并且您应在所有派生类中实现它。

如果将给定参数添加到同一个 vector 中,创建这些函数也没有意义:

void push_back( Quiz* a )
{
work.push_back(a);
}

void push_back( Exam* a )
{
work.push_back(a);
}

void push_back( Project* a )
{
work.push_back(a);
}

你只需要一个功能:

void push_back( Grading* a )
{
work.push_back(a);
}

或者如果你真的想访问派生类的成员,那么你需要强制转换。但是请改用虚拟方法。

关于c++ - 如何在 C++ 中访问派生类中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31522415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com