Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8201827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:56:52+00:00 2026-06-07T06:56:52+00:00

I have an array of objects which all derive from the class BaseStudent. BaseStudent**studentlist

  • 0

I have an array of objects which all derive from the class BaseStudent.

BaseStudent**studentlist = new BaseStudent*[atoi(listSize.c_str())];

That array is populated with either derived Math, English or History objects. I’m now trying to print out specific data from each object in the array and output it to a file.

for (int j=0; j<atoi(listSize.c_str()); j++){
    if(studentlist[j]->getMT() == ENGLISH){
        output << studentlist[j]->GetFN()<<" "<<studentlist[j]->GetLN();
        output << right << setw(42) << studentlist[j]->GetFinal(); // this is an English public function but I can't call this.
    }
}

I need to be able to access the derived classes private member data from the array of objects.

Here’s my header. As you can see I have a setter and getter for every protected member data.

#include <iostream>
#include <string>

using namespace std;

#ifndef BASESTUDENT_H
#define BASESTUDENT_H

enum MajorType {ENGLISH, HISTORY, MATH};
// *********************************************************************
// Base class. All other classes (Enlish, History, Math) inherit from 
// this class.
// *********************************************************************
class BaseStudent
{
public: 
    BaseStudent();
    BaseStudent(string fn, string ln, string m);
    string GetFN(){return firstName;}
    string GetLN(){return lastName;}
    MajorType getMT(){return course;}
    void SetFN(string fn){firstName = fn;}
    void SetLN(string ln){lastName = ln;}
    void SetMT(string m);

protected:
    string firstName;
    string lastName;
    MajorType course;

}; // End Base class

// *********************************************************************
// Enlish class.
// *********************************************************************

class English: public BaseStudent
{
public:
    English(string fn, string ln, string m, double a, double p, double mt, double f);
    double FinalAverage();
    double GetAttendance(){return attendance;}
    double GetProject(){return project;}
    double GetMidterm(){return midterm;}
    double GetFinal(){return final;}
    double GetFinalAverage(){return finalAverage;}
    void SetAttendance(double a){attendance = a;}
    void SetProject(double p){project = p;}
    void SetMidterm(double m){midterm = m;}
    void SetFinal(double f){final = f;}
    void SetFinalAverage(double fa){finalAverage = fa;}

protected: 
    double attendance;
    double project;
    double midterm;
    double final;
    double finalAverage;

}; // End English class

// *********************************************************************
// History class.
// *********************************************************************

class History: public BaseStudent 
{
public:
    History(string fn, string ln, string m, double t, double mt, double f);
    double FinalAverage();
    double GetTermPaper(){return termPaper;}
    double GetMidterm(){return midterm;}
    double GetFinalExam(){return finalExam;}
    double GetFinalAverage(){return finalAverage;}
    double FinalExam(){return finalExam;}
    void SetTermPaper(double t){termPaper = t;}
    void SetMidterm(double m){midterm = m;}
    void SetFinalExam(double f){finalExam = f;} 
    void SetFinalAverage(double fa){finalAverage = fa;}

protected:
    double termPaper;
    double midterm;
    double finalExam;
    double finalAverage;


}; // End History class.

// *********************************************************************
// Math class.
// *********************************************************************

class Math: public BaseStudent
{
public:
    Math(string fn, string ln, string m, double q1, double q2, double q3,
        double q4, double q, double t1, double t2, double f);
    double FinalAverage();
    double GetQuiz1(){return quiz1;}
    double GetQuiz2(){return quiz2;}
    double GetQuiz3(){return quiz3;}
    double GetQuiz4(){return quiz4;}
    double GetQuiz5(){return quiz5;}
    double GetFinalExam(){return finalExam;}
    double GetTest1(){return test1;}
    double GetTest2(){return test2;}
    double GetQuizAverage(){return quizAverage;}
    double GetFinalAverage(){return finalAverage;}
    void SetQuiz1(double q){quiz1 = q;}
    void SetQuiz2(double q){quiz2 = q;}
    void SetQuiz3(double q){quiz3 = q;}
    void SetQuiz4(double q){quiz4 = q;}
    void SetQuiz5(double q){quiz5 = q;}
    void SetTest1(double q){test1 = q;}
    void SetTest2(double q){test2 = q;}
    void SetFinalExam(double q){finalExam = q;}
    void SetQuizAverage();
    void SetFinalAverage(double fa){finalAverage = fa;}

protected:
    double quiz1;
    double quiz2;
    double quiz3;
    double quiz4;
    double quiz5;
    double test1;
    double test2;
    double finalExam;
    double quizAverage;
    double finalAverage;

}; // End Math class.

