DO NOT EDIT THE CLASS OR MAIN. My program runs but it wont compile. There just s
ID: 3532352 • Letter: D
Question
DO NOT EDIT THE CLASS OR MAIN. My program runs but it wont compile. There just seems to be something wrong with the copy constructor.
#include <iostream>
using namespace std;
class CAT
{
public:
CAT(); // default constructor
CAT (const CAT &); // copy constructor
~CAT(); // destructor
int GetAge() const { return *itsAge; } //inline
int GetWeight() const { return *itsWeight; } //inline
void SetAge(int age) { *itsAge = age; }
void SetWeighte(int Weight) { *itsWeight= Weight; } //inline
private:
int *itsAge;
int *itsWeight;
};
// **** End class declaration *********
CAT::CAT()
{
*itsAge=6;
*itsWeight=0;
}
CAT::CAT(const CAT & rhs)
{
setAge.age = 5;
setWeighte.Weight = 4;
*itsAge >> *rhs.itsAge;
*itsWeight >> *rhs.itsWeight;
}
CAT::~CAT()
{
delete [] itsAge;
delete [] itsWeight;
}
// **** Driver program keep as is *********
int main()
{ char Char;
CAT Garfield;
cout << "Garfield's age: " << Garfield.GetAge() << endl;
cout << "Setting Garfield to 6... ";
Garfield.SetAge(6);
cout << "Creating Felix from Garfield ";
CAT Felix(Garfield);
cout << "Garfield's age: " << Garfield.GetAge() << endl;
cout << "Felix' age: " << Felix.GetAge() << endl;
cout << "setting Garfield to 7... ";
Garfield.SetAge(7);
cout << "Garfield's age: " << Garfield.GetAge() << endl;
cout << "boot's age: " << Felix.GetAge() << endl;
cin>>Char;
return 0;
}
Explanation / Answer
//This program will compile AND run but it will crash in an error.
//I do not know what the program is supposed to do because of the lack of comments and i do not know the logic behind //your program also, but i managed to find the problem and fix it.. you just have to fix the logic errors.
//Error: You defined SetAge function with Capital S BUT in the constructor you called it, setAge with Lowercase s.
//Change all setAge to captial S -> SetAge
#include <iostream>
using namespace std;
class CAT
{
public:
CAT(); // default constructor
CAT (const CAT &); // copy constructor
~CAT(); // destructor
int GetAge() const { return *itsAge; } //inline
int GetWeight() const { return *itsWeight; } //inline
void setAge(int age) { *itsAge = age; }
void setWeighte(int Weight) { *itsWeight= Weight; } //inline
private:
int *itsAge;
int *itsWeight;
};
// **** End class declaration *********
CAT::CAT()
{
*itsAge=6;
*itsWeight=0;
}
CAT::CAT(const CAT & rhs)
{
setAge(5);
setWeighte(4);
*itsAge >> *rhs.itsAge;
*itsWeight >> *rhs.itsWeight;
}
CAT::~CAT()
{
delete [] itsAge;
delete [] itsWeight;
}
// **** Driver program keep as is *********
int main()
{ char Char;
CAT Garfield, age;
cout << "Garfield's age: " << Garfield.GetAge() << endl;
cout << "Setting Garfield to 6... ";
Garfield.setAge(6);
cout << "Creating Felix from Garfield ";
CAT Felix(Garfield);
cout << "Garfield's age: " << Garfield.GetAge() << endl;
cout << "Felix' age: " << Felix.GetAge() << endl;
cout << "setting Garfield to 7... ";
Garfield.setAge(7);
cout << "Garfield's age: " << Garfield.GetAge() << endl;
cout << "boot's age: " << Felix.GetAge() << endl;
cin>>Char;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.