Inheritance With the Base Class

Inheriting the Base Object

After creating a derived class, to let you access the parent class, the C# language provides a keyword named base that gives a child class access to public and protected members of its parent class.

Trapezoidal Prism

Practical LearningPractical Learning: Accessing the Base Object from a Child Class

  1. Start Microsoft Visual Studio
  2. On the main menu, click File -> New -> Project...
  3. In the middle list, click Empty Project (.NET Framework) and, in the Name text box, replace the name with Geometry05
  4. Click OK
  5. In the Solution Explorer, right-click Geometry05 -> Add -> Class...
  6. Change the name of the file to Trapezoid
  7. Click Add
  8. Fill the class as follows:
    namespace Geometry05
    {
        public class Trapezoid
        {
            public double TopBase { get; set; }
            public double BottomBase { get; set; }
            public double Height { get; set; }
    
            public double Area
            {
                get
                {
                    return Height * (TopBase + BottomBase) / 2.00;
                }
            }
        }
    }
  9. Click the Class View tab to activate it
  10. In the Class View, right-click Geometry05 -> Add -> Class...
  11. Type TrapezoidalPrism as the name of the class/file
  12. Click Add
  13. Fill the class as follows:
    namespace Geometry05
    {
        public class TrapezoidalPrism : Trapezoid
        {
            private double len;
    
            public double Length
            {
                get
                {
                    return len;
                }
    
                set
                {
                    len = value;
                }
            }
    
            public double BaseArea
            {
                get
                {
                    // "base" refers to a parent's property
                    return base.Area;
                }
            }
    
            public double TopArea
            {
                get
                {
                    // "base" TopBase refers to a parent's property
                    return base.TopBase * Length;
                }
            }
    
            public double BottomArea
            {
                get
                {
                    // "base" BottomBase refers to a parent's property
                    return base.BottomBase * Length;
                }
            }
    
            public double Volume
            {
                get
                {
                    // "base" Area refers to a parent's property
                    return base.Area * Length;
                }
            }
        }
    }
  14. In the Class View, right-click Geometry05 -> Add -> Class...
  15. In the middle list, make sure Class is selected.
    Change the name to Geometry
  16. Press Enter
  17. Change the code as follows:
    using static System.Console;
    
    namespace Geometry05
    {
        public class Geometry
        {
            private static void Main()
            {
                TrapezoidalPrism tp = new TrapezoidalPrism();
    
                WriteLine("Geometry - Trapezoidal Prism");
                WriteLine("----------------------------");
                WriteLine("Enter the following values");
                Write("Top Width:    ");
                double top = double.Parse(ReadLine());
                Write("Bottom Width: ");
                double bottom = double.Parse(ReadLine());
                Write("Length:       ");
                double length = double.Parse(ReadLine());
                Write("Height:       ");
                double height = double.Parse(ReadLine());
    
                tp.TopBase = top;
                tp.BottomBase = bottom;
                tp.Height = height;
                tp.Length = length;
    
                Clear();
    
                WriteLine("Geometry - Trapezoidal Prism");
                WriteLine("------------------------------");
                WriteLine("Top Base:    {0}", tp.TopBase);
                WriteLine("Bottom Base: {0}", tp.BottomBase);
                WriteLine("Height:      {0}", tp.Height);
                WriteLine("Length:      {0}", tp.Length);
                WriteLine("Base Area:   " + tp.BaseArea);
                WriteLine("Top Area:    " + tp.TopArea);
                Write("Bottom Area: {0}" + System.Environment.NewLine, tp.BottomArea);
                Write("Volume:      " + tp.Volume + System.Environment.NewLine);
                WriteLine("==============================");
            }
        }
    }
  18. To execute the project, on the main menu, click Debug -> Start Without Debugging:
    Geometry - Trapezoidal Prism
    ----------------------------
    Enter the following values
    Top Width:
  19. For the Top Width, type a number such as 137.96
  20. In the Bottom Base text box, enter a number such as 209.73
  21. In the Height text box, enter a number such as 87.59
  22. In the Length text box, enter a number such as 142.46
    Geometry - Trapezoidal Prism
    ----------------------------
    Enter the following values
    Top Width:    137.96
    Bottom Width: 209.73
    Length:       87.59
    Height:       142.46
  23. Press Enter:
    Geometry - Trapezoidal Prism
    ------------------------------
    Top Base:    137.96
    Bottom Base: 209.73
    Height:      142.46
    Length:      87.59
    Base Area:   24765.9587
    Top Area:    12083.9164
    Bottom Area: 18370.2507
    Volume:      2169250.322533
    ==============================
    Press any key to continue . . .
  24. Press Enter to close the window and return to your programming environment

