By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,557 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,557 IT Pros & Developers. It's quick & easy.

I need some help with this overloaded program

P: 9
I am having trouble cleaning this mess up. This overloaded program is supposed to utilize an overloaded equality operator that should return true if the two studentís objects are equal and false if the two are student objects are not equal. And include an overloaded assignment operator in the Student class that assign one Student object to another, that allows for multiple assignments such as student1=student2=student3. And use the overloaded ++ operator to display a message indicating whether the student objects are the same. Last, call copy constructor to create third Student object using one of the existing Student object created.
The display example should be:
Enter student1 name: John Smith
Enter student1 SSN: 123456789
Enter student2 name: Jane Smith
Enter student2 SSN: 123456789
Those are not the same students
After using the assignment operator, those are the same students
After using the copy constructor, those are the same students

Thank You, for your help.





Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class Student
  8. {
  9.     private:
  10.  
  11.         string name;
  12.         int SSN = 0 ; 
  13.  
  14.     public:
  15.  
  16.         Student() ; 
  17.         Student( string name, int ssn ) : name(name), SSN(ssn) 
  18.         Student( const  Student&  ) ;
  19.  
  20.         bool operator== ( const Student&  ) ;
  21.         bool operator!= ( const Student&  ) ; 
  22.  
  23.  
  24. };
  25.  
  26. Student create_student( int student_number )
  27. {
  28.     string name;
  29.     int ssn;
  30.  
  31.    cout << "Enter Student" << student_number << " name: ";
  32.    getline( cin, name);
  33.  
  34.    cout << "Enter Student" << student_number << " ssn: ";
  35.    cin >> ssn ;
  36.    cin.ignore( 1000, '\n' ) ; // throw away the new line
  37.  
  38.     return Student( name, ssn ) ;
  39. }
  40.  
  41. bool Student ::operator ==(const Student& student_data)
  42. {
  43.  
  44. return this->name == student_data.name && this->SSN == student_data.SSN ;    
  45.  
  46. }
  47.  
  48. bool Student ::operator !=(const Student& student_data)
  49. {
  50.  
  51.      return !( *this == student_data ) ;
  52. }
  53. void compare( const Student& a, const Student& b )
  54. {
  55.  
  56.  
  57.     if( a == b )  cout<< "These are the same students.\n"<<endl ;
  58.  
  59.     else cout << "These are different students.\n"<<endl;
  60. }
  61.  
  62. int main ()
  63. {
  64.     Student student1 = create_student(1) ;
  65.     const Student student2 = create_student(2) ;
  66.  
  67.     compare( student1, student2 ) ;
  68.  
  69.     student1 = student2 ;
  70.     cout<< "After using the assignment operator: "<<endl ;
  71.     compare( student1, student2 ) ;
  72.  
  73.     const Student student3( student1 ) ;
  74.     cout << "After using the copy constructor: "<<endl ;
  75.     compare( student1, student3 ) ;
  76.  
  77.  
  78.  
  79. }
Sep 30 '15 #1

✓ answered by weaknessforcats

How are you getting any results at all? This code won't compile. Plus your constructors have not been coded and neither has

Expand|Select|Wrap|Line Numbers
  1. bool operator==( const Student& lhs, const Student& rhs)
  2. {
  3.    return true;  // if all the members match match
  4. }
  5.  
I added this code plus the constructors:

Expand|Select|Wrap|Line Numbers
  1. Student::Student(string name, int ssn) : name(name), SSN(ssn)
  2. {
  3. }
  4. Student::Student(const  Student&)
  5. {
  6.  
  7. }
The compiles and links now and I leave it to you to code the guts of these functions.

Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
How are you getting any results at all? This code won't compile. Plus your constructors have not been coded and neither has

Expand|Select|Wrap|Line Numbers
  1. bool operator==( const Student& lhs, const Student& rhs)
  2. {
  3.    return true;  // if all the members match match
  4. }
  5.  
I added this code plus the constructors:

Expand|Select|Wrap|Line Numbers
  1. Student::Student(string name, int ssn) : name(name), SSN(ssn)
  2. {
  3. }
  4. Student::Student(const  Student&)
  5. {
  6.  
  7. }
The compiles and links now and I leave it to you to code the guts of these functions.
Oct 1 '15 #2

P: 9
weaknessforcats,

Thank You, for helping me clean this mess up.
Oct 1 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.