1. The base class Dog is a class with general information about dogs. Store the
ID: 3690686 • Letter: 1
Question
1. The base class Dog is a class with general information about dogs. Store the declaration for this class in a file called lab8dog.h. The class has three protected data members to store a dog's name, color and weight. Include a prototype for a default constructor that sets the name and color to the value "Unknown" and the weight to 0.0. Include a second prototype for a constructor that accepts values for the dog's name, color and weight and assigns them to the data members. Also, include a prototype for a function called displayDog to display the values of each data member.
2. Define all functions for the Dog class in a file called lab8dog.cpp.
3. ShowDog will be a derived class with additional information about dogs who have been entered in dog shows – so a ShowDog "is a" Dog. Store the declaration for the ShowDog class in the file lab8show.h and define the functions in the file lab8show.cpp.
- This class has two private data members – the number of contests a dog has been entered in and the number of contests it has won.
- Define a default constructor that sets these two fields to zero and invokes the Dog default constructor to initialize the inherited data members
- Define a second constructor that accepts a dog's name, color, weight, number of contests entered and number won. Pass the first three arguments to the Dog constructor and use the last two to set the ShowDog's data members.
- Include a prototype for a function called displayDog. It should first call the base class's function (to display the dog's name, color and weight) and then display the number of contests entered by the dog and the number won. This function is redefining the base class version of displayDog. The function should produce an output like this:
Dog's name: Toto (first two line's printed by base class displayDog function)
Dog's weight: 25
Dog's color: Grey
Toto was entered in 5 contests,
Toto won 2 of the contests.
4. Write a client program called lab8client.cpp to demonstrate the use of Dog and ShowDog classes.
- Define a Dog object initializing the name, color and weight with "Bolt", "White" and 20.
- Define a ShowDog object with arguments for all five data members using the values "Uggie", "Blk/Wht", 30, 3, 1.
- Call the displayDog function to display the information about each of these objects.
In your main function, define an array with four elements to hold pointers of type Dog.
Use the new operator to create either a Dog or ShowDog object, storing the pointer to the object (an address) in the next element of the array.
Use the following data to initialize each new object: e.g., dogList[0] = new Dog("Bingo", 20, "Tan");
Name Weight Color #contests #won
Bingo 20 Tan
Beethoven 200 Brown 3 1
Marley 80 Yellow 10 3
Fido 50 Rust
Then write a loop to display the data for each Dog or ShowDog in the array.
Tip: In your header file for the Dog class, make your displayDog function virtual. Also, add a virtual destructor to the Dog class: virtual ~Dog( ) { } (This prevents a warning message from the compiler.)
Explanation / Answer
#include <iostream>
using namespace std;
class Dog
{
protected:
string name;
string color;
double weight;
public:
Dog()
{
name="unknown";
color="unknown";
weight=0.0;
}
Dog(string n,double w,string c)
{
name=n;
color=c;
weight=w;
}
virtual void displayDog()
{
cout<<" Dog's name: "<<this->name;
cout<<" Dog's color: "<<color;
cout<<" Dog's weight: "<<weight;
}
};
class ShowDog:public Dog
{
private:
int noContests;
int winContests;
public:
ShowDog():Dog()
{
noContests=0;
winContests=0;
}
ShowDog(string n,double w,string c,int total,int win): Dog(n,w,c)
{
noContests=total;
winContests=win;
}
void displayDog()
{
cout<<" ";
Dog::displayDog();
cout<<" "<<name <<" entered in "<<noContests<<" contests";
cout<<" " << name<<" won "<<winContests<<" contests";
}
};
int main()
{
Dog d("Bolt", 20,"White");
d.displayDog();
ShowDog sd("Uggie", 30, "Blk/Wht", 3, 1 );
sd.displayDog();
Dog dogList[4];
Dog *temp;
temp= new Dog("Bingo", 20, "Tan");
dogList[0]=*temp;
Dog *temp1 = new ShowDog("Beethoven", 200, "Brown",3,1);
dogList[1]=*temp1;
temp1= new ShowDog("Marley", 80, "Yellow",10,3);
dogList[2]=*temp1;
temp= new Dog("Fido", 50, "Rust");
dogList[3]=*temp;
for(int i=0;i<4;i++)
dogList[i].displayDog();
cout<<" ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.