2015-07-20 69 views
0

我有幾個不同的類數據類型的向量,我試圖打印派生類的變量。如何訪問C++中派生類中的變量?

這裏是類

enter image description here

我已在圖實施的示意圖。我試圖在評分班的作業班打印成績。

的錯誤是在朋友的ostream &操作者< <(ostream的& OS,常量課程& 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; 
} 
+1

是的,但是錯誤信息本身是什麼? – Snappawapa

+0

錯誤:'Grading'中沒有名爲'letter_grade'的成員 – Pete

+1

這意味着clas'Grading'沒有任何名爲'letter_grade'的東西,它看着你的代碼是真的。你正試圖訪問'grading'中的東西 – Snappawapa

回答

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

或者,如果你真的想訪問派生類的成員,然後你需要投。但是改用虛擬方法。打印的派生類

+0

謝謝你的幫助。這工作。 – Pete

1

的一種方法是在基類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<<非成員函數。

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

我無法使用此方法工作,但感謝您的幫助。 – Pete