C++ Procedural to OOP : Migration

   

Class and Object

Class and Object in c++

Class:

  • It is user defined data type combining data members and member functions.
  • We can restrict the access of the member using access specifiers .(public, private, protected)
  • By default every member in class is private.

Object:

  • It is the variable of class.
  • Object gets physical memory and contains data members which are declared into the class.

Example:

class Sample              // class declaration
{
private:                  // access specifier
        int a;
        int b;           // data members
public:
       void function();  // member function
};
int main()
{
    Sample s1;   // s1 is an object of class Sample
    //----- code----
    return 0;
}

Characteristics of an object:

1. State:

  • State of an object is determined by the values of data members of that object.
  • If two objects have same value of data members, then the two objects are having same state.

2. Behavior:

  • A member function in the class decides behavior of the object.
  • Two objects having different states will show different behavior by calling the same member function.

3. Identity:

  • Every object has unique address.

Data members:

  • Data members are declared with any of the fundamental types, also with pointers, reference, array types and user-defined types.
  • By default every data member in class is private.
  • Initializers are not allowed inside the class definition.

Member functions:

1. Constructor:

  • It is a member function of the class having same name as that of class.
  • It doesn’t have any return type. (Not even ‘void’)
  • Constructor is automatically called only once in a lifetime of an object when it is created.
  • Constructor initializes data members of the class.
  • If we don’t write any constructor, compiler provides default constructor for the class.
  • Constructor can be overloaded.
class Sample
{
private:
        int a;
public:
       Sample();        //constructor
       ~Sample();       //destructor
};
int main()
{
    Sample s1;   //constructor is called
    //----- code----
    return 0;
}

2. Destructor:

  • It is a member function of the class having same name as that of class preceded by “~”.
  • It doesn’t have any return type. (Not even ‘void’)
  • Destructor is automatically called when object goes out of scope.
  • Destructor de-initializes data members of the class.
  • If we don’t write any destructor, compiler provides default destructor for the class.
  • Destructor can NOT be overloaded.

Mutators:

  • Mutators are the member functions of the class, which modify state of the object.
  • These functions modify values of the data members.
  • These are typically setter functions in the class.
class Sample
{
private:
        int a;
public:
       Sample();        //constructor
       ~Sample();       //destructor
       int GetA();      //accessers
       void SetA(int);  //mutators
       void Display();  //Facilitators
};

Accessers:

  • Inspectors are the member functions of the class, which read the state of the object.
  • These are typically getter functions in the class

Facilitators:

  • Facilitators are the member functions of the class, which do not directly contribute to the operation of that class.
  • This function provides additional functionality like scanning or printing object data.