Inheriting the Base Constructors

If a parent class has a constructor, you can call that constructor in the child class. The constructor is accessed using the base keyword preceded by a colon. The keyword is accessed as when calling a method but after the parentheses of a constructor in the child class. That is, it must use parentheses. The primary formula to follow is:

class class-name : parent-name
{
    access-level class-name(parameter(s)) : base(parameter(s))
}

To call the base() constructor in a constructor of a child class, there are a few rules you must follow:

To use the base() constructor, the constructor on it is applied in the child class and must use more than or the same number of parameters and same type(s) of parameter(s) as the constructor of the parent class.

To call a constructor of the parent class from the derived class, if you are calling a constructor that doesn't use any parameter, leave the parentheses empty. Here is an example:

public class Circle
{
    private double _radius;

    public Circle()
    {
        _radius = 0.00;
    }
}

public class Cone : Circle
{
    private double _height;

    public Cone() : base()
    {
        _height = 0.00;
    }
}

If you are calling a constructor that uses one parameter, in the parentheses of base(), type the name of the parameter. Here is an example:

using System;

public class Circle
{
    private double _radius;

    public Circle(double rad)
    {
        _radius = rad;
    }
}

public class Cone : Circle
{
    private double _height;

    public Cone(double rad, double hgt) : base(rad)
    {
        _height = hgt;
    }
}

If the constructor of the parent class uses more than one parameter, in the parentheses of base, enter the names of the parameters of the child contructor that is calling it. Here is an example:

public class Person
{
    public string FirstName { get; set; }
    public string LastName  { get; set; }

    public Person(string fName, string lName)
    {
        FirstName = fName;
        LastName  = lName;
    }
}

public class Employee : Person
{
    public Employee(string fName, string lName) : base(fName, lName)
    {
    }
}

To make your code easy to read, you can call the base() constructor on the next line. Here is an example:

public class Person
{
    public string FirstName { get; set; }
    public string LastName  { get; set; }

    public Person(string fName, string lName)
    {
        FirstName = fName;
        LastName  = lName;
    }
}

public class Employee : Person
{
    public Employee(string fName, string lName)
        : base(fName, lName)
    {
    }
}

If the child construtor uses more parameters than the parent constructor, you can initialize the extra parameter(s) in the body of the (child) constructor. Here is an example:

public class Person
{
    public string FirstName { get; set; }
    public string LastName  { get; set; }

    public Person(string fName, string lName)
    {
        FirstName = fName;
        LastName  = lName;
    }
}

public class Employee : Person
{
    public Employee(string fName, string lName, double salary)
        : base(fName, lName)
    {
        HourlySalary = salary;
    }

    public double HourlySalary { get; set; }
}

Practical LearningPractical Learning: Inheriting A Base Constructor

  1. Access the Trapezoid.cs file and add a constructor to the class as follows:
    using System;
    
    namespace Geometry05
    {
        public class Trapezoid
        {
            public double TopBase { get; set; }
            public double BottomBase { get; set; }
            public double Height { get; set; }
    
            public Trapezoid(double top, double bottom, double height)
            {
                TopBase = top;
                BottomBase = bottom;
                Height = height;
            }
    
            public double Area
            {
                get
                {
                    return Height * (TopBase + BottomBase) / 2.00;
                }
            }
        }
    }
  2. Access the TrapezoidalPrism.cs file and create a constructor in the class as follows:
    namespace Geometry05
    {
        public class TrapezoidalPrism: Trapezoid
        {
            private double len;
    
            public TrapezoidalPrism(double top, double bottom, double height, double length)
                : base(top, bottom, height)
            {
                Length = length;
            }
    
            . . . No Change
        }
    }
  3. Accecss the Geometry.cs file and change it as follows:
    using static System.Console;
    
    namespace Geometry05
    {
        public class Geometry
        {
            private static void Main()
            {
                WriteLine("Geometry - Trapezoidal Prism");
                WriteLine("----------------------------");
                WriteLine("Enter the following values");
                Write("Top Width:    ");
                double top = double.Parse(ReadLine());
                Write("Bottom Width: ");
                double bottom = double.Parse(ReadLine());
                Write("Length:       ");
                double length = double.Parse(ReadLine());
                Write("Height:       ");
                double height = double.Parse(ReadLine());
    
                TrapezoidalPrism tp = new TrapezoidalPrism(top: top, bottom: bottom, height: height, length: length);
    
                Clear();
    
                WriteLine("Geometry - Trapezoidal Prism");
                WriteLine("------------------------------");
                WriteLine("Top Base:    {0}", tp.TopBase);
                WriteLine("Bottom Base: {0}", tp.BottomBase);
                WriteLine("Height:      {0}", tp.Height);
                WriteLine("Length:      {0}", tp.Length);
                WriteLine("Base Area:   " + tp.BaseArea);
                WriteLine("Top Area:    " + tp.TopArea);
                Write("Bottom Area: {0}" + System.Environment.NewLine, tp.BottomArea);
                Write("Volume:      " + tp.Volume + System.Environment.NewLine);
                WriteLine("==============================");
            }
        }
    }
  4. To execute the application and test the webpage, press Ctrl + F5
  5. For the Top Width, type a number such as 226.93
  6. For the Bottom Base, enter a number such as 316.41
  7. For the Height, enter a number such as 109.86
  8. For the Length, enter a number such as 186.77
    Geometry - Trapezoidal Prism
    ----------------------------
    Enter the following values
    Top Width:    226.93
    Bottom Width: 316.41
    Length:       109.86
    Height:       186.77
  9. Press Enter:
    Geometry - Trapezoidal Prism
    ------------------------------
    Top Base:    226.93
    Bottom Base: 316.41
    Height:      186.77
    Length:      109.86
    Base Area:   50739.8059
    Top Area:    24930.5298
    Bottom Area: 34760.8026
    Volume:      5574275.076174
    ==============================
    Press any key to continue . . .
  10. Press Enter to close the window and return to your programming environment

Inheritance With this Class

Inheritance With this Object

We already know that every non-static class is equipped with an object named this. If you derive a class from a parent class, the child class has direct access to all public and protected members of the parent class. As an alternative to indicate that you are accessing a local member or a member of the parent class, in the child class, you can start the name of the member with .this.

