Friday, April 20, 2012

A simple program to understand classes in C#

using System;

class building
{
    public int floors;   //number of floors         //C# class doesn't support 'public :'
    public int area;     //total square footage of building
    public int occupants;  //number of occupants
}

class building_Demo
{
    public static void Main()
    {
        building house = new building();  //create a bbuilding object
        int areaPP;    //area per person
      
        //assign values to fields to fields in house
        house.occupants = 4;
        house.area = 2500;
        house.floors = 2;
  
    //compute the area per person
    areaPP = house.area/house.occupants;
  
    Console.WriteLine("house has:\n "+
                       house.floors + " floors\n "+
                       house.occupants + " occupants\n "+
                       house.area + " total area\n "+
                       areaPP + " area per person");
    }
}


OUTPUT:
 house has:
  2  floors
  4  occupants
  2500  total area
  625 area per person

Thursday, April 5, 2012

A simple 'class' program

//class with single object
// using c++

#include <iostream>
using namespace std;

class CRectangle 
//here we define a user-defined-datatype 'CRectangle' using 'class'
{
    private:
        int x, y;


    public:
        void set_values (int,int);


        int area()
        {
            return (x*y);
        }
};

void CRectangle :: set_values (int a, int b)
{
    x = a;
    y = b;
}

int main()
{
    CRectangle rect;    //creating the object (rect) of a class (CRectangle)


    rect.set_values (3,4);
    cout << "area:" << rect.area();
   
    cin.ignore();
    cin.get();
}



Note: we must use the operator scope-resulation (::) to specify that we are defining a function that is a member of the class CRectangle and not a regular global function.
 _________________________________________________________
See also the same program using struct (only difference, we use 'struct' instead of 'class') 

A simple 'struct' program

// simple struct (user defined datatype) program
// using c++


#include <iostream>
using namespace std;

struct student                 //student is user defind data type
{
    int roll_no;
    char name;
    int age;
};

int main()
{
    student a;              //a is the object of student


    cout << "\nEnter the name of student: ";
    cin >> a.name;


    cout << "\n Enter your age: ";
    cin >> a.age;


    cout << "\n enter your roll no: ";
    cin >> a.roll_no;
   
    cout << "\n student name is :" << a.name;
    cout << "\n               age is :" << a.age;
    cout << "\n               roll no :" << a.roll_no;
   
}

Monday, April 2, 2012

Program to illustrate 'const' keyword

The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable cannot be modified.

// program to illustrate const keyword
// using  c#


using System;

public class ConstTest
{
    class MyClass
    {
        public int x;
        public int y;
        public const int c1 = 5;
        public const int c2 = c1 + 5;
       
        public  MyClass(int p1, int p2)
        {
            x = p1;
            y = p2;
        }
    }
   
    public static void Main()
    {
        MyClass mC = new MyClass(11, 22);
        Console.WriteLine ("x = {0}, y = {1}", mC.x, mC.y);
        Console.WriteLine ("c1 = {0}, c2 = {1}", MyClass.c1, MyClass.c2);
    }
}

OUTPUT:
x = 11, y = 22
c1 = 5, c2 = 10