The purpose of this assignment is to overload the constructor, is equal to operator, and the equal to operator for an abstract data type.
Program Requirements:
Modify the Student class created in the previous assignment.
Include an overloaded equality operator (==) in the Student class that tests two Student objects for equality. This function should return back true if the two Student objects are equal and false if the two Student objects are not equal.
Include an overloaded assignment operator (=) in the Student class that assigns one Student object to another. The return type of this operator function should be a Student object to allow multiple assignments such as student1 = student2 = student3; You must write the code for this operator, you cannot use the default assignment operator the C++ compiler will build in the absence of one (page 499).
Include a copy constructor in the Student class to initialize the object being constructed to an existing Student object.
Make sure all your methods are defined in the implementation section of the class. Do not use any inline member functions. Do not print from within the Student class.
Steps 7 through 12 below all apply to the main function.
Create two Student objects.
Prompt the user for a name and social security number. Enter these from the keyboard. Use the appropriate set methods to update the name and social security number for the first student object. The name entered must be able to contain spaces for example John Smith or Jim Doe Jr. Read page 611 - 613 of your textbook under the heading Caution: The Phantom Newline Character for information on using the cin input stream and the getline method together in a program to receive user input.
Prompt the user for a second name and social security number. Enter these from the keyboard. Use the appropriate set methods to update the name and social security number for the second student object. The name entered must be able to contain spaces.
Use the overloaded == operator to display a message indicating whether the student objects created are the same or different. The student objects are equal if the value of the name variables are equal and the social security numbers are equal.
After this comparison, test your assignment operator. Assign the first student object created in step 7 to the second student object created in step 7. Use the overloaded == operator again to display a message indicating whether the two student objects are the same or different.
Call the copy constructor of the Student class to create a third Student object using one of the existing Student objects created in step 7. Use the overloaded == operator again to display a message indicating whether these two student objects are the same or different.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <iostream>
using namespace std;
// class declaration section
class Student
{
private://data members
int SSN;
char Name[80];
public:
Student(); // default constructor
Student(char Name[80], int SSN); //overloaded constructor
void setSSN(int);
int getSSN();
void setName(char *);
char getName();
Student operator+(const Student &);
bool operator==(const Student &);
Student operator= (const Student &);
operator double();
};
//class implementation section//
Student::Student()//default constructor
{
strcpy_s(Name, sizeof(Name), "unassigned");
SSN = 999999999;
}
Student::Student(char* name, int ssn)
{
setSSN(ssn);
setName(name);
}
void Student::setSSN(int ssn){
SSN = ssn;
}
int Student::getSSN(){
return SSN;
}
void Student::setName(char* name)
{
if (name != 0)
char*Name = name;
else char*Name = name;
}
char Student::getName(){
return Name[80];
}
Student Student::operator+(const Student & rhs)
{
Student temp;
temp.SSN = SSN * rhs.SSN;
temp.Name = (char name* rhs.SSN)
}
int main()
{
char* name, int ssn;
cout << "Enter Student 1 Name: ";
cin >> name;
cout << "Enter Student 1 SSN: ";
cin >> ssn;
Student Stud1(char* name, int ssn);
cout << "Enter Student 1 Name: ";
cin >> name;
cout << "Enter Student 1 SSN: ";
cin >> ssn;
Student Stud2(char* name, int ssn);
if (Stud1 == Stud2)
cout << "Those are not the same Students";
else
cout << "Those are the same Students";
Student Stud3 = Stud1 + Stud2;
return 0;
}
|