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 an Application that extends the Thread class, call it MyThread.java. Have your new class accept...
Write an Application that extends the Thread class, call it MyThread.java. Have your new class accept an integer(i.e. 200) when it is instantiated. (MyThread mt = new MyThread(200); ) This integer number will be the number of times that this class loops and prints a message. The message should read “Thread Running...200”. The 200 would be whatever number is passed into the constructor. If the number was 300, then the output should read “ Thread Running ..... 300”. Use a...
Create a class to represent a Mammal object that inherits from (extends) the Animal class. View...
Create a class to represent a Mammal object that inherits from (extends) the Animal class. View javadoc for the Mammal class and updated Animal class in homework 4 http://comet.lehman.cuny.edu/sfakhouri/teaching/cmp/cmp326/s19/hw/hw4/ Use the description provided below in UML. Mammal - tailLength : double - numLegs : int + Mammal() + Mammal(double, int) + Mammal(String, int, double, double, char, double, int) //pass values to parent’s overloaded constructor //and assign valid values to tailLength, numLegs or -1 if invalid + setTailLength(double) : void //if...
The questions in this assessment use the following. class R { ... } class A extends...
The questions in this assessment use the following. class R { ... } class A extends R { ... } abstract class B extends R { ... } final class C extends R { ...} class D extends A { ... } class E extends B { ... } class F extends B { ... } // none of the classes implement a toString() method [0] Draw a class hierarchy for the classes defined above. [1] No or Yes: class...
Using your Barrel class and Temperature class create a VacuumBarrel class that extends the Barrel class...
Using your Barrel class and Temperature class create a VacuumBarrel class that extends the Barrel class and incorporates a Temperature as the private data of the VacuumBarrel. Provide constructors, add(VacuumBarrel), subtract(VacuumBarrel), divide(int), equals(VacuumBarrel), greaterThan(VacuumBarrel) (that has one parameter and returns true if the this is greater than the parameter), toKelvin(), toFahrenheit(), toCelsius(), and toString() methods. Use a similar demo to the one you used with the Temperature class. This demo will put instances of VacuumBarrel in three arrays and manipulates...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT