OOP - Spring24 - MidPaper Soln
OOP - Spring24 - MidPaper Soln
int main() {
Customer c1,c2;
Admin a1;
a1.insert(c1);
c1.display();
a1.gettotal(c2);
return 0;
}
PMAS Arid Agriculture University Rawalpindi
University Institute of Information Technology
Question #3: 07 Marks
Imagine you are developing an advanced Zoo Management System to manage various types of animals at a zoo.
You have identified three main categories of animals: Mammals, Birds, and Reptiles. Each category has specific
attributes and behaviors that need to be managed in the system. Also, each category has some common attributes
and behaviors as well, that can be grouped into two separate classes: Animal and Habitat. Additionally, you have
identified two categories of mammals: Carnivore and Herbivore. Define the following class definitions:
Animal:
Attributes: name, age, species
Method: displayInfo() to display the animal's information.
Habitat:
Attributes: habitatType, climate
Method: displayHabitat() to display the habitat's information.
Mammal:
Specialized Attribute: furColor
Behavior: makeSound() method that prints "Roar!" for mammals.
Bird:
Specialized Attribute: wingSpan
Behavior: makeSound() method that prints "Tweet!" for birds.
Reptile:
Specialized Attribute: scalePattern
Behavior: makeSound() method that prints "Hiss!" for reptiles.
Carnivore:
Specialized Behavior: eat() method that prints "Eating meat".
Herbivore:
Specialized Behavior: eat() method that prints "Eating plants".
Write a main() function by creating objects of Carnivore and Herbivore classes, setting their
attributes while creating objects, and calling their displayInfo(), displayHabitat(), makeSound(),
and eat() methods to verify their behaviors.
Solution:
#include <iostream>
using namespace std;
class Animal {
string name;
int age;
string species;
public:
Animal(string n, int a, string s) : name(n), age(a), species(s)
{}
void displayInfo() {
cout << "Name: " << name << ", Age: " << age << ", Species: " << species << endl;
}
};
class Habitat {
string habitatType;
string climate;
public:
Habitat(string ht, string c) : habitatType(ht), climate(c)
{}
void displayHabitat() {
cout << "Habitat Type: " << habitatType << ", Climate: " << climate << endl;
PMAS Arid Agriculture University Rawalpindi
University Institute of Information Technology
}
};
class Mammal : public Animal, public Habitat {
string furColor;
public:
Mammal(string n, int a, string s, string ht, string c, string fc) : Animal(n, a, s), Habitat(ht, c),
furColor(fc)
{}
void makeSound() {
cout << "Roar!" << endl;
}
};
class Bird : public Animal, public Habitat {
double wingSpan;
public:
Bird(string n, int a, string s, string ht, string c, double ws): Animal(n, a, s), Habitat(ht, c),
wingSpan(ws)
{}
void makeSound() {
cout << "Tweet!" << endl;
}
};
class Reptile : public Animal, public Habitat {
string scalePattern;
public:
Reptile(string n, int a, string s, string ht, string c, string sp): Animal(n, a, s), Habitat(ht, c),
scalePattern(sp)
{}
void makeSound() {
cout << "Hiss!" <<endl;
}
};
class Carnivore : public Mammal {
public:
Carnivore(string n, int a, string s, string ht, string c, string fc): Mammal(n, a, s, ht, c, fc)
{}
void eat() {
cout << "Eating meat" << endl;
}
};
int main() {
Carnivore lion("Sheroo", 5, "Lion", "Grassland", "Tropical", "Golden");
PMAS Arid Agriculture University Rawalpindi
University Institute of Information Technology
Herbivore deer("Cuteoo", 3, "Deer", "Forest", "Temperate", "Brown");
lion.displayInfo();
lion.displayHabitat();
lion.makeSound();
lion.eat();
deer.displayInfo();
deer.displayHabitat();
deer.makeSound();
deer.eat();
return 0;
}