Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data Structures using C++ Consider the following class declaration: class Studen

ID: 3864688 • Letter: D

Question

Data Structures using C++

Consider the following class declaration:

class Student

{

public:

       Student(const char last[ ] = "", const char first[ ] = "",

              const int id = 0 );

            const Student& operator=

                   (const Student& otherStudent); // Overloaded operator=

          ...   // Other member functions

  

private:

       char *lastName;

       char *firstName;

       int ID;

};

Write the code for the definition (i.e., implementation) of the overloaded assignment operator. Hint: Here you may assume that the cstring header file has already been included, and consequently functions such as strlen, strcpy, strcat may be used . Their prototypes are shown as follows for your information:

size_t strlen ( const char * str );

char * strcpy ( char * destination, const char * source );

char * strcat ( char * destination, const char * source );

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

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

class Student
{
public:
Student(const char last[ ] = "", const char first[ ] = "",const int id = 0 );

const Student& operator=(const Student& otherStudent); // Overloaded operator=

// Other member functions
  
private:
char *lastName;
char *firstName;
int ID;
};

const Student& Student::operator=(const Student& otherStudent){

   int fLen = strlen(otherStudent.firstName);
   int lLen = strlen(otherStudent.lastName);

   // creating space
   firstName = new char[fLen];
   lastName = new char[lLen];

   // copying the content of otherStudent in current object
   strcpy(otherStudent.firstName, firstName);
   strcpy(otherStudent.lastName, lastName);

   ID = otherStudent.ID;

   return *this;
}

int main(){


   return 0;
}