Tuesday, February 16, 2010

C# Class Inheritance

This Console-based C# code calculates theArea, perimeter, surface area and volume of the following polygons:square, rechtangle, parellelogram, trapezoid, triangle, circle, cube,prism (both rechtangular and triangular base), cylinder, cone, sphere,pyramid and rhombus. All the user does is to input the polygon'sdimensions and the code will produce the polygon's attributes.

polygons.cs

author : Martin Raphael.K
// email : mrkraj@officetiger.com
// Date : November 02, 2001

// This is the sample code for inheritence

// new is the keyword which is used to override the base(Parent) class
//members base is the keyword whichs is equal to super in java
using System;
public class Base
{
public string str;
public Base()
{
Console.WriteLine("Base class Constructor");
}
~Base()
{
Console.WriteLine("Base class Destructor");
}
public void getData()
{
Console.WriteLine("Base class GetData");
}
}
public class Derived:Base
{
public new string str;

public Derived()
{
Console.WriteLine("Derived class Constructor");
}
~Derived()
{
Console.WriteLine("Derived class Destructor");
}
public new void getData()
{
base.getData();
Console.WriteLine("Derived class GetData");
}
public void getValues()
{
base.str=Console.ReadLine();
this.str=Console.ReadLine();
}
public void printValues()
{
Console.WriteLine("Base class Str "+base.str);
Console.WriteLine("Derived class Str "+this.str);
}
}

class InheritenceDemo
{
public static void Main(string[] a)
{
Derived d=new Derived();
d.getData();
d.getValues();
d.printValues();
Console.WriteLine("———–");
// this is used to get the Base class getData Method
((Base)d).getData();
}
}

// send a feedback to mrkraj@officetiger.com


Related Articles :

No comments:

Post a Comment