Practical LearningPractical Learning: Inheriting this Parent

  1. Access the TrapezoidalPrism.cs file and change its class as follows:
    using System;
    
    namespace Geometry05
    {
        public class TrapezoidalPrism: Trapezoid
        {
            private double len;
    
            public TrapezoidalPrism(double top, double bottom, double height, double length)
                : base(top, bottom, height)
            {
                // "this" refers to a local property
                this.Length = length;
            }
    
            public double Length
            {
                get
                {
                    // "this" refers to a local field
                    return this.len;
                }
    
                set
                {
                    // "this" refers to a local field
                    this.len = value;
                }
            }
    
            public double BaseArea
            {
                get
                {
                    // "base" refers to a parent
                    return base.Area;
                }
            }
    
            public double TopArea
            {
                get
                {
                    // "this" TopBase refers to a parent's property
                    // "this" refers to a local property
                    return this.TopBase * this.Length;
                }
            }
    
            public double BottomArea
            {
                get
                {
                    // "this" BottomBase refers to a parent's property
                    // "this" refers to a local property
                    return this.BottomBase * this.Length;
                }
            }
    
            public double Volume
            {
                get
                {
                    // "base" Area refers to a parent's property
                    // "this" refers to a local property
                    return base.Area * this.Length;
                }
            }
        }
    }
  2. To execute the application and test the form, press Ctrl + F5
  3. For the Top Width, type a number such as 88.68
  4. For the Bottom Base, enter a number such as 124.83
  5. For the Height, enter a number such as 73.97
  6. For the Length, enter a number such as 214.86
    Geometry - Trapezoidal Prism
    ----------------------------
    Enter the following values
    Top Width:    88.68
    Bottom Width: 124.83
    Length:       73.97
    Height:       214.86
  7. Press Enter:
    Geometry - Trapezoidal Prism
    ------------------------------
    Top Base:    88.68
    Bottom Base: 124.83
    Height:      214.86
    Length:      73.97
    Base Area:   22937.3793
    Top Area:    6559.6596
    Bottom Area: 9233.6751
    Volume:      1696677.946821
    ==============================
    Press any key to continue . . .
  8. Press Enter to close the window and return to your programming environment

    Geometry - Tank

  9. On the main menu of Microsoft Visual Studio, click File -> New -> Project...
  10. In the middle list, click Empty Project (.NET Framework) and, in the Name text box, replace the name with Geometry06
  11. Click OK
  12. In the Solution Explorer, right-click Geometry06 -> Add -> Class...
  13. Set the name of the class as Cylinder
  14. Click Add
  15. Fill the class as follows:
    using static System.Math;
    
    namespace Geometry06
    {
        public class Cylinder
        {
            public double Length { get; set; }
            public double Radius { get; set; }
    
            public double Diameter      => this.Radius * 2.00;
            public double Circumference => this.Diameter * PI;
            public double CrossArea     => this.Radius * this.Radius * PI;
            public double LateralArea   => this.Circumference * this.Length;
            public double CentralVolume => this.CrossArea * this.Length;
        }
    }
  16. In the Solution Explorer, right-click Geometry06 -> Add -> Class...
  17. In the middle list, make sure Class is selected
    Change the name to Geometry
  18. Press Enter
  19. Change the code as follows:
    using static System.Console;
    
    namespace Geometry06
    {
        public class Geometry
        {
            private static void Main()
            {
                Cylinder body = new Cylinder();
    
                WriteLine("Geometric Shapes - Cylinder");
                WriteLine("----------------------------");
                WriteLine("Enter the following values");
                Write("Radius: ");
                body.Radius = double.Parse(ReadLine());
                Write("Length: ");
                body.Length = double.Parse(ReadLine());
    
                Clear();
    
                WriteLine("Geometry - Cylinder");
                WriteLine("---------------------------------");
                WriteLine("Radius:         {0}", body.Radius);
                WriteLine("Length:         {0}", body.Length);
                WriteLine("Diameter:       " + body.Diameter);
                WriteLine("Circumference:  " + body.Circumference);
                Write("Cross Area:     " + body.CrossArea + System.Environment.NewLine);
                WriteLine("Lateral Area:   " + body.LateralArea);
                Write("Central Volume: " + body.CentralVolume + System.Environment.NewLine);
                WriteLine("=================================");
            }
        }
    }
  20. To execute the project, on the main menu, click Debug -> Start Without Debugging:
    Geometric Shapes - Cylinder
    ----------------------------
    Enter the following values
    Radius:
  21. In the Radius text box, enter a number such as 79.84
  22. In the Length text box, type a number such as 258.93
    Geometric Shapes - Cylinder
    ----------------------------
    Enter the following values
    Radius: 79.84
    Length: 258.93
  23. Press Enter:
    Geometry - Cylinder
    ---------------------------------
    Radius:         79.84
    Length:         258.93
    Diameter:       159.68
    Circumference:  501.649514925218
    Cross Area:     20025.8486358147
    Lateral Area:   129892.108899587
    Central Volume: 5185292.9872715
    =================================
    Press any key to continue . . .
  24. Press Enter to close the window and return to your programming environment

    Geometry - Tank

  25. In the Solution Explorer, right-click Geometry06 -> Add -> Class...
  26. Change the name of the class as Tank
  27. Click Add
  28. Fill the class as follows:
    using static System.Math;
    
    namespace Geometry06
    {
        public class Tank : Cylinder
        {
            public double Width
            {
                get
                {
                    return this.Diameter;
                }
            }
    
            public double TotalLength
            {
                get
                {
                    return this.Length + this.Radius + this.Radius;
                }
            }
    
            public double TotalArea
            {
                get
                {
                    // Area on one side (half sphere) = Area of Sphere / 2
                    // Areas on both sides = area of a sphere = radius * radius * 4 * PI
                    // Total External Area = lateral area (of the central cylinder) + areas on both sides
                    return this.LateralArea + (this.Radius * this.Radius * PI * 4.00);
                }
            }
    
            public double TotalVolume
            {
                get
                {
                    // Volume on one side (half sphere) = Volue of Sphere / 2
                    // Volumes on both sides = volume of a sphere = radius * radius * radius * PI * 4 / 3
                    // Total Volume = central volume + volumes on both sides (which is the volume of a sphere)
                    return this.CentralVolume + (this.Radius * this.Radius * this.Radius * PI * 4.00 / 3.00);
                }
            }
        }
    }
  29. Access the Index.cshtml file and change its document as follows:
    using static System.Console;
    
    namespace Geometry06
    {
        public class Geometry
        {
            private static void Main()
            {
                Tank whole = new Tank();
    
                WriteLine("Geometric Volume - Tank");
                WriteLine("----------------------------");
                WriteLine("Enter the following values");
                Write("Radius: ");
                whole.Radius = double.Parse(ReadLine());
                Write("Length: ");
                whole.Length = double.Parse(ReadLine());
    
                Clear();
    
                WriteLine("Geometric Volume - Tank");
                WriteLine("---------------------------------");
                WriteLine("Radius:          {0}", whole.Radius);
                WriteLine("Cylinder Length: {0}", whole.Length);
                WriteLine("---------------------------------");
                WriteLine("Each Side (Half Sphere)");
                WriteLine("Diameter:       " + whole.Diameter);
                WriteLine("Circumference:  " + whole.Circumference);
                Write("Cross Area:     " + whole.CrossArea + System.Environment.NewLine);
                WriteLine("---------------------------------");
                WriteLine("The Central Cylinder");
                WriteLine("Lateral Area:   " + whole.LateralArea);
                Write    ("Central Volume: " + whole.CentralVolume + System.Environment.NewLine);
                WriteLine("---------------------------------");
                WriteLine("The Tank as a Whole");
                WriteLine("Through Width:  " + whole.Width);
                WriteLine("Total Length:   " + whole.TotalLength);
                WriteLine("Total Area:     " + whole.TotalArea);
                WriteLine("Total Volume:   " + whole.TotalVolume);
                WriteLine("=================================");
            }
        }
    }
  30. To execute the project, on the main menu, click Debug -> Start Without Debugging:
    Geometric Volume - Tank
    ----------------------------
    Enter the following values
    Radius:
  31. For the Radius, type 79.84
  32. For the Length, type 258.93
    Geometric Volume - Tank
    ----------------------------
    Enter the following values
    Radius: 79.84
    Length: 258.93
  33. Press Enter:
    Geometric Volume - Tank
    ---------------------------------
    Radius:          79.84
    Cylinder Length: 258.93
    ---------------------------------
    Each Side (Half Sphere)
    Diameter:       159.68
    Circumference:  501.649514925218
    Cross Area:     20025.8486358147
    ---------------------------------
    The Central Cylinder
    Lateral Area:   129892.108899587
    Central Volume: 5185292.9872715
    ---------------------------------
    The Tank as a Whole
    Through Width:  159.68
    Total Length:   418.61
    Total Area:     209995.503442846
    Total Volume:   7317111.32738276
    =================================
    Press any key to continue . . .
  34. Press Enter to close the window and return to your programming environment

