Build two classes: Mammal and Dog, where Dog will be derived from Mammal. You ar
ID: 3852481 • Letter: B
Question
Build two classes: Mammal and Dog, where Dog will be derived from Mammal. You are given the code for the Mammal class and are expected to build a Dog class (using Dog.h and Dog.cpp) in similar fashion, except that Dog should be derived from Mammal:
Mammal.h:
#include <iostream> using namespace std;
class Mammal {
public: Mammal();
~Mammal();
virtual void Move() const; virtual void Speak() const;
protected:
int move;
};
Mammal.cpp:
#include "Mammal.h"
Mammal::Mammal() : move(1) {
cout << "Mammal constructor..." << endl; }
Mammal::~Mammal() {
cout << "Mammal destructor..." << endl; }
void Mammal::Move() const {
cout << "Mammal moves " << move << " step!" << endl; }
void Mammal::Speak() const {
cout << "What does a mammal speak? Mammilian!" << endl; }
Explanation / Answer
The given derived class Dog code is given as below.Hope it will be an understandable code.Here it goes:
Dog.h
#include <iostream>
using namespace std;
class Dog:public Mammal {
public: Dog();
~Dog();
virtual void Move() const; virtual void Speak() const;
protected:
int move;
};
Dog.cpp:
#include "Dog.h"
Dog::Dog() : move(1) {
cout << "Dog constructor..." << endl; }
Mammal::~Dog() {
cout << "Dog destructor..." << endl; }
void Dog::Dog() const {
cout << "Dog moves " << move << " step!" << endl; }
void Dog::Speak() const {
cout << "What does a Dog speak? Doggian!" << endl; }
Please rate the answer if it helped.....Thankyou
Hope it helps....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.