



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Learn about inheritance and polymorphism in object-oriented programming through the example of creating classes for Twix and Reese's candies. Discover how to save time and energy by reusing code and defining methods that do different things with different objects.
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!
Public Class Twix Private calories As Integer Private ingredients As ArrayList
Public Sub New() calories = 580 ingredients = New ArrayList ingredients.Add("Sugar") ingredients.Add("Chocolate") ingredients.Add("Caramel") End Sub
Public Function GetInfo() As String Dim sIngred As String Dim i As Integer
sIngred = CStr(ingredients(0)) For i = 1 To ingredients.Count - 1 sIngred = sIngred & " " & CStr(ingredients(i)) Next Return "Calories: " & CStr(calories) & _ " Ingredients: " & sIngred End Function End Class Public Class Reeses Private calories As Integer Private ingredients As ArrayList
Public Sub New() calories = 460 ingredients = New ArrayList ingredients.Add("Sugar") ingredients.Add("Chocolate") ingredients.Add("Peanut Butter") End Sub
Public Function GetInfo() As String Dim sIngred As String Dim i As Integer
sIngred = CStr(ingredients(0)) For i = 1 To ingredients.Count - 1 sIngred = sIngred & " " & CStr(ingredients(i)) Next Return "Calories: " & CStr(calories) & _ " Ingredients: " & sIngred End Function End Class
Dim twixbar As New Twix Dim reesescups As New Reeses Console.WriteLine(twixbar.GetInfo) Console.WriteLine(reesescups.GetInfo)
Calories: 580 Ingredients: Sugar Chocolate Caramel Calories: 460 Ingredients: Sugar Chocolate Peanut Butter
Public Class CandyBar Protected calories As Integer Protected ingredients As ArrayList
Public Sub New() ingredients = New ArrayList ingredients.Add("Sugar") ' All candy bars have sugar End Sub
Public Function GetInfo() As String Dim sIngred As String Dim i As Integer
sIngred = CStr(ingredients(0)) For i = 1 To ingredients.Count - 1 sIngred = sIngred & " " & CStr(ingredients(i)) Next Return "Calories: " & CStr(calories) & _ " Ingredients: " & sIngred End Function End Class
Public Class ChocolateBar Inherits CandyBar
Public Sub New() ingredients.Add("Chocolate") ' All chocolate bars have choc End Sub End Class
Public Class Twix Inherits ChocolateBar
Public Sub New() calories = 580 ingredients.Add("Caramel") End Sub End Class
Public Class Reeses Inherits ChocolateBar
Public Sub New() calories = 460 ingredients.Add("Peanut Butter") End Sub End Class
Dim twixbar As New Twix Dim reesescups As New Reeses Console.WriteLine(twixbar.GetInfo) Console.WriteLine(reesescups.GetInfo)
In CandyBar Class:
Overridable Function GetInfo() As String Dim sIngred As String Dim i As Integer
sIngred = CStr(ingredients(0)) For i = 1 To ingredients.Count - 1 sIngred = sIngred & " " & CStr(ingredients(i)) Next Return "Calories: " & CStr(calories) & _ " Ingredients: " & sIngred End Function
In Twix Class:
Overrides Function GetInfo() As String Return MyBase.GetInfo() & ". Two for me, none for you" End Function
Dim twixbar As New Twix Dim reesescups As New Reeses Console.WriteLine(twixbar.GetInfo) Console.WriteLine(reesescups.GetInfo)
Calories: 580 Ingredients: Sugar Chocolate Caramel Two for me, none for you Calories: 460 Ingredients: Sugar Chocolate Peanut Butter