I started with a super class names Animal (as seen in the photo) but below We on
ID: 3716754 • Letter: I
Question
I started with a super class names Animal (as seen in the photo) but below We only try to describe “Chicken” animal here. Here we have a class “Egg” defined for the “Chicken” class. class Egg { private: ?int size; public: ?Egg()=default; ?Egg(int size):size{size}{} ?void setSize(int size) ?{ ??this->size=size; ?} ?int getSize() const ?{ ??return size; ?} }; Create a subclass “Chicken” inherited from “Animal” (in single file format), it includes the following attributes and methods declarations: (20 pts.) a. An “Egg” pointer which is used to create a dynamic array of “Egg” objects on the heap. b. An integer indicating how big (the size) the chicken is c. Default constructor d. Overloaded constructor which initializes all attributes (including inherited ones) through parameters e. Copy constructor f. Destructor g. Function “display” #include <iostream> using std::cout; using std::endl; class Chicken : public Animal { private: ?Egg* egg{};//used to create a dynamic array of ‘Egg’ class ?int size; public: ?Chicken()=default; //complete overloaded constructor here… ? class Egg { private: ?int size; public: ?Egg()=default; ?Egg(int size):size{size}{} ?void setSize(int size) ?{ ??this->size=size; ?} ?int getSize() const ?{ ??return size; ?} }; Create a subclass “Chicken” inherited from “Animal” (in single file format), it includes the following attributes and methods declarations: (20 pts.) a. An “Egg” pointer which is used to create a dynamic array of “Egg” objects on the heap. b. An integer indicating how big (the size) the chicken is c. Default constructor d. Overloaded constructor which initializes all attributes (including inherited ones) through parameters e. Copy constructor f. Destructor g. Function “display” #include <iostream> using std::cout; using std::endl; class Chicken : public Animal { private: ?Egg* egg{};//used to create a dynamic array of ‘Egg’ class ?int size; public: ?Chicken()=default; //complete overloaded constructor here… ?Explanation / Answer
Please find my code.
#include <iostream>
#include <ostream>
using namespace std;
class Animal {
protected:
int amount;
string livestock;
public:
// default constructor
Animal() {
}
// overloaded constructor
Animal(int a, string s) {
amount = a;
livestock = s;
}
// destructor
~Animal() {
}
// virtual function
virtual void display() {
cout<<amount<<", "<<livestock<<endl;
}
};
class Chicken : public Animal
{
private:
?Egg* egg{};//used to create a dynamic array of ‘Egg’ class
?int size;
public:
?Chicken()=default;
//complete overloaded constructor here…
Chicken(int amount, string livestock, int s) : Animal(amount, livestock) {
size = s;
egg = new Egg[size];
}
// copy constructo
Chicken(Chicken &oth) {
amount = oth.amount;
livestock = oth.livestock;
size = oth.size;
if(egg != NULL)
delete egg;
egg = new Egg[size];
for(int i=0; i<size; i++)
egg[i] = oth.egg[i];
}
//destructor
~Chicken() {
if(egg != NULL)
delete egg;
}
void display() {
Animal::display();
cout<<"Size: "<<size<<endl;
}
};
?