Showing posts with label property. Show all posts
Showing posts with label property. Show all posts

Tuesday, December 4, 2012

A simple 'property' program

What is properties:
  • Properties are like data fields, but have logic behind them
  • From the outside, they look like any other member variable
              But they act like a member function
  • Defined like a field, with 'get' and 'set' code added
  • Can use access modifiers like fields and methode can

Why use Properties?

  • properties can be calculated on-the-fly
  • provide complete control over data field access
            logic is hidden from the object's consumer
  • Properties promote the idea of encapsulation

 //A simple program to implement property
//using c#

using System;

class company
{
    public void interview_sch()
    {
        Console.WriteLine ( "your interview is schedule" );
    }
}

class candidate
{
    private string skills;
   
    public string can_skills                // This
    {                                             // is
        get                                      // 
        {                                         // P
            return skills;     //retrieve    // R
        }                                         // O
                                                   // P
        set                                      // E
        {                                        // R
            skills = value;   //assign      // T
        }                                        // Y
    }                                            //
}

class consultant
{
    public static void Main()
    {
        candidate anil = new candidate();
        anil.can_skills = "java";
        string s = anil.can_skills;
       
        if (s == "c")
        {
            company c = new company();
            c.interview_sch();
        }
       
        else
        {
            Console.WriteLine ( "your criteria deoesn't match" );
        }
    }
}

Sunday, October 21, 2012

Setter & Getter in c++

  1. A property, is a special sort of class member; or intermediate between a field (or data member) and a method.
  2. Properties are read and written like fields, but property reads and writes are (usually) translated to get and set method calls.
  3. Property allows us data validation.

That is, properties are intermediate between member code (methods) and member data  (instance variables) of the class, and properties provide a higher level of encapsulation than public fields.

//property in c++

#include <iostream>
using namespace std;

class student
{
    public:
        void setName(string x)
        {
            name = x;
        }
        string getName()
        {
            return name;
        }
       
    private:
        string name;
};

int main()
{
    student bio_stud;


    bio_stud.setName("sir Virender");
    cout << bio_stud.getName();   
}


OUTPUT:
sir Virender


NOTE:
  1. C++ does not have first class properties, but there exist several ways to emulate properties to a limited degree.
  2. C++ require the programmer to define a pair of accessor and mutator methods.  
Further reading property and indexer in cpp