Add to Technorati Favorites

Object Oriented Concepts in C++

In today's part, we will learn object oriented concepts of C++ (naturally). C++ is an object-oriented language. So, let us start with object itself.

  • An object in C++ is a representation of some physical, logical, or conceptual entity. For example, an object can be a vehicle, bank-account, animal, weapon etc.
  • An Object is an instance of user-defined data type called Class. We can also say that object is a variable whose type is a user-defined Class.

  • Similar objects belong to a class. For example, cat and dog belongs to animal class.
  • Class is a blueprint/boiler-plate/template for instantiating an object.
  • Class encapsulates the Attributes and Behaviour of an entity.

Object is a run-type entity in C++ where as Class is just a template for generating an object


  • Abstraction is representation of only the essential features of an entity. For example, to drive a car, you only need to know about accelerator, brake, clutch, and some other controls without ever knowing how the things inside the car chasis work.
  • Encapsulation together with data/information hiding feature allows limited visibility to the class users. Access specifiers (private, public, and protected) in C++ are used to implement data hiding.
  • Abstraction helps in managing the complexity of the software.
  • Encapsulation is bundling together the attributes and behaviours of an entity
  • Object-oriented approach is a data-centric approach. Data toegether with operations that can manipulate it are encapsulation into a Class.
  • Inheritance is similar to inheriting characteristics from our parents.
  • Software development is costly and time-consuming. Object-orientation not only helps in managing complexity but also promotes software reusability through its 'inheritance' feature.
  • Using inheritance, a class (called derived-class) inherit the already well-developed methods from another class (called base-class); thereby saving lot of time and money.
  • Literally, polymorphism means many forms.
  • A function can exhibit many forms depending on the class of which it is a part. For example , we have a function called make_sound(). If is part of human object, make_sound() returns 'speak' and if it is a part of dog object then it returns 'bark'
  • In C++, polymorphism can be of 3 types:
  • Function overloading: Functions of a class with same name but either different number of arguments or different types of arguments. Howver, you cannot overload a function which can same number of arguments of same type and in same sequence and differing only in their return types. For example, you behave differently when your friend asks something and when your boss asks same thing.
  • Operator Overloading: This feature makes C++ as extensible language. You can overload the common built-in operators of C++ for user-defined data types as well. You can overload most operators but not all. Also, you cannot change the semantics of an operator. Which operators you cannot overload, what does semantics mean, and the guidelines for operator overloading, you will find in later post.
  • Dynamic/Late Binding: This is the truest form of polymorphism. It works in case of inheritance. One can access method of a derived class using a pointer of a base class depending on to which object it points to at run-time. C++ implements this using concept of virtual functions. The common example cited in C++ literature is that of a Shape base class and many derived classes like rectangle, circle. Each of these classes implement their own Draw() function. When you call virtual Draw() function of base class, the Draw() method of derived class to which base class pointer points is called. This feature also allows adding of any derived class without worrying about its methods to be known to base class.
This completes our today's discussion. Naturally, attributes are data-members of a class just like we have eyes and ears. Values of these attributes at any instance will determine state of the object. Behaviour refers to methods or member functions of a class just like we eat and move.

Naturally, there's more coming in next post tommarrow.

No comments: