0% found this document useful (0 votes)
24 views5 pages

OOP - Spring24 - MidPaper Soln

The document describes an assignment question asking students to design classes to model different types of animals and their habitats in a zoo management system. It includes classes for Animal, Habitat, Mammal, Bird, Reptile, Carnivore and Herbivore and provides example code implementing these classes and demonstrating their use.

Uploaded by

farazbashir15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

OOP - Spring24 - MidPaper Soln

The document describes an assignment question asking students to design classes to model different types of animals and their habitats in a zoo management system. It includes classes for Animal, Habitat, Mammal, Bird, Reptile, Carnivore and Herbivore and provides example code implementing these classes and demonstrating their use.

Uploaded by

farazbashir15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PMAS Arid Agriculture University Rawalpindi

University Institute of Information Technology


Mid Term Paper (Spring-2024)
BSCS/BSSE (Morning/Evening)
Object Oriented Programming (CSC-102)
Maximum Marks: 18 Total Time: 1.5 Hr
-------------------------------------------------------------------------------------------------------------------------------
Question #1: 04 Marks
Assuming there is no error and without changing the main function, define the required class and its members such
that the main function compiles and execute without any error. Don’t ignore the comments written in main function:
int main() {
Player p1;
Player p2(3, “Shaheen”); //where first argument is ID and second is Name, both cannot access from main()
p2.showRecord(); //This function is not allowed to modify any data member of the class.
//After the complete execution of main() the message “Player Removed” will display two times.
return 0; }
Solution:
#include <iostream>
using namespace std;
class Player
{
int ID;
string Name;
public:
Player()
{//student may initialize data members here
}
Player(int i, string s)
{
ID=i;
Name=s;
}
void showRecord()const
{
cout<<ID<<Name;
}
~Player()
{
cout<<"\nPlayer Removed";
}
};
Question #2: 07 Marks
DBMS (Database Management System) have two type of users, Admin and the Customer. THE DBMS is being
design in such a way that both admin and customers have separate views, the admin-view and the customer-view,
the admin view has all the rights to store, delete and update the records. On the other hand, the customer view is
only allowed to check the records. Customer only allow Admin to check all the data members (including private) of
the Customer while Customer cannot check the records of admin. The Admin can also perform an operation to
check how many Customers are registered in DBMS.
You are asked to implement this scenario by using the relevant concept of OOP.
Solution:
#include <iostream>
using namespace std;
class Customer
PMAS Arid Agriculture University Rawalpindi
University Institute of Information Technology
{
string name;
int id;
static int total;
public:
Customer()
{
total++;
}
void display()const
{
cout<<endl<<name<<id;
}
friend class Admin;
};
int Customer::total=0;
class Admin
{
public:
void store(Customer & c)
{
c.name="kashif";
c.id=3;;
}
void del(Customer & c)
{}
void update(Customer & c)
{}
void gettotal(Customer c)
{
cout<<c.total;
}
};
int main() {
Customer c1,c2;
Admin a1;
a1.store(c1);
c1.display();
a1.gettotal(c2);
return 0;
}

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;
}
};

class Herbivore : public Mammal {


public:
Herbivore(string n, int a, string s, string ht, string c, string fc): Mammal(n, a, s, ht, c, fc)
{}
void eat() {
cout << "Eating plants" << 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;
}

You might also like