...

/

Solution Review: Implement the Derived Class

Solution Review: Implement the Derived Class

This review provides a detailed analysis to solve the 'Implement the Derived Class' challenge.

We'll cover the following...

Solution

Press + to interact
// Base Class
class Product {
// Private Data Members
private string name;
private double price;
// Getter method for name
public string GetName(int liters) {
if (liters == 1) {
this.name = "Cola";
return this.name;
}
else if (liters == 2) {
this.name = "Fanta";
return this.name;
}
else if (liters == 3) {
this.name = "Dew";
return this.name;
}
else return "";
}
// Getter method for price
public double GetPrice(int liters) {
if (liters == 1) {
this.price = 2;
return this.price;
}
else if (liters == 2) {
this.price = 3.5;
return this.price;
}
else if (liters == 3) {
this.price = 4;
return this.price;
}
else return 0;
}
}
// Derived Class
class Beverage : Product {
public int Liters { get; set; } // Liters of a Beverage
public string GetDetails() {
string details = GetName(this.Liters) + ", " + GetPrice(this.Liters) + ", " + Liters;
return details;
}
}
class Demo {
public static void Main(string[] args) {
Beverage berverage = new Beverage();
berverage.Liters = 2;
Console.WriteLine(berverage.GetDetails());
}
}

Explanation

  • Line 47:
...