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

时间:2015-07-20 17:14:08

标签: c++ oop

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

这是类的图表

enter image description here

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

错误发生在朋友ostream&运算符<<(ostream& os,const CourseWork& dt)函数。

这是我的课程

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

2 个答案:

答案 0 :(得分: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);
}

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

答案 1 :(得分:1)

打印派生类的一种方法是在基类virtual中创建Grading成员函数,在必要时创建重写成员函数是派生类,并使用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;
}

operator<<std::ostream之间为Grading创建非成员函数。

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