【问题标题】:How to access a variable in the derived class in c++?如何在 C++ 中访问派生类中的变量?
【发布时间】:2015-07-20 17:14:07
【问题描述】:

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

这是类的示意图

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

错误在friend ostream& operator

这是我的课

#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;
}

【问题讨论】:

  • 是的,但是错误信息本身是什么?
  • 错误:“评分”中没有名为“letter_grade”的成员
  • 这意味着类Grading 没有任何称为letter_grade 的东西,这看你的代码是正确的。您正在尝试访问 grading 中不存在的内容
  • Grading 中确实没有名为 letter_grade 的成员。你为什么要访问它?

标签: c++ oop


【解决方案1】:

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

vector< Grading* > work;

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

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

    virtual print() const = 0;

    // ...
}

你应该在你的所有派生类中实现它。

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

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);
}

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

【讨论】:

    【解决方案2】:

    打印派生类的一种方法是在基类Grading中创建virtual成员函数,必要时创建覆盖成员函数是派生类,并在非成员中使用virtual成员函数功能。

    Grading:

    virtual ostream& write(ostream& os) const
    {
       return os << this->name << std::setw(20) << this->percent;
    }
    

    Project:

    virtual ostream& write(ostream& os) const
    {
       // Use the immediate base class to call the base
       // class implementations first.
       // Then write the data appropriate for this class.
       return Assignment::write(os) << this->letter_grade;
    }
    

    std::ostreamGrading 之间为operator&lt;&lt; 创建一个非成员函数。

    ostream& operator<<(ostream& os, const Grading& gr) 
    {
       return gr.write(os);
    }
    

    使用上面写出CourseWork的函数中的非成员函数。

    friend ostream& operator<<(ostream& os, const CourseWork& dt) {
        os << "Grading" << std::setw(20) << "Percentage" << std::setw(20) << "Raw-Score" << endl;
        for (int i = 0; i < (int)dt.work.size(); i++) {
           os << *(dt.work.at(i)) << std::endl;
        }
        return os;
    }
    

    【讨论】:

    • 我无法使用此方法,但感谢您的帮助。
    猜你喜欢
    • 2011-07-14
    • 2021-08-15
    • 2019-05-05
    • 2016-01-05
    • 2018-06-26
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多