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.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » Class InheritanceScreenshots 
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.