(1) Use C++ to create a new header file Student.h and write the prototype of a c
ID: 3539317 • Letter: #
Question
(1) Use C++ to create a new header file Student.h and write the prototype of a class Student includes the private portion consisting of an integer for ID and a static char array for name. Then, add appropriate operations such as below to the classes:
Student();
Student(char name[], int id);
void print(ostream & out);
int get_id() const;
void set_name(char new_name[]);
(2) Implement the class methods in the same file Student.h.
(3) Write a driver (or client with main function in a new file main.cpp) to do the following: (a) declare two students A and B, A has default name and id; and B%u2019s name is %u201CJoe%u201D and id is 999999999. (b) Check if student B%u2019s id is 999999999, print %u201Ccorrect%u201D if it is and %u201Cincorrect%u201D if it is not on screen. (c) Print student B%u2019s id and name on screen (d) Change the name of student A to %u201CAnonymous%u201D.
Explanation / Answer
//Student.h
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
class Student
{
public:
Student();
Student(char name[], int id);
void print(ostream & out);
int get_id() const;
void set_name(char new_name[]);
private:
int ID;
char Name[100];
};
Student :: Student()
{
}
Student :: Student(char name[], int id)
{
strcpy(Name,name);
ID = id;
}
void Student :: print(ostream & out)
{
out<<" Name : " << Name;
out<<" ID : " << ID << endl;
}
int Student :: get_id() const
{
return ID;
}
void Student :: set_name(char new_name[])
{
strcpy(Name,new_name);
}
//main.cpp
#include <iostream>
#include "Student.h"
using namespace std;
int main()
{
Student A;
Student B("Joe", 999999999);
cout<<"Checking if id of B is 999999999"<<endl;
if(B.get_id() == 999999999)
cout << "Correct "<<endl;
else
cout<<" Incorrect"<<endl;
cout<<"Student B";
B.print(cout);
A.set_name("Anonymous");
cout<<"Student A";
A.print(cout);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.