Returning this Object

You may remember that a method of a class can return this object that represents the class itself. Here is an example:

public class Triangle
{
    public void Examine()
    {
        Triangle inside = this;
    }
}

Geometry - Cone

Practical LearningPractical Learning: Returning this Object

  1. Save the following picture somewhere on your computer and return to your programming environment:
  2. On the main menu, click File -> New -> Project...
  3. In the middle list, click Empty Project (.NET Framework) and, in the Name text box, replace the name with Geometry07
  4. Click OK
  5. In the Solution Explorer, right-click Geometry -> Add -> Class...
  6. Set the name of the class as Circle
  7. Click Add
  8. Change the document as follows:
    namespace Geometry07
    {
        public class Circle
        {
            public double Radius { get; set; }
    
            public Circle(double radius)
            {
                Radius = radius;
            }
    
            public double Diameter => Radius * 2.00;
            public double Circumference => Diameter * Math.PI;
            public double Area => Radius * Radius * Math.PI;
    
            public Circle Encircle()
        	 {
                return this;
           }
        }
    }
  9. In the Solution Explorer, right-click Geometry07 -> Add -> Class...
  10. Change the name of the class to Cone
  11. Click Add
  12. Change the document as follows:
    using static System.Math;
    
    namespace Geometry07
    {
        public class Cone : Circle
        {
            public double Height { get; set; }
    
            public Cone(double radius, double height) : base(radius)
            {
                Height = height;
            }
    
            public double BaseArea => Area;
            public double Volume   => Radius * Radius * Height * PI / 3.00;
        }
    }

