Tuesday, December 13, 2011

Program to explain classes and objects in C-Sharp


//This program creates two vehicle objects.
using System;
class Vehicle
{
    public int passengers, fuelcap, mpg;
}
class Twovehicle
{
    static void Main()
    {
        Vehicle bus = new Vehicle();
        Vehicle car = new Vehicle();

        int range1, range2;

        // Assign values to fields in bus
        bus.passengers = 25;
        bus.fuelcap = 16;
        bus.mpg = 21;

        // Assign values to fields in bus
        car.passengers = 4;
        car.fuelcap = 14;
        car.mpg = 12;
       
        //compute the ranges assuming a full tank of gas
        range1 = bus.fuelcap * bus.mpg;
        range2 = car.fuelcap * car.mpg;

        Console.WriteLine("Bus can carry " + bus.passengers + " with a range of " + range1);
        Console.WriteLine("Car can carry " + car.passengers + " with a range of " + range2);
    }
}
Post a Comment

Google : The top most search engine

Google : MAGIC BOX

nRelate - Posts and Homepage

LinkWithin

Related Posts Plugin for WordPress, Blogger...

Which is the toughest subject ?