int main() { Junior J1; Freshman F1; J1.setValue(); F1.setValue(); J1.getValue()
ID: 3853182 • Letter: I
Question
int main()
{
Junior J1;
Freshman F1;
J1.setValue();
F1.setValue();
J1.getValue();
F1.getValue();
Junior J2(J1);
Freshman F2(F1);
J2.getValue();
F2.getValue();
Junior J3;
J3=J1;
Freshman F3;
F3=F1;
J3.getValue();
F3.getValue();
return 0;
}
Here is an outline of what my program looks like and the name of the functions:
//BASE CLASS STUDENT
class Student
{
private:
long ID;
string Name;
float GPA;
public:
Student();
void getValue();
void setValue();
~Student();
};
//DERIVED CLASS FRESHMAN
class Freshman: public Student
{
private:
float *CS1; //a.) test score variable dynamic array
int number_of_scores;
double average;
public:
Freshman();
void getValue();
void setValue();
~Freshman();
};
//DERIVED CLASS JUNIOR
class Junior: public Student
{
private:
float *CS8; //a.) test score variable dynamic array
int number_of_scores;
double average;
public:
Junior();
void getValue();
void setValue();
~Junior();
};
#include-iostream> NOTE: I've been trying to do all other problems myself but I don't know what to do with this Problem Don't worry about the codes In the accessor and mutator both in base and derived classes and the dynamic memory part, I just need an example for the copy constructor and overloaded assignment operator. I'm not even sure what values go in the functions to be copied. Please help ) Copy Constructor and Assignment Operator a) Edit your b s assignment operator. cla ss d e finition to include copy constructor a nd n overlo a d d b) Copy constructor n ds to cr t n identical copy of an object while cre ating c) Now d fin copy constructors in your derived cla ss s as w e ll and call the ba s e d) Ove rlo d ssignment op rator (-) in your b s € and derived class e s to copy a e) Us th following m in function to demonstra te that your copy constructors and ne w obj e ct. cla ss copy constructor from th m. d e riv e d object to another. overlo d e d ssignm nt operators work prope rly.Explanation / Answer
Is the function definition: Student& Student::operator=(const Student& studentobj) or is it.. Student& Student::operator=(const Student& studentobj1) is this what is called the assignment operator?
Student& Student::operator=(const Student& studentobj) : This is Operator Overloading
Student& Student::operator=(const Student& studentobj) : This is Copy Constructor.
Answer for the Question:
See the below code for Copy constructor and operator ovrloading are written for Base and it's derived clsses and called the base class constructor from derived class constructor.
class Student
{
private:
long ID;
string Name;
float GPA;
public:
Student();
void getValue();
void setValue();
~Student();
Student(const Student &studentobj);
Student& operator=(const Student &studentobj);
};
Student::Student(const Student &studentobj)
{
this->ID = studentobj.ID;
this->stpcpy(Name,studentobj.Name);
this->GPA = studentobj.GPA;
}
Student& Student::operator=(const Student& studentobj1)
{
this->ID = studentobj.ID;
this->stpcpy(Name,studentobj.Name);
this->GPA = studentobj.GPA;
return *this;
}
//DERIVED CLASS FRESHMAN
class Freshman: public Student
{
private:
float *CS1; //a.) test score variable dynamic array
int number_of_scores;
double average;
public:
Freshman();
void getValue();
void setValue();
~Freshman();
Freshman(const Freshman &freshmanobj);
Freshman& operator=(const Freshman &freshmanobj);
};
Freshman::Freshman(const Freshman &freshmanobj):Student(juniorObj)
{
this->number_of_scores =freshmanobj.number_of_scores;
this->CS1 = new float[this->number_of_scores];
for(int i=0; i<number_of_scores;i++)
this->CS1[i] = freshmanobj.CS1[i];
this->average = freshmanobj.average;
}
Freshman& Freshman::operator=(const Freshman& freshmanobj1)
{
this->number_of_scores =freshmanobj1.number_of_scores;
this->CS1 = new float[this->number_of_scores];
for(int i=0; i<number_of_scores;i++)
this->CS1[i] = freshmanobj1.CS1[i];
this->average = freshmanobj1.average;
return *this;
}
//DERIVED CLASS JUNIOR
class Junior: public Student
{
private:
float *CS8; //a.) test score variable dynamic array
int number_of_scores;
double average;
public:
Junior();
void getValue();
void setValue();
~Junior();
Junior(const Junior& juniorObj);
Junior& operator=(const Junior& juniorObj1);
};
Junior::Junior(const Junior &juniorObj):Student(juniorObj)
{
this->number_of_scores =juniorObj.number_of_scores;
this->CS8 = new float[this->number_of_scores];
for(int i=0; i<number_of_scores;i++)
this->CS8[i] = juniorObj.CS8[i];
this->average = juniorObj.average;
}
Junior& Junior::operator=(const Junior& juniorObj1)
{
this->number_of_scores =juniorObj1.number_of_scores;
this->CS8 = new float[this->number_of_scores];
for(int i=0; i<juniorObj1;i++)
this->CS8[i] = juniorObj1.CS8[i];
this->average = juniorObj1.average;
return *this;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.