Build a derived class of Vehicle for trucks : Class Inheritance « Class Interface « C# / C Sharp






Build a derived class of Vehicle for trucks

Build a derived class of Vehicle for trucks
/*
C# A Beginner's Guide
By Schildt

Publisher: Osborne McGraw-Hill
ISBN: 0072133295
*/

/* 
  Project 8-1 
 
  Build a derived class of Vehicle for trucks. 
*/ 
using System; 
 
class Vehicle {    
  int pri_passengers; // number of passengers    
  int pri_fuelcap;    // fuel capacity in gallons   
  int pri_mpg;        // fuel consumption in miles per gallon   
   
  // This is a constructor for Vehicle.  
  public Vehicle(int p, int f, int m) {  
    passengers = p;  
    fuelcap = f;  
    mpg = m;  
  }  
 
  // Return the range.   
  public int range() {   
    return mpg * fuelcap;   
  }   
   
  // Compute fuel needed for a given distance.  
  public double fuelneeded(int miles) {   
    return (double) miles / mpg;   
  } 
 
  // Properties 
  public int passengers { 
    get { return pri_passengers; } 
    set { pri_passengers = value; } 
  }   
 
  public int fuelcap { 
    get { return pri_fuelcap; } 
    set { pri_fuelcap = value; } 
  }   
 
  public int mpg { 
    get { return pri_mpg; } 
    set { pri_mpg = value; } 
  }   
}    
  
// Use Vehicle to create a Truck specialization.    
class Truck : Vehicle {  
  int pri_cargocap; // cargo capacity in pounds  
  
  // This is a constructor for Truck.  
  public Truck(int p, int f, int m, int c) : base(p, f, m)  
  {  
    cargocap = c;  
  }  
 
  // Property for cargocap. 
  public int cargocap { 
    get { return pri_cargocap; } 
    set { pri_cargocap = value; } 
  }   
}  
    
public class TruckDemo {    
  public static void Main() {    
  
    // construct some trucks 
    Truck semi = new Truck(2, 200, 7, 44000);    
    Truck pickup = new Truck(3, 28, 15, 2000);    
    double gallons;   
    int dist = 252;   
   
    gallons = semi.fuelneeded(dist);    
    
    Console.WriteLine("Semi can carry " + semi.cargocap +  
                       " pounds."); 
    Console.WriteLine("To go " + dist + " miles semi needs " +   
                       gallons + " gallons of fuel.\n");   
       
    gallons = pickup.fuelneeded(dist);    
    
    Console.WriteLine("Pickup can carry " + pickup.cargocap +  
                       " pounds."); 
    Console.WriteLine("To go " + dist + " miles pickup needs " +   
                       gallons + " gallons of fuel.");  
  }    
}


           
       








Related examples in the same category

1.Inheritance 3Inheritance 3
2.Four layers of class hierarchyFour layers of class hierarchy
3.An example of inheritance-related name hidingAn example of inheritance-related name hiding
4.Using base to overcome name hidingUsing base to overcome name hiding
5.Call a hidden methodCall a hidden method
6.A multilevel hierarchy 1A multilevel hierarchy 1
7.Demonstrate when constructors are calledDemonstrate when constructors are called
8.illustrates inheritanceillustrates inheritance
9.Private field and public Property in inheritancePrivate field and public Property in inheritance
10.Illustrates versioningIllustrates versioning
11.Class Hierarchy testClass Hierarchy test
12.Class Hierarchy with two children classClass Hierarchy with two children class
13.A simple class hierarchyA simple class hierarchy
14.A base class reference can refer to a derived class objectA base class reference can refer to a derived class object
15.Pass a derived class reference to a base class referencePass a derived class reference to a base class reference
16.a multilevel hierarchya multilevel hierarchy