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