Wednesday, October 8, 2014

Intro_To_Inheritance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Introduction_To_Inheritance
{
    public class Circle
    {
        private double _radius;

        public Circle(double radius)
        {
            _radius = radius;
        }
        //default constructor
        public Circle()
        {
            _radius = 1;
        }

        public double Radius { get { return _radius; } }

        public double Area()
        {
            return Math.PI * _radius * _radius;
        }

        public double Perimeter()
        {
            return 2 * Math.PI * _radius;
        }
    }
}
//=============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Introduction_To_Inheritance
{
    public class Sphere : Circle
    {
        ///class sphere is inheriting from class Circle
        ///it means that the Sphere inherits all the fields,properties and methods of the Circle.
        ///However, the Sphere will not have a direct access to the private fields defined in Circle, 
        ///but has access to any thing that is defined as public or protected
        ///
        ///Sphere will not inherit the constructor
        ///In Inheritance, the Circle is considered the parent class or the base class
        ///the Sphere is considered the child class or the derived class
        ///
        ///Inheritance creates a parent-child relationship.
        ///
        ///Access modifiers: public,private, protected, internal, protected internal
        ///                  
        ///public members: are accessible to every class
        ///private members: are accessible to only the class where they are defined       
        ///protected members: are accessible by the class itself and all its children (all
        ///child classes)                                 
        ///internal members: are accessible by any class within the same assembly (or namespace)   
        ///------------------------------------------
        ///since Sphere inherits from Circle, it has a radius
        ///even though it does not have access to the _radius
        ///defined in the Circle.
        ///When you define a constructor in the Sphere class
        ///you need to define the parameter radius as well
        ///

        //child constructor
        //The child constructor will always call the parent
        //default constructor or another constructor if specified
        public Sphere(double radius):base(radius)
        {
            //how are going to initialize _radius, when the 
            //Sphere has no way to directly access _radius as defined.
           
            ///The sphere constructor should be set up to call
            ///the parent (Circle) constructor, pass the radius
            ///value to it to allow it to initialize the _radius field. 
        }
        //The sphere does inherit the Area
        ///method defined in the Circle, however
        ///The computation in the circle area
        ///does not fit the correct formula for the sphere
        ///The sphere needs to redefine the 
        ///Area metod that it inherited from Circle.
        ///This redefinition is called method hiding.
        ///use the keyword new to tell the compiler
        ///that you are using method hiding

        new public double Area()
        {
            return 4 * Math.PI * Radius * Radius;
        }
    }
}
//===========================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Introduction_To_Inheritance
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Circle c1 = new Circle(5.6);

            Sphere sp1 = new Sphere(9.3);
          
        }
        Random rand = new Random();
        private void btnCreateCircleSphere_Click(object sender, EventArgs e)
        {
            double radius = rand.Next(10, 100);

            //create a Circle object
            Circle c = new Circle(radius);
            //Compute perimeter and area
            double circle_peri = c.Perimeter();
            double circle_area = c.Area();

            //create a Sphere object (using same radius)
            Sphere sp = new Sphere(radius);
            double sphere_peri = sp.Perimeter();
            double sphere_area = sp.Area();

            //display both
            richTextBox1.Text = String.Format(
            "{0}  Radius: {1}  peri: {2:f}   area: {3:f}\n\n",
                    c.GetType().Name,radius, circle_peri, circle_area);

            richTextBox1.AppendText(String.Format(
                "{0}  Radius: {1}  peri: {2:f}   area: {3:f}",
                sp.GetType().Name,radius, sphere_peri, sphere_area));
        }
    }
}

///Lab Assignment
///New Project:
///Define a class Cylinder: with _radius and _height
///constructor, properties and methods:Volume and Area
///
///Define a class Cone that inherits from Cylinder
///Add constructor
///Add any necessary method hiding
///
///In Form1 create a Cylinder object and a Cone object
///Display their data: type,radius,height,area,volume.

No comments:

Post a Comment