11.6. Lab & Review Questions
Follow the lab instructions as seen in, e.g., Chapter 10 to prepare this lab.
Name this lab Ch11OOPLab.
What is the relationship between “student1” and class Student in the following code?
1using System; 2public class Student { 3 4 private string studentName; 5 private int studentAge; 6 7 public string Name 8 { 9 get { return studentName; } 10 set { studentName = value; } 11 } 12 public int Age 13 { 14 get { return studentAge; } 15 set { studentAge = value; } 16 } 17} 18 19public class GFG { 20 static public void Main() 21 { 22 Student student1 = new Student(); 23 // Student student2 = new Student(); 24 // Student student3 = new Student(); 25 student1.name = "Doris"; 26 Console.WriteLine("student1.name: " + student1.name); 27 } 28}
In the preceding code, if you uncomment student2 and student3, will the code execute without error?
In the preceding code, if the content of the Main method is changed to the following, what would the output be?
Student obj = new Student(); obj.Name = "TY Chen"; obj.Age = 35; Console.WriteLine(" Name : " + obj.Name); Console.WriteLine(" Age : " + obj.Age);
Perform the following tasks:
Inherit from the following abstract class Shape to create two derived classes: Triangle and Rectangle.
Implement the method so that Triangle and Rectangle are able to calculate the areas respectively.
Prepare the three classes in a file called Shape.cs and run the code from the method in Program.cs.
Note that in the derived classes you may need to design fields in addition to implementing the method.
public abstract class Shape { public abstract double GetArea(); }