Programming 2 Week 2
Programming 2 Week 2
These assignments introduce core concepts like inheritance, abstract classes, and method
overriding, providing a solid foundation in object-oriented programming.
These assignments expand on the core concepts by applying inheritance, abstract classes, and
method overriding in a more complex, real-world context.
Task:
• Create a base class Vehicle with a property Model (string) and a method void
StartEngine() that prints "Engine started".
• Create a derived class ElectricCar that inherits from Vehicle and overrides
StartEngine() to return "Electric engine started".
• Create a second derived class DieselCar that also inherits from Vehicle and
overrides the StartEngine() method by to return "Diesel engine started".
• In the Start() method, create objects of both ElectricCar and DieselCar, and call
the StartEngine() method on both. Demonstrate method overriding by calling the
method using base class and derived class references.
Sample Output:
Engine started
Electric engine started
Diesel engine started
Objective: Create an abstract class with a mix of abstract and virtual methods to demonstrate
the combination of the two.
Task:
Task:
• Create a base class Person with properties Name and Age. Provide a constructor that
accepts both parameters and initializes the properties.
• Create a derived class Employee that inherits from Person and adds a property
JobTitle. Provide a constructor in Employee that accepts Name, Age, and JobTitle,
and passes Name and Age to the base class constructor using base().
• Create a second derived class Manager that inherits from Employee and adds a
property Department (string). Provide a constructor in Manager that accepts Name,
Age, JobTitle, and Department, and passes Name, Age, and JobTitle to the
Employee constructor using base().
• In the Start() method, create instances of Employee and Manager, and use their
ToString() methods to print the details of these instances.
Sample Output:
Objective: Use an abstract class with a combination of virtual methods and data handling in
derived classes.
Task:
• Create an abstract class Employee with the properties Name (string) and Salary
(decimal). Provide an abstract method CalculateBonus() that will be implemented in
derived classes to calculate the bonus based on the salary. Also add a constructor that
initializes all properties to this class.
• Create a derived class FullTimeEmployee that overrides the CalculateBonus()
method to return a bonus equal to 10% of the salary.
• Create a second derived class PartTimeEmployee that overrides the
CalculateBonus() method to return a bonus equal to 5% of the salary.
• Add a virtual method DisplayEmployeeDetails() in the base class Employee, which
displays the Name and Salary. Override this method in both derived classes to also
display the calculated bonus amount.
• In the Start() method, create instances of FullTimeEmployee and
PartTimeEmployee, and display their details, including the calculated bonus.
Sample Output:
Full-time employee: John Doe
Salary: 50000.00
Bonus: 5000.00
Objective: Build a basic zoo system using abstract classes and inheritance.
Task:
• Create an abstract class Animal with properties Name and Species, and an abstract
method string MakeSound(). Override the ToString() method to return the Name
and Species.
• Add a constructor to the Animal that accepts a name and instantiates properties Name
and Species (This should be set to the class name as a default).
• Create two derived classes: Lion and Elephant. In Lion, override MakeSound() to
return "Roar", and in Elephant, override it to return "Trumpet".
• Create a Zoo class with a method DisplayAnimalDetails(Animal animal) that
accepts an Animal object and prints its Name and Species (hint: look at the
first bullet point).
• In the Start() method, create instances of Lion and Elephant, and call the
DisplayAnimalDetails- method to display the details.
Sample Output:
Objective: Extend the zoo system to include feeding functionality for animals.
Task:
• Extend the Animal class by adding a virtual method Feed() that returns "Feeding
{AnimalName}the {AnimalSpecies}".
• Override the Feed() method in both Lion and Elephant to customize the feeding
behavior. For example, "Feeding the lion meat" and "Feeding the elephant
vegetables".
• Create a ZooKeeper class with a method FeedAllAnimals(Animals[] animals) that
accepts a collection of Animal objects and calls the Feed() method for each one.
• In the Start() method, create instances of Lion and Elephant, add them to a
collection, and call FeedAllAnimals() to feed all animals.
Sample Output:
Objective: Extend the zoo system to handle animal sounds and feeding in a coordinated way.
Task:
Sample Output:
Roar (Lion)
Trumpet (Elephant)