Classes and Objects


Classes in Object oriented programming
  •  A class is simply a representation of a type of object.
  • Class is composed of three things: a name, attributes, and operations.  
  • There was some major problems in earlier approach is that the data and the functions working upon the data being separated.
  • But the rule of Object oriented says that if the data and functions acting upon the data can go together let them be together, So we define a unit which contains data and its function together as a Class.
Class Example

 The Employee class would be like this

 /*------------------------------------------------------------*/

public class Employee // class name
{
    int EmployeeID;      // These are instance variables. They belong to object. 
    string EmployeeName; // These are attributes of a class at any point of time the value of these variables determines the state of the object.
    string EmployeeAddress;
    double EmployeeBasicSalary; 

    public void AddDetails()  // Methods or Member function. These determine the behavior of a class
    {
        // Logic to add the details of employee.
    }
    public void PrintDetails()
    {
        // Logic to print the details of employee.
    }
}

 /*------------------------------------------------------------*/

Objects in Object oriented programming
  • An object is a software bundle of variables and related methods.
  • Objects are related to real life such as people,cars and houses.
  • Objects are instances of classes
  • The object is declared in the below way
                   Employee emp1 = new Employee();
  • An object has following characteristics
  1. Attribute: The set of values of an attribute of a particular object is called its state. In Class Program attribute can be a string or it can be a integer or any other simple or complex Datatype.
  2. Behaviour: Every object has behavior. If a behavior of an object needs to be performed, then the corresponding method is called.
  3. Identity: Each time an object is created the object identity is been defined.This identity is usually created using an identifier which is derived from the type of item

No comments:

Post a Comment