Build a derived class of Vehicle for trucks : Class Inheritance « 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# / C Sharp » Class Interface » Class Inheritance 




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 (doublemiles / 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(2200744000);    
    Truck pickup = new Truck(328152000);    
    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
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.