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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:32:44+00:00 2026-05-26T16:32:44+00:00

Hello to all that read I am self learning C++ from a text book.

  • 0

Hello to all that read

I am self learning C++ from a text book.

…and I have been given a serious of questions that I am have trouble interpreting, the questions are:

4.
Modify the constructor of Exercise 3 so that it assigns a new ID number as each variable is established. The ID number should be output to the screen.

**Please Note: Exercise 3 was just adding a Class constructor to the class structure – and basically setting all the class private member variables to ‘0’ within that added constructor.

5.
Add constructors to the class of exercise 4. They should output the ID number when they are invoked.

6.
Write a constructor whose argument is an ID number. Overload it with the constructor of(Exercise 5) and then Write a main program to test this new constructor.

**So the questions: 4 and 5 are confusing me as they appear to be asking the same questions (i.e outputting the I.D numbers) and I need more than one ‘constructor’ in order to do this( I get that part).

**Question 6 Is effectively asking me to overload more than one instance declared of the class with an ID number as the argument!!!! I am confused because in my text book it specifically states that you cannot have an argument for a constructor when you have an array of classes declared within in the main program.

So if anyone can shed light on these questions and clarify how they would proceed I would appreciate the help. I appreciate that I may of interpreted the questions wrongly that is why I am asking for help.

**Please note: The name of the class: ‘class classroom’ is not ideal but it is what I copied from the text book question!

**Please also note that I have answered (I think!) exercise/question 4 by adding code that Id’s each instance of the class variable.

And the relevant code relating to the above questions are:

#include <iostream>
#include <cstdio>

using namespace std;


  class classroom{

         char name[25];
         int student_id;
         float grades[10];
         float average;
         int num_tests;
         float letter_grade;
         **static int next_student_id;**
  public:       
         void enter_name_id(void);
         void enter_grade(void);
         void average_grades(void);
         void letter_grades(void);
         void output_name_id_grade(void);
         classroom();

  };

         **int classroom::next_student_id=1;**

and the constructor:

classroom::classroom(){

          int i;

          num_tests=0;

         **student_id=next_student_id++;**           

         **cout<<"\nstudent id: "<<student_id;**

          average=0.0;
          for(i=0;i<10;i++){

            grades[i]=0.0;

          }

          for(i=0;i<27;i++){

             name[i]='-';       
          }
          cout<<"\n*****************Finished*****************";
     }

And the main program declaration:

int main()
{       
      classroom students[3];
//and so on...
}

and the code added for question/Exercise 4 was:

in the class structure I added the following private member variable:

**static int next_student_id;**

and outside of the class structure I initialized the static variable to 1

int classroom::next_student_id=1;

and in the constructor I added this code: which basically outputted to the screen a unique ID for each instance of classroom class variable:

student_id=next_student_id++;

** cout<<“\nstudent id: “<

Many thanks in advance

  • 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-26T16:32:45+00:00Added an answer on May 26, 2026 at 4:32 pm

    Questions 4 and 5 do appear to be the same from what you’ve provided – I’d reread it carefully to be sure, but it wouldn’t be the first time textbook examples had a typo (in fact, I’ve run across that many times – alot of book companies are lazy when it comes to the exercises).

    You do have the right answer with your static counter, so I wouldn’t worry about it – you’ve understood the goal and solved the problem.

    Question 6… it sounds like it wants you to just add an extra constructor to the class – so, you would have the default constructor (which you must explicitly write, but you already have done this in order to do your static counter), and you would also have a one-parameter constructor taking an integer. The one-parameter constructor should not need to use the static variable as it is allowing the user to provide the id to the class explicitly.

    Moving on (since I think you’ve already got all of this), you are also correct in thinking that you can’t provide an argument to a constructor when making an array. To test #6 as it suggests, you’ll either have to make a stand-alone object (i.e. not an array), or something like a std::vector or other container that provides this capability.

    #include <iostream>
    #include <vector>
    
    class A {
        public:
            A(int i) { /* Empty */ }
    }; 
    
    int main()
    {
        //Creates a new vector holding 5 A's initialized with the 2nd parameter.
        std::vector<A> myVec(5, A(7));
    }
    

    Note that this technically creates a temporary A which is used to initialize the 5 vector entries. Also note that a std::vector is part of the STL (standard library), and it is basically a dynamically sized array.

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

Sidebar

Related Questions

Hello to all that read I am self learning C++ from a text book:
Hello to all that read I have a small problem(or it could be large!),
Hello! I would like to extract all citations from a text. Additionally, the name
This has to be a common question that all programmers have from time to
hello all I have a small dialog which I created dynamically, which has a
Problem Hello all! I have this code which takes my jpg image loops through
Hello friends i need a book/ tutorials for rails without using scoffold. All the
I have read through all relevant posts on Prawn but found no mentioning (even
hello i have a problem that's maybe difficult to descripe, i have an application,
I fought with the IDE and read around all day enough to learn that

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.