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

  • SEARCH
  • Home
  • 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 711037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:41:23+00:00 2026-05-14T04:41:23+00:00

For an assignment I’ve made a simple C++ program that uses a superclass (Student)

  • 0

For an assignment I’ve made a simple C++ program that uses a superclass (Student) and two subclasses (CourseStudent and ResearchStudent) to store a list of students and print out their details, with different details shown for the two different types of students (using overriding of the display() method from Student).

My question is about how the program collects input from the user of things like the student name, ID number, unit and fee information (for a course student) and research information (for research students):

My implementation has the prompting for user input and the collecting of that input handled within the classes themselves. The reasoning behind this was that each class knows what kind of input it needs, so it makes sense to me to have it know how to ask for it (given an ostream through which to ask and an istream to collect the input from).

My lecturer says that the prompting and input should all be handled in the main program, which seems to me somewhat messier, and would make it trickier to extend the program to handle different types of students.

I am considering, as a compromise, to make a helper class that handles the prompting and collection of user input for each type of Student, which could then be called on by the main program. The advantage of this would be that the student classes don’t have as much in them (so they’re cleaner), but also they can be bundled with the helper classes if the input functionality is required. This also means more classes of Student could be added without having to make major changes to the main program, as long as helper classes are provided for these new classes. Also the helper class could be swapped for an alternative language version without having to make any changes to the class itself.

What are the major advantages and disadvantages of the three different options for user input (fully encapsulated, helper class or in the main program)?

  • 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-05-14T04:41:24+00:00Added an answer on May 14, 2026 at 4:41 am

    As mentioned by scv, it is normally better to decouple presentation (view) from internal structure (model).

    Here you have a typical case:

    • the Student class, root of a model hierarchy
    • the Displayer class, root of another independent hierarchy

    The issue with the display is that it varies according to two elements, which calls for a system of double dispatch (using virtual).

    This is traditionally solved using the Visitor Pattern.

    Let’s check the base classes first:

    // student.h
    class Displayer;
    
    class Student
    {
    public:
      virtual ~Student();
      virtual void display(Displayer& d) const = 0; // display should not modify the model
    };
    
    // displayer.h
    class Student;
    class CourseStudent;
    class ResearchStudent;
    
    class Displayer
    {
    public:
      virtual ~Displayer();
    
      virtual void display(const Student& s) = 0; // default method for students
                                                  // not strictly necessary
      virtual void display(const CourseStudent& s) = 0;
      virtual void display(const ResearchStudent& s) = 0;
    };
    

    And now, let’s implement some:

    // courseStudent.h
    #include "student.h"
    
    class CourseStudent: public Student
    {
    public:
      virtual void display(Displayer& d) const;
    
    };
    
    // courseStudent.cpp
    #include "courseStudent.h"
    #include "displayer.h"
    
    // *this has static type CourseStudent
    // so Displayer::display(const CourseStudent&) is invoked
    void CourseStudent::display(Displayer& d) const
    {
      d.display(*this);
    }
    
    
    // consoleDisplayer.h
    #include "displayer.h"
    
    class ConsoleDisplayer: public Displayer
    {
    public:
      virtual void display(const Student& s) = 0; // default method for students
                                                  // not strictly necessary
      virtual void display(const CourseStudent& s) = 0;
      virtual void display(const ResearchStudent& s) = 0;
    };
    
    // consoleDisplayer.cpp
    #include "consoleDisplayer.h"
    
    #include "student.h"
    #include "courseStudent.h"
    #include "researchStudent.h"
    
    void ConsoleDisplayer::display(const Student& s) { }
    
    void ConsoleDisplayer::display(const CourseStudent& s) { }
    
    void ConsoleDisplayer::display(const ResearchStudent& s) { }
    

    As you can see, the hard part is that if I wish to add a new derived class of Student, then I need to add a new virtual method in Displayer and override it in every derived class of Displayer… but otherwise it works great.

    The advantage is that the logic of display is now decoupled from the model, thus we can add new display logic without ever touching our model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A 32-bit process can only address 4GB address space at… May 14, 2026 at 7:18 pm
  • Editorial Team
    Editorial Team added an answer You can add the rules based on that selector using… May 14, 2026 at 7:18 pm
  • Editorial Team
    Editorial Team added an answer I have found a workaround for my current problem. I… May 14, 2026 at 7:18 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.