Answer to Question #206369 in C++ for Ah made

Question #206369

Following is the diagram for a hierarchy of Mammals, Map the code into C++, each class should contain a

function Sound which can take some parameters and produce a sound.


Parent class: Mammal

-eyeColor:int

+getEyeColor:int

Classes inherited from Mammal:

1)class Dog

-barkFrequency:int

+bark:void

2)class Cat

MeowFrequency:int

+meow:void

Classes inherited from Dog

A) class GermanShephard

+isGerman()

B) class Poodle

+isFrench:void

Implement:

 Inheritance

 Static Polymorphism

 A main( ) which can test all the classes



1
Expert's answer
2021-06-13T10:24:26-0400
#include <iostream>
using namespace std;


class Mammal{
    protected:
        int eyeColor;
    
    public:
        Mammal(int color = 1){
            this->eyeColor = color;
        }
        
        int getEyeColor(){
            return this->eyeColor;
        }
};


class Dog: public Mammal{
    protected:
        int barkFrequency;
        
    public:
        Dog(int frequency = 1){
            this->barkFrequency = frequency;
        }
        
        void bark(){
            string s = "";
            for(int i = 0; i < this->barkFrequency; i++){
                s += "woow ";
            }
            cout<<s<<"\n";
        }
        //static polymorphism. Dog barks for a given frequency.
        void bark(int frequency){
            string s = "";
            for(int i = 0; i < frequency; i++){
                s += "woow ";
            }
            cout<<s<<"\n";
        }
};


class Cat: public Mammal{
    protected:
        int MeowFrequency;
        
    public:
        Cat(int frequency = 1){
            this->MeowFrequency = frequency;
        }
        
        void meow(){
            string s = "";
            for(int i = 0; i < this->MeowFrequency; i++){
                s += "meow ";
            }
            cout<<s<<"\n";
        }
        //static polymorphism. Cat meows for a given frequency.
        void meow(int frequency){
            string s = "";
            for(int i = 0; i < frequency; i++){
                s += "meow ";
            }
            cout<<s<<"\n";
        }
};


class GermanShephard: public Dog{
    protected: 
        bool germen;
        
    public:
        GermanShephard(){
            this->germen = 1;
        }
        
        bool isGerman(){
            return germen;
        }
};


class Poodle: public Dog{
    protected:
        bool french;
        
    
    public:
        Poodle(){
            this->french = 1;
        }
    
        bool isFrench(){
            return french;
        }
    
};


int main() {
    Mammal m(3);
    std::cout << "Eye collor of Mammal : "<<m.getEyeColor() << std::endl;


    
    Dog dog1(5);
    std::cout << "Dog is barking : "<< std::endl;
    dog1.bark();
    
    Cat cat1(7);
    std::cout << "Cat is meowing : "<< std::endl;
    cat1.meow();
    
    GermanShephard g1;
    std::cout << "Is it a GermanShephard?: " <<std::endl;
    if(g1.isGerman()){
        std::cout << "Yes" << std::endl;
    }
    else{
        std::cout << "No" << std::endl;
    }
    std::cout << "Dog is barking : "<< std::endl;
    g1.bark();      //this is test of inheritence. GermanShephard object is calling Dog's function.
    g1.bark(10);   //this is test of inheritence and polymorphism at the same time.
    
    Poodle p1;
    std::cout << "Is it a Poodle?: " <<std::endl;
    if(p1.isFrench()){
        std::cout << "Yes" << std::endl;
    }
    else{
        std::cout << "No" << std::endl;
    }
    std::cout << "Dog is barking : "<< std::endl;
    p1.bark();      //this is test of inheritence. Poodle object is calling Dog's function.
    p1.bark(10);  //this is test of inheritence and polymorphism at the same time.
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog