Polymorphism in Object-Oriented Programming, Summaries of Mathematics

A lab exercise on polymorphism in object-oriented programming. It covers three main tasks: creating a mathoperations class with overloaded add() methods, implementing an animal class hierarchy with virtual and overridden explainself() methods, and designing a shape class hierarchy with abstract and virtual methods for calculating perimeter, area, and drawing. Example usage and solutions for each task, demonstrating the principles of polymorphism, method overloading, and method overriding in c#. This lab exercise is likely intended for students in a computer science or software engineering course, covering fundamental object-oriented programming concepts and their practical application.

Typology: Summaries

2021/2022

Uploaded on 10/04/2022

hoang-nguyen-nhat
hoang-nguyen-nhat 🇨🇳

11 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page 1 of 3
Lab: Polymorphism
1. MathOperations
Create a class MathOperations, which should have 3 times method Add(). Method Add() have to be invoked
with:
Add(int, int): int
Add(double, double, double): double
Add(decimal, decimal, decimal): decimal
You should be able to use the class like this:
StartUp.cs
public static void Main()
{
MathOperations mo = new MathOperations();
Console.WriteLine(mo.Add(2, 3));
Console.WriteLine(mo.Add(2.2, 3.3, 5.5));
Console.WriteLine(mo.Add(2.2m, 3.3m, 4.4m));
}
Examples
Input Output
5
11
9.9
Solution
The MathOperation class should look like this:
2. Animals
Create a class Animal, which hold two properties:
Name: string
FavouriteFood: string
pf3

Partial preview of the text

Download Polymorphism in Object-Oriented Programming and more Summaries Mathematics in PDF only on Docsity!

Lab: Polymorphism

1. MathOperations

Create a class MathOperations , which should have 3 times method Add(). Method Add() have to be invoked with:  Add(int, int): int  Add(double, double, double): double  Add(decimal, decimal, decimal): decimal You should be able to use the class like this: StartUp.cs public static void Main() { MathOperations mo = new MathOperations(); Console.WriteLine(mo.Add(2, 3)); Console.WriteLine(mo.Add(2.2, 3.3, 5.5)); Console.WriteLine(mo.Add(2.2m, 3.3m, 4.4m)); }

Examples

Input Output 5 11

Solution

The MathOperation class should look like this:

2. Animals

Create a class Animal , which hold two properties :  Name : string  FavouriteFood : string

Animal have one virtual method ExplainSelf(): string You should add two new classes Cat and Dog. There override ExplainSelf() method by adding concrete animal sound on new line. (Look at examples below) You should be able to use the class like this: StartUp.cs Animal cat = new Cat("Pesho", "Whiskas"); Animal dog = new Dog("Gosho", "Meat"); Console.WriteLine(cat.ExplainSelf()); Console.WriteLine(dog.ExplainSelf());

Examples

Input Output I am Pesho and my favourite food is Whiskas MEEOW I am Gosho and my fovourite food is Meat DJAAF

Solution

3. Shapes Create class hierarchy, starting with abstract class Shape:  Abstract methods: