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.
#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;
}
Comments
Leave a comment