Monday, 30 April 2012

Inheritance in C# ( Camera Example )


using System;
public class Camera
{
  public string Company;
  public string Model;
  public Camera(string Company, string Model)
  {
    this.Company = Company;
    this.Model = Model;
  }   
  public void Start()
  {
    Console.WriteLine(Model + " started");
  }
}
   
public class Cam : Camera
{
  public bool DSLR;
  public Cam(string Company, string Model, bool DSLR) :
  base(Company, Model) 
  {
    this.DSLR = DSLR;
  }   
}

public class SLR : Camera
{
   
  public bool Autofocus;
  public SLR(string Company,
    string Model, bool Autofocus) :
  base(Company, Model) 
  {
    this.Autofocus = Autofocus;
  }
   
  public void shoot!()
  {
    Console.WriteLine(Model + " shoot!!!");
  }   
}
   
class xyzabc
{
  public static void Main()
  { 
    Cam myCam = new Cam("Canon", "PowerShot110", true);
    Console.WriteLine("myCam.Company = " + myCam.Company);
    Console.WriteLine("myCam.Model = " + myCam.Model);
    Console.WriteLine(
      "myCam.DSLR = " + myCam.DSLR);
    myCam.Start();
    SLR mySLR =
      new SLR("Harley-Davidson", "V-Rod", false);
    Console.WriteLine(
      "mySLR.Company = " + mySLR.Company);
    Console.WriteLine(
      "mySLR.Model = " + mySLR.Model);
    Console.WriteLine(
      "mySLR.Autofocus = " + mySLR.Autofocus);
    mySLR.Start();
    mySLR.shoot!();
  }
}
 

No comments:

Post a Comment