Questions
-
What is the keyword used to call the constructor from the base class?
-
this. - the name of the base class.
-
base -
over -
inherits
-
-
Inheritance is …
- … a way of implementing a class by re-using another class’ code.
- … impossible to represent in a UML diagram.
- … achieved using the
;(semicolon) symbol. - … a way of connecting a basic class with a derived class.
Comment
The class inherited from is called the base, superclass or parent class, but not basic.
-
Suppose your are given an
ElectricDeviceclass and aWallDecorclass. You would like to write aClockclass that represents at the same time an electric device and a wall decor. This is possible only if theElectricDeviceandWallDecorclasses are…- Sealed
- Interfaces
- Protected
- Constructors
-
An abstract class…
- … can be instantiated.
- … can contain a constructor.
- … can contain abstract and non-abstract methods.
- … is represented in a UML diagram by prefixing its name with <<Abstract>>.
-
In a UML diagram, an abstract method is …
- … underlined.
- … impossible to distinguish from a non-abstract method.
- … necessarily part of an abstract class.
- … in italics.
-
A class
Studentinheriting from an abstract classPerson…- … must override all of
Person’s methods. - … must implement all the methods marked
abstractinStudent. - … can be instantiated.
- … is connected to
Personusing an open triangle end and a continuous line (⇽) on the UML diagram. - … is connected to
Personusing an open triangle end and a dashed line (◁┈) on the UML diagram.
- … must override all of
-
A method with header
public virtual void Test(int a, out int b)will…- Return a value
- Set the value of
b - Require two arguments
- Be overrideable
-
A method with header
public abstract string Test()will…- Have an empty body
- Need to be inside an
abstractclass - Not be overriden
-
Give at least two differences between an abstract class and an interface.
Solution
A possible list includes:
- An abstract class can contain
privatemethods. - An abstract class is inherited by another class, while an interface is realized by another class.
- An class can inherit from only one abstract class, but it can realize multiple interfaces.
- Interfaces do not need to explicitly mark their methods and properties as public or abstract, since they are always public and abstract.
- Abstract methods in an abstract class need to be explicitly overridden by methods in the class inheriting from them, while classes realizing interfaces need not to mark the methods as explicitely overriding methods from the interface.
- An abstract class can contain
Problems
-
Assume given the following class implementation:
class Computer { private string brand; public void SetBrand(string brandP) { brand = brandP; } public Computer(string bP) { SetBrand(bP); } public override string ToString() { return $"Brand: {brand}"; } }Write a
Laptopclass that-
Inherits from
Computer, -
Has one single (automatic) property, called
ScreenSize, of typeint, -
Has a constructor that takes 2 parameters of appropriate datatypes, and use them to set
brandandScreenSize, -
Has a
ToStringmethod that returns astringcontaining the brand and screen size.Solution
class Laptop : Computer { public int ScreenSize { get; set; } public Laptop(int ssP, string bP) : base(bP) { ScreenSize = ssP; } public override string ToString() { return base.ToString() + $"\nScreen size: {ScreenSize}\""; } }
-
-
Consider the diagram representing the “Room”, “ClassRoom”, “Office” classes and their relations.
Suppose you are given an implementation of the
Roomclass, such thatRoom test = new Room("UH", 243); Console.WriteLine(test);displays
UH 243-
Write an implementation of the
ClassRoomclass. YourToStringmethod should display the room’s building and number, in addition to whether it has AV set-up.Solution
class ClassRoom : Room { private bool av; public ClassRoom(string bP, int nP, bool aP) : base(bP, nP) { av = aP; } public override string ToString() { return base.ToString() + "av? " + av; } } -
Write a
SameBuildingstatic method to be placed inside theRoomclass such thatOffice test1 = new Office("UH", 127, "706 737 1566"); ClassRoom test2 = new ClassRoom("UH", 243, true); Office test3 = new Office("AH", 122, "706 729 2416"); Console.WriteLine(Room.SameBuilding(test1, test2)); Console.WriteLine(Room.SameBuilding(test2, test3));Would display “true” and “false”.
Solution
public static bool SameBuilding(Room a, Room b) { return a.building == b.building; }
-
-
Consider the diagram representing the “Room”, “BedRoom”, “BathRoom” classes and their relations.
-
Write an implementation of the
SurfaceAreaproperty for theRoomclass, assuming you are given an implementation of theWidthandLengthproperties. -
Check the statements that would compile, assuming that
rTestis aRoomobject,beTestis aBedRoomobject, andbaTestis aBathRoomobject.-
rTest.Capacity = 12; -
baTest.Width = 12; -
beTest.capacity = 3; -
rTest.SurfaceArea = -2; -
baTest.Capacity = 3; -
beTest.Shower = true; -
Console.WriteLine(baTest.ToString());
-
-
Write a complete implementation of the
BedRoomclass.- Your
Capacityproperty should use thecapacityattribute, and throw an exception if the argument given is strictly less than 1. - Your
ToStringmethod should complement theRoom’sToStringby appending to itsstringthe capacity (in person) of theBedRoomobject.
Solution
using System; class BedRoom : Room { private int capacity; public int Capacity { set { if (value < 0) { throw new ArgumentException( "A capacity must be positive or 0." ); } else { capacity = value; } } } public BedRoom(double wP, double lP, int cP) : base(wP, lP) { Capacity = cP; } public override string ToString() { return base.ToString() + "Capacity: " + capacity; } } - Your
-
Write the
ToStringmethod of theBathRoomclass, knowing that a disclaimer should be part of thestringreturned if theBathRoomhas a shower or a bathtub but no hot water.Solution
public override string ToString() { string equipement = ""; if (Shower) { equipement += "a shower"; } if (Shower && Bathtub) { equipement += " and "; } if (Bathtub) { equipement += "a bathtub"; } if (!Shower && !Bathtub) equipement += "no shower nor bathtub"; equipement += "."; if (!hotWater && (Shower || Bathtub)) { equipement += " However, it does not have hot water"; } return base.ToString() + "\nThis bathtub has " + equipement; }
-
-
Consider the diagram representing the “Article”, “Book” classes and their relations.
-
Write a (partial) implementation of the
Articleabstract class:- Write an implementation for the
priceattribute: you can either use a getter and a setter (as pictured in the UML diagram), or a property. However, in both cases, setting the price to a negative value should result in anArgumentOutOfRangeExceptionexception being thrown. - Write an abstract
ShippingCosts()method.
Solution
using System; abstract class Article { public string Id { get; set; } private decimal price; public void SetPrice(decimal priceP) { if (priceP < 0) { throw new ArgumentOutOfRangeException(); } else { price = priceP; } } public decimal GetPrice() { return price; } public Article(string idP, decimal priceP) { Id = idP; SetPrice(priceP); } public abstract decimal ShippingCosts(); } - Write an implementation for the
-
Now, assume given a complete implementation of the
Articleabstract class. Write a complete implementation of theBookclass (header included), containing:- An implementation of the
Titleproperty using auto-properties. - A
Bookconstructor that passes theidPandpriceParguments to theArticleconstructor. ThetitlePargument should be assigned to theTitleproperty. - A
ShippingCosts()method that returns either 5.0, or 10% of the Book’s price, whichever is smallest.
Solution
class Book : Article { public string Title { get; set; } public Book(string idP, decimal priceP, string titleP) : base(idP, priceP) { Title = titleP; } public override decimal ShippingCosts() { decimal tenp = .1M * GetPrice(); if (tenp > 5M) { tenp = 5M; } return tenp; } } - An implementation of the
-
Write statements that, if placed in a
Mainmethod, would- Create a
BookwithId“AAA001”,price$12.5, titled “What it’s like to be a bird”. - Display (nicely) its shipping costs.
- Display its
Id(as retrieved from the object).
Solution
using System; class Program { public static void Main() { Book test = new Book( "AAA001", 12.5M, "What it's like to be a bird." ); Console.WriteLine($"{test.ShippingCosts():C}"); Console.WriteLine(test.Id); } } - Create a
-