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 Classclass Product {// Private Data Membersprivate string name;private double price;// Getter method for namepublic 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 pricepublic 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 Classclass Beverage : Product {public int Liters { get; set; } // Liters of a Beveragepublic 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: