These are my homework questions. I have attempt to do most of it and I need somebody to check to see if im doing this right.

1. Given the following declaration for a TestScore class, write a derived class declaration called IDScore that adds an integer student ID number as a private member, and that supplies (1) a constructor whose parameters correspond to the three member fields, and (2) an observer that returns the ID number.

class TestScore
{
public:
 	TestScore(string name, int score);
 	string GetName() const;
 	int GetScore() const;
private:
 	string studentName;
 	int studentScore;
};

my attempt:
class IDScore : public TestScore
{
public:
 	IDScore(int studID, int studScore, string studName);
 	int GetIDNumber() const;
private:
 	int studentIDNumber;
};

2. Write the implementation file for the TestScore class in Exercise 1. The constructor just assigns its parameters to the private data members, and the observers simply return the corresponding member.

my attempt:
TestScore::TestScore(string name, int score) : 
studentName(name) , studentScore(score)
{
}
TestScore::GetName() const
{
return studentName;
}
TestScore::GetScore() const
{
Return studentScore;
}

3. Write the implementation file for the IDScore class in Exercise 1. The constructor just assigns its parameters to the private data members, and the observer simply returns the corresponding member.

my attempt:

TestScore::TestScore(int studID, int studScore, string studName) : studentIDNumber(studID)
{
}
TestScore::GetIDNumber() const
{
return studentIDNumber;
}

Close. (And in the future, please use [ CODE ] tags around your code -- select a code segment and click the button/icon at the top of the editor.)

In part 3, remember which class you're providing the implementation for. Then, how are the studentName and studentScore going to get initialized in the constructor (hint: you should be re-using the existing constructor as appropriate).

Also, if you want to know if your code is correct, add a

int main ()
{
   ...
   return 0;
}

block somewhere, have it create a couple of instances of your class, and print the values returned from the observer-methods. If the code compiles and your tests print out the same values that you passed to the constructors, then your code is minimally correct. If it won't compile, the compiler error messages tell you exactly what you did wrong, and often enough where in the code the mistake is (other times, only where the compiler couldn't figure out what to do next, and the actual error might be a ways back). If it doesn't print the correct values, you need to go back and see why not.

Thank you for the reply. I have two more questions. I been looking at these codes and been scratching my head trying to understand it. please help

What's the potential danger in the following code segment?

int index;
int value;
int xeperp[100];
cin >> index >> value;
xeperp[index] = value;

What is the side effect of the following function?

int Max(int trio[3])
{
int temp;
if(trio[0]>trio[1])
{
temp = trio[1];
trio[1] = trio[0];
trio[0] = temp;
}
if (trio[1] > trio[2];)
{
temp = trio[2];
trio[2] = trio[1];
trio[1] = temp;
}
return trio[2];
}

I'll ask you questions in return. See if they help.

For the first example, what values are provided by the user? How are they then used in the code? What -could- the user provide? What if he/she inputs something unexpected?

For the second example, do you understand what is meant by "side-effect"? What is the function called? What does it return? What else does it do?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.