Assignment 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CE101T – Object Oriented Programming

Section: Assignment # 7 Total marks: 35


Name : __________________ Roll number : __________________

Submission:
• Email instructor or TA if there are any questions. You cannot look at others’ solutions or use others’
solutions, however, you can discuss it with each other. Plagiarism will be dealt with according to the
course policy.
• Submission after due time will not be accepted.

Follow this naming convention for your report, it should highlight difficulties you faced and things you
learned in this assignment. Naming Pattern ( Roll#_Assignment#.pdf e.g BSCE23000_Assignment3.pdf )
In this assignment you have to do following tasks:
Task 1: Ensure that you have installed all three softwares in your personal computer (Github, Cygwin
& CLion). Now, accept the assignment posted in Google Classroom and after accepting, clone the
repository to your computer. Make sure you have logged into the github app with your account.
Task 2: Open Cygwin app, Move to your code directory with following command “cd
<path_of_folder>”
<path_of_folder> can be automatically populated by dragging the folder and dropping it to the
cygwin window.
Run the code through Cygwin, use command “make run”, to get the output of the code
Task 3: Solve the given problems, write code using CLion or any other IDE.
Task 4: Keep your code in the respective git cloned folder.
Task 5: Commit and Push the changes through the Github App
Task 5: Write the code in separate files (structure_name.h, structure_name.cpp) for each struct, declare
struct variables, and call functions from main.cpp. Ensure that file names are in lowercase.
Task 6: Run ‘make run’ to run C++ code
Task 7: Run ‘make test’ to test the C++ code

Object Oriented Programming


CE101T-Spring 2024
Problem Statement: Inheritance, Operator Overloading,
Composition, & Aggregation

Task 1 & 2:
Continue from Lab 7 and complete the second and third tasks from the lab as Task 1 and Task 2
for Assignment 7.

Task 3: Zoo Simulation: Write a program for creating a Zoo simulation in C++. The Zoo
will have two types of animals: Lions and Elephants. Each animal will have a name, age, and a
unique sound they make. Your task is to implement the necessary classes to represent these animals
and display their details. Additionally, you need to merge two zoos to create a bigger zoo.
Instructions:
1. Create an Animal class as the base class. This class should have the following attributes
and functionalities:
It has private data members such as:
• name (string),
• age (integer).
It has public member functions such as:
• A parameterized constructor to initialize the attributes (name, age).
• int getName() to return the name of the animal.
• int getAge() to return the age of the animal.
• void makeSound() to print the sound of the animal.
2. Create two derived classes: Lion and Elephant. Each of these classes should inherit from
the Animal class and represent the corresponding animal type.
It has public member functions such as:
• A parameterized constructor to initialize the name, and age of the animal.
• Implement the makeSound() function in each derived class to output the sound of the
corresponding animal type.
3. Create a ZooUsingComposition class that will act as a container for storing animals. This
class should have the following functionalities:

Object Oriented Programming


CE101T-Spring 2024
It has private data members such as:
• maxAnimals (const int) for array size
• Animal animals[maxAnimals] (An array animals to store pointers to Animal objects)
• numAnimals(int) (to keep track of the number of animals in the zoo)
It has public member functions such as:
• A parameterized constructor to initialize numAnimals to zero.
• void addAnimal(Animal animal): Add an animal to the zoo. The function should take
a pointer to the Animal object and add it to the animals array. The zoo can hold up to 10
animals.
• void displayAllAnimals(): Display the details of all the animals in the zoo, including
their names, ages, and sounds.
• Implement the operator+ function in the Zoo class to merge two zoos. The function
should take another Zoo object as a parameter and return a new Zoo object containing all
the animals from both zoos.
Hint: Merging of two arrays
• Implement the stream out operator function in the Zoo class to print the following:
************Composition***********
Composition is a unidirectional relationship
Composition must have the following properties:
The component is an integral part of the composed class and its objects.
Each object of the composed class has its own instance of the component.
The composed class manages the creation, lifetime, and destruction of the component
within each of its objects.
The component, as part of an object, is not aware of the composed class or other objects of
the same class.
************Composition***********
4. Create a ZooUsingAggregation class that will act as a container for storing animals. This
class should have the following functionalities:
It has private data members such as:
• maxAnimals (const int) for array size

Object Oriented Programming


CE101T-Spring 2024
• Animal *animals[maxAnimals] (An array animals to store pointers to Animal objects)
• numAnimals(int) (to keep track of the number of animals in the zoo)
It has public member functions such as:
• A parameterized constructor to initialize numAnimals to zero.
• void addAnimal(Animal *animal): Add an animal to the zoo. The function should take
a pointer to the Animal object and add it to the animals array. The zoo can hold up to 10
animals.
• void displayAllAnimals(): Display the details of all the animals in the zoo, including
their names, ages, and sounds.
• Implement the operator+ function in the ZooUsingAggregation class to merge two
zoos. The function should take another ZooUsingAggregation object as a parameter and
return a new ZooUsingAggregation object containing all the animals from both zoos.
Hint: Merging of two arrays
• Implement the stream out operator function in the ZooUsingAggregation class to print
the following:
************Aggregation***********
Aggregation is a unidirectional relationship
Aggregation must have the following properties:
The component is an integral part of the containing class and its objects.
The part (member) can belong to more than one object (class) at a time.
The part (member) does not have its existence managed by the object (class)
The part (member) does not know about the existence of the object (class)
************Aggregation***********

In the main() function:


• Create different animal objects of each type (Lion and Elephant).
• Create two zoos, add animals to them, and display the details of all the
animals in each zoo.
• Merge the two zoos using the + operator and display the details of all the

Object Oriented Programming


CE101T-Spring 2024
animals in the merged zoo.
• Pass the test cases as required.

Object Oriented Programming


CE101T-Spring 2024

You might also like