C++ Classes and Objects

C++ supports object oriented programming. Object-oriented programming is based on the concept of classes and objects. The use of classes and objects makes it easier to build and design applications and shows close resemblance to the real world.

WHAT IS CLASS?
  • It is a user defined data type which serves as a blueprint for an object. 
  • It is a logical entity i.e. no storage is assigned when we define a class.
  • A single class can be used to create multiple objects.
  • A class describes the contents of the objects that belong to it.

WHAT IS OBJECT?
  • An object is an instance of a class.
  • An object is the actual component of program i.e. storage is assigned when we create an object.
  • It is an entity having some characteristics and behavior.
  • An object’s properties are what it knows and its methods are what it can do. 
  • Objects are the fundamental building blocks of applications from an object-oriented perspective.

EXAMPLE

The concept of classes and objects can be understood by the following example:

Human Beings have certain properties and behavior. Each person differs from the other on the basis of one or more properties like name, age, gender, height, weight etc. Thus, we all share certain properties and are distinguished based on them.

Let us define a class named HumanBeing

class HumanBeing
{
          string name;
          int age;
          char gender;
          float height;
          float weight;
};

All Human Beings share common behavior such as breathing, eating, drinking etc. Though Human Beings can be distinguished by their set of properties; but all of them breathe, drink and eat. Thus, all the objects of the same class have the same behavior.

Lets redefine our class adding some behavior,

class HumanBeing
{
          //Properties or Data Members

          string name;
          int age;
          char gender;
          float height;
          float weight;

          // Behavior or Member Functions

          Breathe();
          Eat();
          Drink();
};

Once we have defined a class, we can define as many objects as required. Each object will possess some properties and behavior. The properties of objects may be same or different depending upon the context.

HumanBeing Alice, Bob, Steve;

Here Alice, Bob and Steve are objects of HumanBeing class. Each of them possess a set of properties and common behavior or methods.


Share this

Related Posts

1 comments :

comments
November 12, 2018 at 6:20 PM delete

thank you for sharing useful post.
c++ programming tutorial
welookups

Reply
avatar

FIND US ON FACEBOOK!