Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I have here code where I have to complete copy constructor and copy assignment. I tried doing it but I'm not sure about the

Hello,

I have here code where I have to complete copy constructor and copy assignment. I tried doing it but I'm not sure about the output. Thanks

#define _CRT_SECURE_NO_WARNINGS

#include

#include

class Person {

char* name;

protected:

void SetName(const char* _name) {

if (name != nullptr) delete[] name;

int len = strlen(_name) + 1;

name = new char[len];

strcpy(name, _name);

}

char* GetName() const { return name; }

public:

Person() {

name = nullptr;

}

Person(const char* _name) {

int len = strlen(_name) + 1;

name = new char[len];

strcpy(name, _name);

}

};

class Student :public Person {

double* marks;

int numMarks;

public:

Student() {};

Student(const char* _name, double* _marks, int _numMarks) : Person(_name) {

marks = new double[_numMarks];

numMarks = _numMarks;

for (int i = 0; i < numMarks; ++i) marks[i] = _marks[i];

}

Student(const Student& st) {

//1. Complete copy constructor

}

Student& operator=(const Student& rhs) {

//2. Complete copy assignment

}

void SetName(const char* _name) {

Person::SetName(_name);

};

char* GetName() const { return Person::GetName(); };

void SetMarks(double* _marks, int _numMarks) {

if (marks != nullptr) delete[] marks;

marks = new double[_numMarks];

numMarks = _numMarks;

for (int i = 0; i < numMarks; ++i) marks[i] = _marks[i];

};

double GetAverage() {

double runningTotal = 0.0;

for (int i = 0; i < numMarks; ++i) runningTotal += marks[i];

return runningTotal / numMarks;

}

};

int main(void) {

double marks1[] = { 55.0, 66.0, 66.0, 76.0 };

Student student1("Beth", marks1, 4);

double marks2[] = { 77.0, 66.0, 82.0, 86.0, 90.0 };

Student student2("John", marks2, 5);

Student student3(student2);

student3.SetName("Barry");

Student student4;

student4 = student1;

std::cout << student1.GetName() << " has an average of " << student1.GetAverage() << "%" << std::endl;

std::cout << student2.GetName() << " has an average of " << student2.GetAverage() << "%" << std::endl;

std::cout << student3.GetName() << " has an average of " << student3.GetAverage() << "%" << std::endl;

std::cout << student4.GetName() << " has an average of " << student4.GetAverage() << "%" << std::endl;

return 0;

}

Here's my work along with the output.

//1. Complete this copy constructor

//cout << "Copy constructor" << endl;

numMarks = st.numMarks;

if (st.marks != nullptr && numMarks > 0)

{

marks = new double[numMarks];

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

{

marks[i] = st.marks[i];

}

}

else

{

marks = nullptr;

}

//2. Complete this copy assignment

if (this != &rhs)

{

(Person&)*this = rhs;

delete[] marks;

}

return *this;

output:

Beth has an average of 65.75%

KT has an average of 80.2%

Barry has an average of 80.2%

Beth has an average of -nan(ind)%

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

This code is working but I am failing a hidden test. \f

Answered: 1 week ago

Question

Common defects of the eye?

Answered: 1 week ago

Question

The eye is responsible for producing tears....?

Answered: 1 week ago

Question

Cause on the an increase in pressure with in the eye ?

Answered: 1 week ago

Question

What is the Normal Eye Power ?

Answered: 1 week ago

Question

Thomas Aquinas was born........?

Answered: 1 week ago