Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++ 1.Make a class with three archives .h .cpp and main 2.Implement the assign operator (*this) 3.Add the student name dynamically 4.implement the >>,

In C++

1.Make a class with three archives .h .cpp and main

2.Implement the assign operator (*this)

3.Add the student name dynamically

4.implement the >>,<< operator

5.Show all the operators in the main program

Use Tonny Gaddis Student Scores (version 3) page 832, chap. 14 to do all this.

Although copy constructors solve the initialization problems inherent with objects con- taining pointer members, they do not work with simple assignment statements. Copy con- structors are just thatconstructors. They are only invoked when an object is created. Statements like the following still perform memberwise assignment:

student2 = student1;

In order to change the way the assignment operator works, it must be overloaded. Operator overloading permits you to redefine an existing operators behavior when used with a class object.

C++ allows a class to have special member functions called operator functions. If you wish to redefine the way a particular operator works with an object, you define a function for that operator. The Operator function is then executed any time the operator is used with an object of that class. For example, the following version of the StudentTestScores class overloads the = operator.

#ifndef STUDENTTESTSCORES_H

#define STUDENTTESTSCORES_H

#include

using namespace std;

const double DEFAULT_SCORE = 0.0; class StudentTestScores {

private:

string studentName; // The student's name

double *testScores; // Points to array of test scores

int numTestScores; // Number of test scores

// Private member function to create an

// array of test scores.

void createTestScoresArray(int size)

{ numTestScores = size;

testScores = new double[size];

for (int i = 0; i < size; i++)

testScores[i] = DEFAULT_SCORE; }

public:

// Constructor

StudentTestScores(string name, int numScores)

{ studentName = name;

createTestScoresArray(numScores); }

// Copy constructor

StudentTestScores(const StudentTestScores &obj)

{ studentName = obj.studentName;

numTestScores = obj.numTestScores;

testScores = new double[numTestScores];

for (int i = 0; i < numTestScores; i++)

testScores[i] = obj.testScores[i]; }

// Destructor

~StudentTestScores()

{ delete [] testScores; }

// The setTestScore function sets a specific

// test score's value.

void setTestScore(double score, int index)

{ testScores[index] = score; }

// Set the student's name.

void setStudentName(string name)

{ studentName = name; }

// Get the student's name.

string getStudentName() const

{ return studentName; }

// Get the number of test scores.

int getNumTestScores() { return numTestScores; }

// Get a specific test score. double getTestScore(int index) const

{ return testScores[index]; }

// Overloaded = operator void operator=(const StudentTestScores &right)

{ delete [] testScores;

studentName = right.studentName;

numTestScores = right.numTestScores;

testScores = new double[numTestScores];

for (int i = 0; i < numTestScores; i++)

testScores[i] = right.testScores[i]; }

};

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago