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

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Language Basics » 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
w___ww___._j_a___v__a___2s_.___c___o__m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.