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
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.
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.