#endif

Do I need some sort of implementation of virtual functions?

Here’s my driver so far:

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include"basestudent.h"

using namespace std;

int main(void)
{
    string listSize;

    string fileIn = "";
    string fileOut = "";
    string firstname ="";
    string lastname ="";
    string major = "";
    string eolDummy;
    int mQuiz1, mQuiz2, mQuiz3, mQuiz4, mQuiz5, mTest1, mTest2, mFinalExam;
    int eAttendance, eProject, eMidterm, eFinalExam;
    int hTermPaper, hMidterm, hFinalExam;

    ifstream input;
    ofstream output;
    do{
        input.clear();

        cout << "Please enter the filename: ";
        cin >> fileIn;
        cout << "Please enter an output name: ";
        cin >> fileOut;

        input.open(fileIn);
        if (!input)
            cout << "Invalid file, please enter again." << endl;
    } while(!input);

    input >> listSize;
    BaseStudent**studentlist = new BaseStudent*[atoi(listSize.c_str())];
    int i = 0;
    while (!input.eof())
    {
        getline(input, lastname, ',');
        getline(input, firstname, '\n');

        input >> major;

        if (major == "Math") {
            input >>mQuiz1>>mQuiz2>>mQuiz3>>mQuiz4>>mQuiz5>>mTest1>>mTest2
                  >>mFinalExam>>eolDummy;
            // Math Constructor call
            // Array += object
            studentlist[i] = new Math(firstname,lastname,major,mQuiz1,mQuiz2,mQuiz3,mQuiz4,mQuiz5,
                                      mTest1,mTest2,mFinalExam);
        }
        else if (major == "History"){
            input >>hTermPaper>>hMidterm>>hFinalExam>>eolDummy;
            // History Constructor call
            // Array += object
            studentlist[i] = new History(firstname,lastname,major,hTermPaper,hMidterm,hFinalExam);
        }
        else if(major == "English"){
            input >>eAttendance>>eProject>>eMidterm>>eFinalExam>>eolDummy;
            // English Constructor call
            // Array += object
            studentlist[i] = new English(firstname,lastname,major,eAttendance,eProject,eMidterm,eFinalExam);
        }
        i++;
    }

    output.open(fileOut);
    output << "Student Grade Summary" << endl;
    output << "---------------------" << endl << endl;
    output << "ENGLISH CLASS "<<endl<<endl;
    output << "Student                                   Final   Final   Letter"<<endl;
    output << "Name                                      Exam    Avg     Grade"<<endl;
    output << "----------------------------------------------------------------"<<endl;
    for (int j=0; j<atoi(listSize.c_str()); j++){
        if(studentlist[j]->getMT() == ENGLISH){
            output << studentlist[j]->GetFN()<<" "<<studentlist[j]->GetLN();

            output << right << setw(42) << studentlist[j]->



    input.close();
    output.close();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:56:55+00:00Added an answer on June 7, 2026 at 6:56 am

    When you take the pointer from your array, you need to cast it using dynamic_cast to the appropriate class

    e.g.

    BaseStudent *p = somearray[0];
    
    if ( English* pEnglish = dynamic_cast<English*>(p) )
    {
       // call the methods
       cout << p->FinalAverage();
       ...
    }
    else if ( History* pHistory = dynamic_cast<History*>(p) )
    {
       // call the methods
    }
    else if ( Math* pMath = dynamic_cast<Math*>(p) )
    {
       // call the methods
    }
    

    btw use a vector instead of a raw array, it is more convenient and safer.

    std::vector<BaseStudent*> yourvector;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that contains objects from two models: @search_results = User.find(:all, :conditions
I have an array myArr which contains objects of custom class..e.g. objs of type
I have an array of objects which all have a timestamp marking when they
I have an array of DataRow objects in my C# project which I would
I have an array of Deferred objects, which I'm trying to map to their
I have a method which returns an array of fixed type objects (let's say
I have an array of objects that have QTTime structs as attributes. The objects
I have an array of ActiveRecord objects, each one which has its own respective
I have an Array of NSDictionary objects. These Dictionaries are parsed from a JSON
I have an array of arrays with objects and now want to get all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.