C++ Object Oriented Programming(OOP)

Object Oriented Programming(OOP)

Object Oriented programs involve creating classes, creating objects from those classes, and creating applications, which are stand-alone executable programs that use those objects. After being created, classes can be reused over and over again to develop new programs. Actual use of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.Object Oriented Programming is a paradigm that provides many concepts such as

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Object

Object is an instance of a class, which hold the data of the variable declared in Class. Objects take up space in the memory and have an associated address like a record in Pascal, or a structure in c.

Class

A class is a user defined data type. A class is a logical abstraction. It is a template that defines the form of an object. A class specifies both code and data. It is not until an object of that class has been created that a physical representation of that class exists in memory. When you define a class, you declare the data that it contains and the code that operates on that data. Data is contained in instance variables defined by the class known as data members, and code is contained in functions known as member functions. The code and data that constitute a class are called members of the class.

Example

Inheritance

Inheritance permits better structuring of the software and helps in reducing the amount of code, as common sections of code can be reused. Inheritance is probably the most powerful feature of object-oriented programming, after classes themselves.Inheritance is the process of creating new classes, called derived classes, from existing or base classes. The derived class inherits all the capabilities of the base class. So, in short, Inheritance is the process by which objects of one class acquire the properties of objects of another class in the hierarchy manner.

Base & Derived Classes

Access specifiers/Visibility modes in Inheritance

There are three types of Access specifiers:

  • Public: When deriving a class from a public base class. Then the public members of the base class become public members of the derived class.
  • Protected: When deriving from a private base class. Then both public member and protected members of the base class will become Private in the derived class.
  • Private: When deriving from a protected base class. Then both public member and protected members of the base class will become protected in the derived class.

Type of Inheritance

  • Single inheritance
  • Multiple inheritance
  • Multilevel inheritance
  • Hierarchical inheritance
  • Hybrid inheritance

Single Inheritance

When a derived class inherits from a single base class, it is referred to as single inheritance. In single inheritance, the derived class inherits all the members of its base class and can directly access the public and protected members of the base class.

Multiple Inheritance

When a derived class inherits from more than one base class simultaneously, it is referred to as multiple inheritances. In multiple inheritances, the derived class inherits the members of all its base classes and can directly access the public and the protected members of its base class.

Multilevel Inhertance

When one class is inherited from another class, which in turn is inherited from some other class, it is referred to as multilevel inheritance. Multilevel inheritance comprises two or more levels. However, in this unit, only two levels of multilevel inheritance are discussed. The derived class is inherited from the direct base class which in turn is derived from another base class. Hence, the derived class has all the members of its direct base class as well as its indirect base class. This is known as the transitive nature of multilevel inheritance.

Hierarchical Inheritance

Hierarchical inheritance is a type of inheritance in which more than one class is derived from a single base class. In hierarchical inheritance, a base class provides members that are common to all of its derived classes.

Hybrid Inheitance

According to the user requirement, the various types of inheritance can be combined in a program. Such inheritance is called hybrid inheritance.

Polymorphism

Polymorphism (a Greek word meaning having multiple forms) is the ability of an entity, such as a function or a message to be processed in more than one form. In C++, polymorphism can be achieved either at compile-time or at runtime. At compile time, polymorphism is implemented using operator overloading and function overloading. However, at runtime, it is implemented using virtual functions.

Abstraction

Abstraction is a mechanism to hide irrelevant details and represent only the essential features so that one can focus on important things at a time. For example, while driving a car, a driver only knows the essential features to drive a car such as how to use the clutch, brake, accelerator, gears, steering, etc. and least bothers about the internal details of the car like motor, engine, wiring, etc. Abstraction can be of two types, namely, data abstraction and control abstraction. Data abstraction represents essential features without including background details and control abstraction means hiding the implementation details.

Encapsulation

In C++, objects are described by means of class definitions. A class definition collectively defines data and the functions that operate on this data. Software produced according to this principle is more robust, easier to maintain and easier to extend since there are fewer dependencies between the modules and the details of the implementation are encapsulated in classes.