What command do I use for the SetId member

So here is my assignment:

1. Declare a class named Student that contains
a. Three private data members:
char firstName[20];
char lastName[20];
int id;
b. Three public member functions:
• SetName function that takes two (char*) parameters and returns nothing.
• SetId function that takes one (int) parameter and returns nothing.
• Display function that takes nothing and returns nothing.

2. Implement the SetName, SetId, and Display member functions declared below:
• SetName function that takes two (char*) parameters and copies them to the firstName and lastName private data members (use strcpy).
SetId function that takes one (int) parameter and assigns it to the id private data member.
• Display function that takes no parameter and prints the firstName, lastName and id of a Student object.

3. Based on the class that you have written in the previous questions, write a main() function that creates a Student object and prints out the following:
Student Name: John Smith
Id: 150306


4. Add to the Student class a default constructor and a destructor. The default constructor should set the name "John Doe" and 0 for the Id. Create a default student object in your main function and print it out with the Display function:
Student Name: John Doe
Id: 0


5. Add a constructor to the Student class that takes three parameters (char*, char*, int) and initializes the firstName, lastName, and id data members. Create a student object in your main function using this constructor with the name "Sally Jones" and with an Id of 12345. Print out the information with the Display function:
Student Name: Sally Jones
Id: 12345

I'm pretty sure I have the whole thing finished, the only problem is I don't know what command to use in the SetId member of the student class. The above underlined and bolded step is the one I got stuck on and skipped. Here is my code:

#include <iostream>
#include <cstring>
using namespace std;

class Student
{
private:
char firstName[20];
char lastName[20];
int id;
public:
Student();
Student(char *f, char *l, int);
~Student();
void SetName(char *f, char *l);
void SetId(int);
void Display();

};

Student::Student() //default Constuctor function
{
strcpy(firstName, "John");
strcpy(lastName, "Doe");
id = 0;
}

Student::Student(char *f, char*l, int) //Parameterized constructor function
{
strcpy(firstName, "Sally");
strcpy(lastName, "Jones");
id = 12345;
}

Student::~Student() {} //Destructor


void Student::SetName(char *f, char *l)
{
strcpy(firstName, f);
strcpy(lastName, l);
}

void Student::SetId(int)
{
// Dont know what to put here.
}

void Student::Display(void)
{
cout << "Student name: " << firstName << " " << lastName << endl;
cout << "Id: " << id <<endl << endl;
}

int main()
{
Student defaultStudent, studentOne, studentTwo("Sally", "Jones", 12345), studentThree; //Declare Objects

defaultStudent.Display(); //Constuctor test
studentOne.SetName("John", "Smith");
studentOne.SetId(150306);
studentOne.Display();
studentTwo.Display();

getchar();
}

The output I get is correct, minus the Id for John Smith:

Student name: John Doe
Id: 0

Student name: John Smith
Id: 0

Student name: Sally Jones
Id: 12345


Thanks for all the help!


Last edited on
Topic archived. No new replies allowed.