Object-Oriented Programming: Concepts and Principles

Basic Concepts of Object Oriented Programming

It is necessary to understand some of the concepts used extensively in object-oriented programming. These include:

  1. Objects
  2. Classes
  3. Data abstraction and encapsulation
  4. Inheritance
  5. Polymorphism
  6. Dynamic binding
  7. Message passing

1. Objects

Objects are the basic run time entities in an object-oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program has to handle. They may also represent user-defined data such as vectors, time etc.

2. Classes

We just mentioned that objects contain data, and code to manipulate that data. The entire set of data and code of an object can be made a user-defined data type with the help of class. In fact, objects are variables of the type class.

3. Data Abstraction

Abstraction refers to the act of representing essential features without including the background details or explanation.

4. Encapsulation

The wrapping up of data and function into a single unit (called class) is known as encapsulation. Data and encapsulation is the most striking feature of a class. The data is not accessible to the outside world, and only those functions which are wrapped in the class can access it.

5. Inheritance

Inheritance is the process by which objects of one class acquired the properties of objects of another classes. It supports the concept of hierarchical classification.

6. Polymorphism

Polymorphism is another important OOP concept. Polymorphism, a Greek term, means the ability to take more than one form. An operation may exhibit different behavior in different instances. The behavior depends upon the types of data used in the operation.

The process of making an operator to exhibit different behaviors in different instances is known as operator overloading. something similar to a particular word having several different meanings depending upon the context. Using a single function name to perform different type of task is known as function overloading.

7. Dynamic Binding

Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run time.

8. Message Passing

An object-oriented program consists of a set of objects that communicate with each other. The process of programming in an object-oriented language, involves the following basic steps:

  1. Creating classes that define object and their behavior,
  2. Creating objects from class definitions, and
  3. Establishing communication among objects.

Constructors

A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the class.

Characteristics of Constructors

  • They should be declared in the public section.
  • They are invoked automatically when the objects are created.
  • They do not have return types, not even void and they cannot return values.
  • They cannot be inherited, though a derived class can call the base class constructor.
  • Like other C++ functions, Constructors can have default arguments.
  • Constructors cannot be virtual.

Default constructor

A constructor that accepts no parameters is called the default constructor. The default constructor for class Student is Student : : Student( ).

Parametrized constructor

achieved by passing arguments to the constructor function when the objects are created. The constructors that can take arguments are called parameterized constructors.

Copy Constructor

A copy constructor is used to declare and initialize an object from another object.

Dynamic Constructors

The constructors can also be used to allocate memory while creating objects. Allocation of memory to objects at the time of their construction is known as dynamic construction of objct

Destructors

A destructor is used to destroy the objects that have been created by a constructor. Like constructor, the destructor is a member function whose name is the same as the class name but is preceded by a tilde (~).

OPERATOR OVERLOADING

This language has ability to provide the operators with a special meaning for a data types. The mechanism of giving such special meanings to an operator is known as operator overloading.

We can overload (give additional meaning to) all the C++ operators except:

  • Class member access operators ( . & .*)
  • Scope resolution operators ( : : )
  • Size operator (sizeof)
  • Conditional operators (? : )

Rules For Overloading Operators

  • Only existing operators can be overloaded. New operators cannot be created.

  • The overloaded operator must have at least one operand that is of user-defined type.

  • We cannot change the basic meaning of an operator.

  • Overloaded operators follow the syntax rules of the original operators.

  • Binary arithmetic operators such as +, -, * and / must explicitly return a value. They must not attempt to change their own arguments.

INHERITANCE

Reusability is an important feature of OOP. C++ strongly supports the concept of reusability. The mechanism of deriving a new class from an old one is called inheritance (or derivation). The old class is referred to as base class. The new class is called the derived class or subclass.

Forms of inheritance are:

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

Single Inheritance

A derived class with only one base class.

Multiple Inheritance

A derived class with several base classes. Multiple inheritance allows us to combine the features of several existing classes as a starting point for defining new classes.

Multilevel Inheritance

The mechanism of deriving a class from another derived class.

Hierarchical Inheritance

Traits of one class may be inherited by more than one class

Hybrid Inheritance

The mechanism of deriving a class by using a mixture of different methods.

Friend Functions

The private members cannot be accessed from outside the class. That is, non-member function cannot have an access to the private data of a class.The functions that are declare with the keyword friend are known as friend function.A function can be declared as a friend in any number of classes.

Procedural Programming (POP)*

  • Focuses on procedures and functions that perform specific tasks
  • Programs are built around procedures and functions
  • Data is shared among functions through parameters and global variables
  • Emphasizes step-by-step execution of algorithms
  • No encapsulation or data hiding
  • Examples: C, Pascal

Object-Oriented Programming (OOP)*

  • Focuses on objects and classes that represent real-world entities
  • Programs are built around objects and their interactions
  • Data is encapsulated within objects, and access is controlled through methods
  • Emphasizes modularity, reusability, and abstraction
  • Supports inheritance, polymorphism, and encapsulation
  • Examples: C++, Java, Python

Polymorphism

Polymorphism is a fundamental concept in Object-Oriented Programming (OOP) that allows for:

  1. Method Overloading: Multiple methods with the same name but different parameters.
  2. Method Overriding: A subclass provides a different implementation of a method already defined in its superclass.
  3. Operator Overloading: Redefining operators for custom classes, allowing intuitive operations.
  4. Function Polymorphism: Functions with the same name but different parameters or return types.

Virtual function

A virtual function in C++ is a base class member function that you can redefine in a derived class to achieve polymorphism. You can declare the function in the base class using the virtual keyword

File streams

File streams come in two flavors also: the class ifstream (input file stream) inherits from istream, and the class ofstream (output file stream) inherits from ostream. Thus all of the member functions and operators that you can apply to an istream or ostream object can also be applied to ifstream and ofstream objects.

Define an array. Explain the use of array of objects with example.

  • Array is a set of Elements having the same data type, or we can Say that Arrays are Collections of Elements having the same name and same data type.
  • The Array of Objects stores objects. An array of a class type is also known as an array of objects.

Advantages of Array of Objects:


1. The array of objects represent storing multiple objects in a single name.
2. In an array of objects, the data can be accessed randomly by using the
index number.
3. Reduce the time and memory by storing the data in a single variable

Array is a set of Elements having the same data type, or we can Say that
Arrays are Collections of Elements having the same name and same data
type.
● The Array of Objects stores objects. An array of a class type is also known as an array of objects.


Function overloading is a feature of object-oriented programming where
two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called FunctionOverloading. In Function Overloading “Function” name should be the same and
the arguments should be different. Eg polymorphism.

Inline function is the optimization technique used by the compilers.One can simply prepend inline keyword to function prototype to make a function
inline. Inline function instruct compiler to insert complete body of the function wherever that function got used in code.
Advantages :-
1) It does not require function calling overhead.
2) It also save overhead of variables push/pop on the stack, while function calling.
3) It also save overhead of return call from a function.
4) It increases locality of reference by utilizing instruction cache.
Disadvantages :-
1) May increase function size so that it may not fit on the cache, causing lots of
cahce miss.
2) After in-lining function if variables number which are going to use register
increases than they may create overhead on register variable resource utilization.
3) It may cause compilation overhead as if some body changes code inside inline
function than all calling location will also be compiled.
4) If used in header file, it will make your header file size large and may also make
it unreadable.