Getting this Parent

A method of an inherited class can return this. You must then identify the role of this object as it is used in a derived class.

Practical LearningPractical Learning: Getting this Parent

  1. In the Cone class, create two methods as follows:
    using static System.Math;
    
    namespace Geometry07
    {
        public class Cone : Circle
        {
            public double Height { get; set; }
    
            public Cone(double radius, double height) : base(radius)
            {
                Height = height;
            }
    
            public double BaseArea => Area;
            public double Volume   => Radius * Radius * Height * Math.PI / 3.00;
    
            public Circle Create()
            {
                return this;
            }
    
            public Cone Surround()
            {
                return this;
            }
        }
    }
  2. In the Solution Explorer, right-click Geometry07 -> Add -> Class...
  3. In the middle list, make sure Class is selected.
    Change the name to Geometry
  4. Press Enter
  5. Change the code as follows:
    using static System.Console;
    
    namespace Geometry07
    {
        public class Geometry
        {
            private static void Main()
            {
                Cone dongle = new Cone(0.00, 0.00);
    
                WriteLine("Geometric Volume - Cone");
                WriteLine("----------------------------");
                WriteLine("Enter the following values");
                Write("Radius: ");
                double radius = double.Parse(ReadLine());
                Write("Height: ");
                double height = double.Parse(ReadLine());
    
                Circle round = new Circle(radius);
                dongle = new Cone(radius, height);
    
                Circle c = dongle.Create();
    
                Clear();
    
                WriteLine("Geometric Volume - Cone");
                WriteLine("---------------------------------");
                WriteLine("Base Radius:   {0}", radius);
                WriteLine("Height:        {0}", height);
                WriteLine("---------------------------------");
                WriteLine("Base");
                WriteLine("Diameter:      " + c.Diameter);
                WriteLine("Circumference: " + c.Circumference);
                Write("Base Area:     " + c.Area + System.Environment.NewLine);
                WriteLine("---------------------------------");
                WriteLine("Cone");
                WriteLine("Volume:        " + dongle.Volume);
                WriteLine("=================================");
            }
        }
    }
  6. To execute the application, press Ctrl + F5:
    Geometric Volume - Cone
    ----------------------------
    Enter the following values
    Radius:
  7. For the Radius, type a number such as 84.79
  8. For the Height, type a number such as 375.67
    Geometric Volume - Cone
    ----------------------------
    Enter the following values
    Radius: 84.79
    Height: 375.67
  9. Press Enter:
    Geometric Volume - Cone
    ---------------------------------
    Base Radius:   84.79
    Height:        375.67
    ---------------------------------
    Base
    Diameter:      169.58
    Circumference: 532.751282195757
    Base Area:     22585.9906086891
    ---------------------------------
    Cone
    Volume:        2828293.03065542
    =================================
    Press any key to continue . . .
  10. Press Enter to close the window and return to your programming environment
  11. Change the Geometry.cs document as follows:
    using static System.Console;
    
    namespace Geometry07
    {
        public class Geometry
        {
            private static void Main()
            {
                Cone fancy = new Cone(0.00, 0.00);
    
                WriteLine("Geometric Volume - Cone");
                WriteLine("----------------------------");
                WriteLine("Enter the following values");
                Write("Radius: ");
                double radius = double.Parse(ReadLine());
                Write("Height: ");
                double height = double.Parse(ReadLine());
    
                Cone dongle = new Cone(radius, height);
                fancy = dongle.Surround();
    
                Clear();
    
                WriteLine("Geometric Volume - Cone");
                WriteLine("---------------------------------");
                WriteLine("Base Radius:   {0}", radius);
                WriteLine("Height:        {0}", height);
                WriteLine("---------------------------------");
                WriteLine("Base");
                WriteLine("Diameter:      " + fancy.Diameter);
                WriteLine("Circumference: " + fancy.Circumference);
                Write("Base Area:     " + fancy.Area + System.Environment.NewLine);
                WriteLine("---------------------------------");
                WriteLine("Cone");
                WriteLine("Volume:        " + fancy.Volume);
                WriteLine("=================================");
            }
        }
    }
  12. To execute the application, on the main menu, click Debug -> Start Without Debugging
  13. For Radius, type a number such as 84.79
  14. For the Height, type a number such as 375.67
  15. Press Enter:
    Geometric Volume - Cone
    ---------------------------------
    Base Radius:   84.79
    Height:        375.67
    ---------------------------------
    Base
    Diameter:      169.58
    Circumference: 532.751282195757
    Base Area:     22585.9906086891
    ---------------------------------
    Cone
    Volume:        2828293.03065542
    =================================
    Press any key to continue . . .
  16. Press Enter to close the window and return to your programming environment

