Question

In: Computer Science

Write a class that extends the LeggedMammal class from the previous laboratory exercise.

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

Solutions

Expert Solution

// source.cpp

#include
#include "dog.h"
using namespace std;

int main()
{
   dog A("X", 10, true, 4, "F", true);

   cout << A.getBreed() << endl;
   cout << A.getIsRegistered() << endl;
   cout << A.getKindofFur() << endl;
   cout << A.getNumberofLegs() << endl;
   cout << A.getPresenceofTail() << endl;
   cout << A.getSize() << endl << endl;

   A.setBreed("Y");
   A.setIsRegistered(false);
   A.setSize(20);

   cout << A.getBreed() << endl;
   cout << A.getIsRegistered() << endl;
   cout << A.getKindofFur() << endl;
   cout << A.getNumberofLegs() << endl;
   cout << A.getPresenceofTail() << endl;
   cout << A.getSize() << endl << endl;

   // this is one weird looking dog.
   dog *B = new dog("Z", 30, false, 2, "LOL", false);

   cout << B->getBreed() << endl;
   cout << B->getIsRegistered() << endl;
   cout << B->getKindofFur() << endl;
   cout << B->getNumberofLegs() << endl;
   cout << B->getPresenceofTail() << endl;
   cout << B->getSize() << endl << endl;

   return 0;
}

// leggedmammel.h

#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();
   void setNumberofLegs(int);
   void setKindofFur(string);
   void setPresenceofTail(bool);
   // 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;
}

// mutators
void leggedmammal::setNumberofLegs(int legs)
{
   this->mNumberOfLegs = legs;
}

void leggedmammal::setKindofFur(string fur)
{
   this->mKindOfFur = fur;
}

void leggedmammal::setPresenceofTail(bool tail)
{
   this->mPresenceOfTail = tail;
}

// Purpose: To limit the change of our properties.
// Never allow the properties to be reassigned
// after we initialize our class.

#endif

// dog.h

#pragma once
#include "leggedmammal.h"
using namespace std;
class dog
   : public leggedmammal
{
   // Limit the access to our properties
   // Declare them as Private
private:
   string breed;
   double size;
   bool isRegistered;

   // public methods of our class.
public:
   dog() = default;
   dog(string, double, bool, int, string, bool);
   string getBreed();
   double getSize();
   bool getIsRegistered();
   void setBreed(string);
   void setSize(double);
   void setIsRegistered(bool);
   ~dog() = default;
};

// Note: This is for readability purposes.
// Define the body outside of our class.

dog::dog(string breed, double size, bool isRegistered, int legs, string fur, bool tail) :leggedmammal(legs, fur, tail)
{
   this->breed = breed;
   this->size = size;
   this->isRegistered = isRegistered;
}

// accessors
string dog::getBreed()
{
   return breed;
}

double dog::getSize()
{
   return size;
}

bool dog::getIsRegistered()
{
   return isRegistered;
}

// mutators
void dog::setBreed(string breed)
{
   this->breed = breed;
}

void dog::setSize(double size)
{
   this->size = size;
}

void dog::setIsRegistered(bool isRegistered)
{
   this->isRegistered = isRegistered;
}


Related Solutions

c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Package pacman.score Class ScoreBoard Object ScoreBoard public class ScoreBoard extends Object ScoreBoard contains previous scores and...
Package pacman.score Class ScoreBoard Object ScoreBoard public class ScoreBoard extends Object ScoreBoard contains previous scores and the current score of the PacmanGame. A score is a name and value that a valid name only contains the following characters: A to Z a to z 0 to 9 and must have a length greater than 0. The value is a integer that is equal to or greater than 0. Implement this for Assignment 1 Constructor Summary Constructors Constructor Description ScoreBoard() Creates...
Package pacman.score Class ScoreBoard Object ScoreBoard public class ScoreBoard extends Object ScoreBoard contains previous scores and...
Package pacman.score Class ScoreBoard Object ScoreBoard public class ScoreBoard extends Object ScoreBoard contains previous scores and the current score of the PacmanGame. A score is a name and value that a valid name only contains the following characters: A to Z a to z 0 to 9 and must have a length greater than 0. The value is a integer that is equal to or greater than 0. Implement this for Assignment 1 Constructor Summary Constructors Constructor Description ScoreBoard() Creates...
write the following programs: Ram.java, Truck.java, LandVehicle.java and Vehicle.java. public class Ram extends Truck { public...
write the following programs: Ram.java, Truck.java, LandVehicle.java and Vehicle.java. public class Ram extends Truck { public static void main(String[] args) { // your implementation } public Ram() { // your implementation } } public class Truck extends LandVehicle { public Truck() { // your implementation } public Truck(String s) { // your implementation } } public class LandVehicle extends Vehicle { public LandVehicle() { // your implementation } } public class Vehicle { public Vehicle() { // your implementation
Using the class Date that you defined in Exercise O3, write the definition of the class...
Using the class Date that you defined in Exercise O3, write the definition of the class named Employee with the following private instance variables: first name               -           class string last name                -           class string ID number             -           integer (the default ID number is 999999) birth day                -           class Date date hired               -           class Date base pay                 -           double precision (the default base pay is $0.00) The default constructor initializes the first name to “john”, the last name to “Doe”, and...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all the fields and methods from the Case class. It should also contain:  One field for a CellPhone object. Be sure to put in the appropriate access modifier. (private, public)  A constructor with 3 parameters – owner’s name, Case color, the phone number of the cell phone that will be in the Case. This constructor MUST call the super class’s constructor. Then set...
Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override...
Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override the extract()method in your class. You can use your main() method for testing your method (although you do not need to provide a main method). Recall that a list is an ordered collection of data X_0, X_1, X_2, ..., X_n-1 The extract(int start, int end) method removes all elements X_start, X_start_1, ..., X_end-1 from the list. It also returns all removed elements as a...
If the class Parrot extends the class Bird, what can't be said about the classes? Select...
If the class Parrot extends the class Bird, what can't be said about the classes? Select one answer: A. The Parrot class has access to the Parrot methods B. The Parrot class has access to the Bird methods C. The Bird class has access to the Bird methods D. The Bird class has access to the Parrot methods
13. Recall the following situation, from a previous exercise: An assembly line worker’s job is to...
13. Recall the following situation, from a previous exercise: An assembly line worker’s job is to install a particular part in a device, a task which they can do with a probability of success of 0.78 on each attempt. (Assume that a success on one attempt is independent of success on all other previous or future attempts.) Suppose that they need to install ten such parts a day. (a) What is the probability that it will take them 12 or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT