Question

C++ Programming Assignment S Mammal Lab This labs goal is to give you some practice using inheritance, virtual functions, po

Use the randO function to generate a random weight between 0 and 150 pounds. (Think modulus. Note: they are small horses and

To give you an idea of the general criteria that will be used for grading, here is a checklist that you might find helpful: P
C++ Programming Assignment S Mammal Lab This lab's goal is to give you some practice using inheritance, virtual functions, pointers, dynamic memory allocation, random numbers, and polymorphism. To complete the lab implement the following steps: Create a class called Mammal. All mammals have a weight and a name, so its data should be the mammal's weight and name. Provide a default constructor that sets the mammal's weight to 0 and name to null, and another constructor that allows the weight and name to be set by the client. To allow tracking of how your code executes, your constructors and destructors should each output a message when they are called (e.g.. Invoking Mammal default constructor" for the default constructor). Your Mammal class should also have a method called Speak that cannot be implemented. That is, it should be declared as a purely virtual function. Your class should also have Get and Set methods to allow the weight and name to be accessed 1. 2. From the Mammal class, derive Dog, Cat, Horse, and Pig classes. The derived classes should each have constructor and destructors that output an appropriate message (e g, the Dog constructor outputs Invoking Dog constructorand that allow the weight and Comment (US1) Perhaps something like this to clarify name of the Mammal to be initialized (think member initialization list) The derived classes should each have a member function called Speak that overrides the Mammal Speak version. Dog Speak should output Woof, Cat Speak should output Meow Comment lGMICD2RI!!nderstand hought there, but it changes the meaning It's not cause and effect I cas see bow case and Horse Speak should output "I'm Mr. Ed,"and Pig Speak should output "Oink effect would be implied with the allows beag te gular form please leave thr here, but change alows to allow Thai' 3. Write a main function that uses the Mammal and derived classes as needed to do the following. You must perform the actions below in the sequence described i.e., do not take a shortcut around using dynamic memory allocation deallocation and virtual methods since they are the whole point of the lab)
Use the randO function to generate a random weight between 0 and 150 pounds. (Think modulus. Note: they are small horses and pigs.) Your program should use a seed value of 100 and set the seed only once. a. b. Prompt the user to make an animal selection (eg., (1) for dog, (2) for cat, (3) for horse, and (4) for pig) and to enter a name for the animal. Dynamically create a Dog, Cat, Horse, or Pig object (depending on what the user entered) and initialize it with a constructor to which is passed its weight and name. Save the object (think array). Repeat steps a. and b. 4 more times. You do not know what animals the user will select or in what order, so you must figure out how to create and store the appropriate objects. c. d. After the user has entered all 5 selections, execute another loop that cycles through the 5 selections and invokes the Speak method and also displays the name and weight for the animal. If you have done it properly, each of your outputs will correspond to the type of Mammal the user selected in the order he or she entered them
To give you an idea of the general criteria that will be used for grading, here is a checklist that you might find helpful: Program executes without crashing Appropriate Internal Documentation Base Mammal Class: Correct Private Data Members Constructors function appropriately (correct number of constructors, numberof arguments, output of messages, etc.) Correct get and set functions Derived Dog, Cat, Pig, and Horse classes Correct Private Data Members Constructors function appropriately (correct number of constructors, number of arguments, output of messages, etc.) Correct get and set functions Speak function implementation Dynamic allocation of mammals Random generation of weights and correct seed value Output contains appropriate information Array used to store 5 mammals correctly Memory is de allocated appropriately
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<cstdlib>
#include<string.h>

using namespace std;

class Mammal
{
private:
float weight;
char name[30];
public:
// CONSTRUCTOR member function
Mammal()
{
weight=0;
strcpy(name,"null");
cout<<"Invoking Mammal default constructor"<<endl;
}
Mammal(float w, char s[])
{
weight=w;
strcpy(name,s);
}
// DESTRUCTOR member function
~Mammal()
{
cout<<"Invoking Mammal destructor"<<endl;
}
// MUTATOR member functions
void setWeight(float w)
{
weight=w;
}
void setName(char s[])
{
strcpy(name,s);
}
// ACCESSOR member functions
float getWeight()
{
return weight;
}
string getName()
{
return name;
}
// PURE VIRTUAL function
virtual void speak()=0;
};

class Dog:public Mammal
{
public:
// CONSTRUCTOR member function
Dog()
{
cout<<"Invoking Dog default constructor"<<endl;
}
~Dog()
{
cout<<"Invoking Dog destructor"<<endl;
}
Dog(float w, char s[]):Mammal(w,s){}

void speak()
{
cout<<"Woof"<<endl;
}
};

class Cat:public Mammal
{
public:
// CONSTRUCTOR member function
Cat()
{
cout<<"Invoking Cat default constructor"<<endl;
}
~Cat()
{
cout<<"Invoking Cat destructor"<<endl;
}
Cat(float w, char s[]):Mammal(w,s){}

void speak()
{
cout<<"Meow"<<endl;
}
};

class Horse:public Mammal
{
public:
// CONSTRUCTOR member function
Horse()
{
cout<<"Invoking Horse default constructor"<<endl;
}
~Horse()
{
cout<<"Invoking Horse destructor"<<endl;
}
Horse(float w, char s[]):Mammal(w,s){}

void speak()
{
cout<<"I'm Mr. Ed"<<endl;
}
};

class Pig:public Mammal
{
public:
// CONSTRUCTOR member function
Pig()
{
cout<<"Invoking Pig default constructor"<<endl;
}
~Pig()
{
cout<<"Invoking Pig destructor"<<endl;
}
Pig(float w, char s[]):Mammal(w,s){}

void speak()
{
cout<<"Oink"<<endl;
}
};


int main()
{
char name[30];
srand(100);
int weight;

Mammal *m[5];
Dog *d;
Cat *c;
Horse *h;
Pig *p;
int i,j=0;

do{
cout<<"1.Dog"<<endl<<"2.Cat"<<endl<<"3.Horse"<<endl<<"4.Pig"<<endl<<endl;
cout<<"Select any animal from the above"<<endl;
cin>>i;

if(i<1 && i>4) continue;

cout<<"Enter the name for animal"<<endl;
cin>>name;
weight=rand()%151;
switch(i)
{
case 1:
{
d = new Dog(weight,name);
m[j++]=d;

}
break;
case 2:
{
c = new Cat(weight,name);

m[j++]=c;
}
break;
case 3:
{
h = new Horse(weight,name);
m[j++]=h;

}
break;
case 4:
{
p = new Pig(weight,name);
m[j++]=p;

}
break;
}
if(j==5) break;
}while(1);
cout<<endl;

for(i=0; i<5 ; i++)
{
cout<<"Speak of the animal is: ";
m[i]->speak();
cout<<"Name of the animal is: "<<m[i]->getName();
cout<<endl;
cout<<"Weight of the animal is: "<<m[i]->getWeight();
cout<<endl;
cout<<endl;
}

/* Deallocation of memory */
delete d;
delete c;
delete h;
delete p;


return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Programming Assignment S Mammal Lab This lab's goal is to give you some practice using inheritance, virtual functions, pointers, dynamic memory allocation, random numbers, and polym...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

  • Please help me with the following question. This is for Java programming. In this assignment you...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

  • Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic me...

    Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic memory allocation, pointers, recursion, and debugging Mandatory Instructions Develop a C++ object oriented solution to the Towers of Hanoi puzzle. Your solution will involve designing two classes one to represent individual Disk and another to represent the TowersOfHanoi game. TowersOfHanoi class will implement the game with three linked lists representing disks on each...

  • Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment...

    Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment also aims at creating a C++ project to handle multiple files (one header file and two .cpp files) at the same time. Remember to follow documentation and variable name guidelines. Create a C++ project to implement a simplified banking system. Your bank is small, so it can have a maximum of 100 accounts. Use an array of pointers to objects for this. However, your...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • Objective In this assignment, you will practice solving a problem using object-oriented programming and specifically, you...

    Objective In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will use the concept of object aggregation (i.e., has-a relationship between objects). You will implement a Java application, called MovieApplication that could be used in the movie industry. You are asked to implement three classes: Movie, Distributor, and MovieDriver. Each of these classes is described below. Problem Description The Movie class represents a movie and has the following attributes: name (of type String), directorName...

  • Need some help with this C++ code. Please screenshot the code if possible. Purpose: Demonstrate the...

    Need some help with this C++ code. Please screenshot the code if possible. Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment also aims at creating a C++ project to handle multiple files (one header file and two .cpp files) at the same time. Remember to follow documentation and variable name guidelines. Create a C++ project to implement a simplified banking system. Your bank is small, so it can have a maximum of...

  • Overview This assignment will give you experience on the use of classes. Understand the Application Every...

    Overview This assignment will give you experience on the use of classes. Understand the Application Every internet user–perhaps better thought of as an Internet connection–has certain data associated with him/her/it. We will oversimplify this by symbolizing such data with only two fields: a name ("Aristotle") and a globally accessible IP address ("139.12.85.191"). We could enhance these by adding other—sometimes optional, sometimes needed—data (such as a MAC address, port, local IP address, gateway, etc), but we keep things simple and use...

  • This java assignment will give you practice with classes, methods, and arrays. Part 1: Player Class...

    This java assignment will give you practice with classes, methods, and arrays. Part 1: Player Class Write a class named Player that stores a player’s name and the player’s high score. A player is described by:  player’s name  player’s high score In your class, include:  instance data variables  two constructors  getters and setters  include appropriate value checks when applicable  a toString method Part 2: PlayersList Class Write a class that manages a list...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT