Use virtual methods and polymorphism : Override Virtual « Class Interface « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » Override VirtualScreenshots 
Use virtual methods and polymorphism
Use virtual methods and polymorphism

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Use virtual methods and polymorphism. 
 
using System; 
 
class TwoDShape {  
  double pri_width;  // private 
  double pri_height; // private 
  string pri_name;   // private 
  
  // A default constructor.  
  public TwoDShape() {  
    width = height = 0.0;  
    name = "null";  
  }  
  
  // Parameterized constructor.  
  public TwoDShape(double w, double h, string n) {  
    width = w;  
    height = h;  
    name = n;  
  }  
  
  // Construct object with equal width and height.  
  public TwoDShape(double x, string n) {  
    width = height = x;  
    name = n;  
  }  
  
  // Construct an object from an object.  
  public TwoDShape(TwoDShape ob) {  
    width = ob.width;  
    height = ob.height;  
    name = ob.name;  
  }  
  
  // Properties for width, height, and name 
  public double width 
    get return pri_width; 
    set pri_width = value; 
  
 
  public double height 
    get return pri_height; 
    set pri_height = value; 
  
 
  public string name 
    get return pri_name; 
    set pri_name = value; 
  
  
  public void showDim() {  
    Console.WriteLine("Width and height are " +  
                       width + " and " + height);  
  }  
  
  public virtual double area() {   
    Console.WriteLine("area() must be overridden");  
    return 0.0;  
  }   
}  
  
// A derived class of TwoDShape for triangles. 
class Triangle : TwoDShape {  
  string style; // private 
    
  // A default constructor.  
  public Triangle() {  
    style = "null";  
  }  
  
  // Constructor for Triangle.  
  public Triangle(string s, double w, double h
    base(w, h, "triangle") {  
      style = s;   
  }  
  
  // Construct an isosceles triangle.  
  public Triangle(double x: base(x, "triangle") {  
    style = "isosceles";   
  }  
  
  // Construct an object from an object.  
  public Triangle(Triangle ob: base(ob) {  
    style = ob.style;  
  }  
  
  // Override area() for Triangle. 
  public override double area() {  
    return width * height / 2;  
  }  
  
  // Display a triangle's style. 
  public void showStyle() {  
    Console.WriteLine("Triangle is " + style);  
  }  
}  
  
// A derived class of TwoDShape for rectangles.   
class Rectangle : TwoDShape {   
  // Constructor for Rectangle.  
  public Rectangle(double w, double h:  
    base(w, h, "rectangle"){ }  
  
  // Construct a square.  
  public Rectangle(double x:  
    base(x, "rectangle") { }  
  
  // Construct an object from an object.  
  public Rectangle(Rectangle ob: base(ob) { }  
  
  // Return true if the rectangle is square. 
  public bool isSquare() {   
    if(width == heightreturn true;   
    return false;   
  }   
     
  // Override area() for Rectangle. 
  public override double area() {   
    return width * height;   
  }   
}  
  
public class DynShapes {  
  public static void Main() {  
    TwoDShape[] shapes = new TwoDShape[5];  
  
    shapes[0new Triangle("right"8.012.0);  
    shapes[1new Rectangle(10);  
    shapes[2new Rectangle(104);  
    shapes[3new Triangle(7.0);  
    shapes[4new TwoDShape(1020"generic")
  
    for(int i=0; i < shapes.Length; i++) {  
      Console.WriteLine("object is " + shapes[i].name);  
      Console.WriteLine("Area is " + shapes[i].area());  
  
      Console.WriteLine();    
    }  
  }  
}


           
       
Related examples in the same category
1.Polymorphism
2.Virtual keyword can be used to start a new inheritance ladder
3.Virtual and overloadVirtual and overload
4.Class hierarchy: override and virtual
5.Demonstrate a virtual methodDemonstrate a virtual method
6.When a virtual method is not overridden, the base class method is usedWhen a virtual method is not overridden, 
   the base class method is used
7.illustrates polymorphismillustrates polymorphism
8.Demonstrates the use of a virtual method to override a base class methodDemonstrates the use of a virtual method to override a base class method
9.Demonstrates the use of a virtual property to override a base class propertyDemonstrates the use of a virtual property to override a base class property
10.Method override 3Method override 3
11.Test Polymorphism Virtual Functions
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.