Comparing an Object to this Parent

Remember that you can compare an object to this to find out what that object represents. This operation can be used to find out if an object refers to a parent, grand-parent, etc, of the object.

The new Modifier

Introduction

Imagine you create a class, such as one for a geometric shape such as a trapezoid. As we saw above, you can use such a class as the base class for a prism. Both the trapezoid and its related prism have an area but their areas are different.

If you create or declare a new member in a derived class and that member has the same name as a member of the base class, when creating the new member, you may want to indicate to the compiler that you want to create a brand new and independent version of that method. When doing this, you would be asking the compiler to hide the member of the base class that has the same name, when the member of the current class is invoked.

Creating a New Version of a Member

To create a new version of a member, type the new keyword to its left.

ApplicationPractical Learning: Creating a New Version of a Member

  1. Open the Geometry05 project created earlier
  2. Access the TrapezoidalPrism file and add a new method in the class as follows:
    namespace Geometry05
    {
        public class TrapezoidalPrism : Trapezoid
        {
            private double len;
    
            public TrapezoidalPrism(double top, double bottom, double height, double length)
                : base(top, bottom, height)
            {
                Length = length;
            }
    
            public double Length
            {
                get
                {
                    // "this" refers to a local field
                    return this.len;
                }
    
                set
                {
                    // "this" refers to a local field
                    this.len = value;
                }
            }
    
            public double BaseArea
            {
                get
                {
                    // "base" refers to the parent
                    return base.Area;
                }
            }
    
            public double TopArea
            {
                get
                {
                    // "base" refers to the parent
                    // "this" Length refers to a local property
                    return this.TopBase * this.Length;
                }
            }
    
            public double BottomArea
            {
                get
                {
                    // "base" refers to the parent
                    // "this" Length refers to a local property
                    return this.BottomBase * this.Length;
                }
            }
    
            public new double Area
            {
                get
                {
                    return BaseArea + TopArea + BottomArea + BaseArea;
                }
            }
    
            public double Volume
            {
                get
                {
                    // "base" refers to the parent
                    // "this" Length refers to a local property
                    return base.Area * this.Length;
                }
            }
        }
    }
  3. Access the Geometry.cs file and change its document as follows:
    using static System.Console;
    
    namespace Geometry05
    {
        public class Geometry
        {
            private static void Main()
            {
                double top = 0.00;
                double bottom = 0.00;
                double height = 0.00;
                double length = 0.00;
    
                TrapezoidalPrism tp = new TrapezoidalPrism(0, 0, 0, 0);
                
                WriteLine("Geometry - Trapezoidal Prism");
                WriteLine("----------------------------");
                WriteLine("Enter the following values");
                Write("Top Base:    ");
                top = double.Parse(ReadLine());
                Write("Bottom Base: ");
                bottom = double.Parse(ReadLine());
                Write("Length:      ");
                length = double.Parse(ReadLine());
                Write("Height:      ");
                height = double.Parse(ReadLine());
    
                tp = new TrapezoidalPrism(top: top, bottom: bottom, height: height, length: length);
    
                Clear();
    
                WriteLine("Geometry - Trapezoidal Prism");
                WriteLine("------------------------------");
                WriteLine("Top Base:    {0}", tp.TopBase);
                WriteLine("Bottom Base: {0}", tp.BottomBase);
                WriteLine("Height:      {0}", tp.Height);
                WriteLine("Length:      {0}", tp.Length);
                WriteLine("------------------------------");
                WriteLine("Base Area:   " + tp.BaseArea);
                WriteLine("Top Area:    " + tp.TopArea);
                Write("Bottom Area: {0}" + System.Environment.NewLine, tp.BottomArea);
                WriteLine("Total Area:  " + tp.Area);
                Write("Volume:      " + tp.Volume + System.Environment.NewLine);
                WriteLine("==============================");
            }
        }
    }
  4. To execute the application, on the menu, click Debug -> Start Without Debugging:
    eometry - Trapezoidal Prism
    ---------------------------
    nter the following values
    op Base:
  5. For the Top Base value, type a number such as 137.96
  6. For the Bottom Base, type a number such as 209.73
  7. For the Height, type a number such as 87.59
  8. For the Length, type a number such as 142.46
    Geometry - Trapezoidal Prism
    ----------------------------
    Enter the following values
    Top Base:    137.96
    Bottom Base: 209.73
    Length:       87.59
    Height:       142.46
  9. Press Enter:
    Geometry - Trapezoidal Prism
    ------------------------------
    Top Base:    137.96
    Bottom Base: 209.73
    Height:      142.46
    Length:      87.59
    ------------------------------
    Base Area:   24765.9587
    Top Area:    12083.9164
    Bottom Area: 18370.2507
    Total Area:    79986.0845
    Volume:      2169250.322533
    ==============================
    Press any key to continue . . .
  10. Press Enter to close the window and return to your programming environment
  11. Close your programming environment

Previous Copyright © 2001-2019, FunctionX Next