In: Computer Science
C++ code on Visual Studio Code:
Write a class that extends the LeggedMammal class from the previous laboratory exercise. The class will represent a Dog. Consider the breed, size and is registered. Initialize all properties of the parent class in the new constructor. This time, promote the use of accessors and mutators for the new properties. Instantiate a Dog object in the main function and be able to set the values of the properties of the Dog object using the mutators. Display all the properties of the Dog object using the accessors.
Here's my previous code:
#ifndef _LEGGEDMAMMAL
#define _LEGGEDMAMMAL
#include
using namespace std;
class leggedmammal{
// Limit the access to our properties
// Declare them as Private
private:
int mNumberOfLegs;
string mKindOfFur;
bool mPresenceOfTail;
// public methods of our class
public:
leggedmammal(int, string, bool);
int getNumberofLegs();
string getKindofFur();
bool getPresenceofTail();
// Note: This is for readability purposes
// Define the body outside of our class.
};
leggedmammal::leggedmammal(int legs, string fur, bool tail){
// initialization of properties
this->mNumberOfLegs - legs;
this->mKindOfFur - fur;
this->mPresenceOfTail - tail;
}
// accessors
int leggedmammal::getNumberofLegs(){
return this->mNumberOfLegs;
}
string leggedmammal::getKindofFur(){
return this->mKindOfFur;
}
bool leggedmammal::getPresenceofTail(){
return this->mPresenceOfTail;
}
// Purpose: To limit the change of our properties.
// Never allow the properties to be reassigned
// after we initialize our class.
#endif
// source.cpp #include int main() cout << A.getBreed() << endl; A.setBreed("Y"); cout << A.getBreed() << endl; // this is one weird looking dog. cout << B->getBreed() << endl; return 0; |
// leggedmammel.h #ifndef _LEGGEDMAMMAL using namespace std; class leggedmammal leggedmammal::leggedmammal(int legs, string fur, bool
tail) // accessors string leggedmammal::getKindofFur() bool leggedmammal::getPresenceofTail() // mutators void leggedmammal::setKindofFur(string fur) void leggedmammal::setPresenceofTail(bool tail) // Purpose: To limit the change of our properties. #endif |
// dog.h #pragma once // public methods of our class. // Note: This is for readability purposes. dog::dog(string breed, double size, bool isRegistered, int legs,
string fur, bool tail) :leggedmammal(legs, fur, tail) // accessors double dog::getSize() bool dog::getIsRegistered() // mutators void dog::setSize(double size) void dog::setIsRegistered(bool isRegistered) |