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 7694261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:16:46+00:00 2026-05-31T21:16:46+00:00

I have a class of students which I store into set in my cpp

  • 0

I have a class of students which I store into set in my cpp file. The problem I am having is printing out the actual Student object. I have tried all that I can think of and all I get is either the address of the pointer or a compile error. I have a method in my student class called display that prints out all the information in the format I want it to be.

Here is what I have so far.

.cpp file

#include "Name.h"
#include "Student.h"
#include<iostream>
#include<string>
#include<set>
using namespace std;

int main()
{
    Student s1;
    set<Student *> s;
    while(cin>>s1)
    {
        s.insert(new Student(s1));
    }
    for(set<Student *>::const_iterator it = s.begin(); it != s.end(); ++it)
    {
        &(*it).display(cout);
    }
}

Student.h

#ifndef STUDENT_H
#define STUDENT_H
#include<string>
#include<iostream>
#include<map>
#include "Name.h"
typedef std::map<std::string, int> GradeMap;

class Student {
public:
    Student(const std::string id="", const Name& name = Name(),const GradeMap & grades = GradeMap()):id_(id),name_(name),grades_(grades){}
    Student(const Student& s):id_(s.id_),name_(s.name_),grades_(s.grades_){}
    virtual ~Student(){}
    friend std::istream& operator>>(std::istream& is, Student& s);

    virtual void display(std::ostream& os) const{
        os << "ID: " << id_ <<std::endl<< "Name: " << name_ << std::endl;

        for(std::map<std::string, int>::const_iterator it = grades_.begin(); it != grades_.end(); ++it)
            os<<it->first<<' '<<it->second<<std::endl;
    }

private:
  std::string id_;
  Name name_;
  GradeMap grades_;

};

inline std::istream& operator>>(std::istream& is, Student& s)
{
    std::string id;
    std::string key;
    int grade;
    int count = 0;
    Name name;

    if(is>>id>>name>>count){
        s.id_ = id;
        s.name_ = name;
    }
    else {
        is.setstate(std::ios_base::failbit);
    }


    for(int i = 0; i < count; i++)
    {
        if(is>>key>>grade)
        {
            s.grades_[key] = grade;
        }
    }
    return is;
}
#endif

Name.h

#ifndef NAME_H
#define NAME_H
#include <string>
#include <iostream>
class Name{
    public:
        explicit Name(const std::string& first = "",const std:: string& last = ""):first_(first),last_(last){}
        friend std::ostream& operator<<(std::ostream&, const Name&);
        friend std::istream& operator>>(std::istream&, Name&);
private:
        std::string first_;
        std::string last_;
};

inline std::ostream& operator<<(std::ostream& os, const Name& n){
    return os << n.first_<< " " << n.last_;
}
inline std::istream& operator>>(std::istream& is, Name& n){
    std::string first,last;
    if(is >> first >> last ){
        n.first_ = first;
        n.last_ = last;
    }
    else
        is.setstate(std::ios_base::failbit);
    return is;
}
#endif

also here is the file I using to test this

111111111
john smith
3
comp2510 25
eng2525 60
bio3512 45
222222222
jane doe
2
elex1510 90
comp2510 85

The file is organized like so. The id of the student comes first, then their name, and then the number of courses they have taken follow by that many number of courses plus the grade they got in that course.

My question is how would you print out the actual student object?

  • 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-31T21:16:47+00:00Added an answer on May 31, 2026 at 9:16 pm

    In the for loop:

    for(set<Student *>::const_iterator it = s.begin(); it != s.end(); ++it)
    {
        &(*it).display(cout);
    }
    

    (*it) is a Student*:

    (*it)->display(cout);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that stores exam scores for a class of students. I
I have a Store class which is an array of Person; I have a
I've recently set some coursework for some undergraduate students for which they have to
I have a similar model Class Student(models.Model): A simple class which holds the basic
I have an attendance page which outputs a list of students in a class
I have a controller class below which adds a student in to session. class
I have a data class Student, and I have an aggregate class Students. Student
Let's say I have a class of 30 students and want generate every possible
I have an ASP.NET WebService that returns an object of List public class Students
Let's say I have a table that represents a super class, students . And

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.