Answer to Question #235396 in C++ for makwapa

Question #235396

Define a class named Animal that holds private data fields name, color, age and sound. Create public functions that set (give values) and get (retrieve values) the data. Write a main() function that demonstrates the class operates correctly by creating at least one Animal object, and calling all the methods defined for the Animal class. 


1
Expert's answer
2021-09-11T23:39:52-0400
#include <iostream>


using namespace std;


class Animal{
    private:
        string name;
        string color;
        int age;
        string sound;
    public:
    void setName(string n){
        name=n;
    }
    string getName(){
        return name;
    }
    void setColor(string c){
        color=c;
    }
    string getColor(){
        return color;
    }
    void setAge(int a){
        age=a;
    }
    int getAge(){
        return age;
    }
    void setSound(string s){
        sound=s;
    }
    string getSound(){
        return sound;
    }
    void display(){
        
    }
};
int main()
{
    Animal a1;
    a1.setName("Dog");
    a1.setColor("Brown");
    a1.setAge(5);
    a1.setSound("Bark");
    cout<<"\nName: "<<a1.getName();
    cout<<"\nColor: "<<a1.getColor();
    cout<<"\nAge: "<<a1.getAge();
    cout<<"\nSound: "<<a1.getSound();
    
    Animal a2;
    a2.setName("Cat");
    a2.setColor("White");
    a2.setAge(2);
    a2.setSound("Meuw");
    cout<<"\nName: "<<a2.getName();
    cout<<"\nColor: "<<a2.getColor();
    cout<<"\nAge: "<<a2.getAge();
    cout<<"\nSound: "<<a2.getSound();


    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
APPROVED BY CLIENTS