Warning: Constant TEMPLATEPATH already defined in /home/u667926427/domains/techcodeninja.com/public_html/wp-includes/default-constants.php on line 416
Object Oriented Programming In C++: Comprehensive 2024 Guide

Concept of Object Oriented Programming In C++

The Concept of object Oriented Programming came in late 1970’s and early 1980’s at Bell Laboratories. It was first used system implementation language for the nascent
Unix Operating System. Bjarne Stroustrup developed extension to C by adding some enhancements specially addition of class into C language. So, it is also called as superset of C Known as “C with Classes”. These extension was further named “C++” and released in 1985.

The errors faced in POP are motivation factors in the invention of OOP approach. The programming in which data is logically represented in the form of across and physically Object Oriented Programming.

Object Oriented Programming (OOP) in C++ is a way of writing code that focuses on organizing information into “objects” that contain both data and the functions that operate on that data.

Imagine an object as a self-contained entity that has its own characteristics (data) and actions (functions) it can perform. For instance, a car object might store information about its color, speed, and model (data), while also having functions to accelerate, brake, and honk the horn.

In C++, OOP allows you to create these objects, defining their properties and behaviors through classes. Classes act as blueprints for creating individual objects, providing a structure for how the objects will work and interact.

Programming Languages and Software Crisis

Developments in software technology continue to be dynamic. New tools and techniques are announced in quick succession. This has forced the software engineers and industry to continuously look for new approaches to software design and development, and they are becoming more and more critical in view of the increasing complexity of software systems as well as the highly competitive nature of the industry. These rapid advances appear to have created a situation of crisis within the industry.

The following issues need to be addressed to face this crisis:

  • How to represent real-life entities of problems in system design?
  • How to design systems with open interfaces?
  • How to ensure reusability and extensibility of modules?
  • How to develop modules that are tolerant to any changes in future?
  • How to improve software productivity and decrease software cost?
  • How to improve the quality of software? How to manage time schedules?
  • How to industrialize the software development process?

According to software industry reports on software implementation suggest that software products should be evaluated carefully for their quality before they are delivered and implemented.

Some of quality issues that must be considered for critical evaluation are:

  • Correctness
  • Maintainability
  • Reusability
  • Openness and interoperability
  • Portability
  • Security
  • Integrity
  • User friendliness

Procedure Vs Object Oriented Programming Language

Q) Comparison on C and C++

Q) Comparing Procedural Programming & Object Oriented Programming Paradigm

Q)Comparison of POP & OOP

Both OOP (Object Oriented Programming) and POP (Procedural Oriented Programming) are languages (high-level) in the world of programming- widely used in application development. These languages have a different approach based on the nature of code development- and thus, they work differently. Before we understand the difference between procedural and object oriented programming, let us know more about them.

The machine language rest on the concept of electricity being turned ‘on’ or ‘off’. From
this on/off, yes/no, two state system, binary system is constructed.

The binary system is based on two digits viz. 0 and 1. M/C language consists of a stream
of 0 and 1 having different combination and different meaning.

Layers of computer software

eg. 11110010 01110011 11010010 00010000 01110000 00101011.
Assembly language was abbreviation and mnemonic code (codes more easily memorized) to replace the 0s and 1s of machine language.

This could be expressed in assembly language (LDA, ADD) statement as

PACK 210(8, 13), 02B(4, 7)

Procedure-Oriented Programming

Conventional Programming, using high level languages such as COBOL (Common
Business Oriented Language), FORTAN (Formula Translation) and C, is commonly called
as procedural oriented programming (POP). POP basically consists of writing a list of
instructions (or actions) for the computer to follow, and organizing these instructions into
groups known as function. A typical program structure for procedural programming is shown in the figure below.

The technique of hierarchical decomposition has been used to specify the task to be completed for solving a problem.

In a multi-function program, many important data items are placed as globally. So that
they may be accessed by all the functions. Each function may have its own local data. The
figure shown below shows the relationship of data and function in a procedure-oriented
program.

Some features of procedure-oriented programming are:

  • Emphasis is on doing things (Algorithms).
  • Large programs are divided into smaller programs called as functions.
  • Most of the functions share global data.
  • Data move openly around the system from function to function.
  • Functions transform data from one form to another.

Object Oriented Programming Paradigm

The object oriented approach is to remove some of the flows encountered in the
procedure approach as a programming model that follows the concept of objects. The objects contain codes in the form of methods and data in the form of attributes. In the case of Object Oriented Programming, it designs computer programs by using the concept of the objects interacting with the real world. There are various Object Oriented Programming languages. But the most popular ones among all are class-based. It means that the objects are instances of the classes determining their types.

Procedure Oriented Programming (c)Object Oriented Programming (c++)
Follow top-down approach of program design.Follow bottom-up approach of program design.
For the development of code, C supports procedural programming. procedural programming.
C++ is known as hybrid language because C++ supports both procedural and object oriented programming paradigms.
Generally data cannot be hidden.Data can be hidden so that non-member function can not access them.
Programs are divided into functions.Program are divided into objects.
Maintaining and enhancing code is still difficult.Maintaining and enhancing code is easy.
Data move from function to functionData and function are tied together. Only related function can access them.
Code reusability is still difficult.Code reusability is easy in compare to POP
Examples: COBOL (Common
Business Oriented Language), FORTAN (Formula Translation), BASIC, ALGOL, Pascal, and C.
Examples: Java, Python, C#, C++, JavaScript, PHP, Ruby, Dart, Perl, Swift, Scala, Objective, Small Talk etc.
C is a subset of C++.C++ is a superset of C.
C contains 32 keywords.C++ contains 63 keywords.
C was developed by Dennis Ritchie between the year 1969 and 1973 at AT&T Bell Labs.C++ was developed by Bjarne Stroustrup in 1979.
C is a function-driven language.C++ is an object-driven language
For the development of code, C supports procedural programming. procedural programming.
C++ is known as hybrid language because C++ supports both procedural and object oriented programming paradigms.

Characteristics of object oriented programming are

  • Emphasis is on data rather than procedure.
  • Data structures are designed such that they characterize the objects.
  • Programs are divided into what are called objects.
  • Functions that operate on the data of an object are tied together in the data structure.
  • Data is hidden and cannot be accessed be external functions.
  • Objects may communicate with each other through functions.
  • New data and functions can be easily added whenever necessary.
  • Follows bottom-up approach in program design.

Feature of Object Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of “objects” that encapsulate data and behavior.

Q) What are the features of Object Oriented Programming? Explain.

Objects

Objects are the basic run-time entities in an object-oriented language. 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 and lists. Program objects should be chosen such that they match closely with the real-world objects. Objects take up space in the memory. E.g. Automobile are objects that have size, weight, color etc as attribute.

Class

In OOP, it is most important to create a class for representing data. A class is a collection of objects of similar types. Classes are user-defined data types and behaves like the built-in types of a programming language. A class also consists method (i.e. function). So, both data and functions are wrapped into a single class. for eg. mango, apple, orange etc are objects of the class fruits.

The data and the operation of a class can be declared as one of the three types

  1. Public
  2. Protected
  3. Private

Inheritance

Q) What is an Inheritance? Write a program to describe the multiple inheritance with suitable example. (VVI Question)

Inheritance is a mechanism that allows a new class (derived or child class) to inherit properties and behavior from an existing class (base or parent class). It promotes code reusability, enabling the creation of new classes based on existing ones and facilitating the extension and customization of functionality.

Data Encapsulation

The process of wrapping up of data and function that operate on data as a single unit is called as data encapsulation.

Or

The wrapping up of data and functions into a single unit (as a class) is known as encapsulation.

Data 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. these methods provide the interface between the object’s data and the program. this insulation of the data from direct access by the program is called data hiding or information hiding.

Data Abstraction

The process of hiding the details of a class from rest of the program (after declaring private) is call data abstraction. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost, and function to operate on these attributes. The attributes are sometimes called data members because they hold information. The functions that operate on these data are sometimes called methods or member functions.

Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables methods to be written to handle objects of a base class, and these methods can be used with objects of any derived class. Polymorphism can take the form of method overriding (having a method in a subclass with the same name as a method in its superclass) or method overloading (multiple methods having the same name but different parameters).

The process of making an operator to exhibit different behavior at different instance is called operator overloading.

Another example of polymorphism is function overloading, where a single function can perform various different types of task.

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 call at run-time. It is also called late binding. This is associated with polymorphism and inheritance. A function call associated with a polymorphic reference depends on the dynamic type of that reference.

Message communication

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

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

Objects communicate with one another by sending and receiving information much the same way as people pass messages to one another. The concept of message passing makes it easier to talk about building systems that directly model or simulate their real-world counterparts.

Advantage and Disadvantage of OOPs

Pros

  • Redundant code is eliminated by various techniques like inheritance.
  • System can easily be upgraded from small to large.
  • Easy for managing complex and large size problems.
  • Code reusability is easier
  • Follow bottom-up approach

Cons

  • Compiler and runtime overhead.
  • Requires the mastery in software engineering
  • Difficult to trace errors and debug.

Compilers and Linking

The process of compiling and linking again depends upon the operating system. A few popular systems are

Unix AT&T C++

This process of implementation of a C++ program under UNIX is similar to that of a C program. We should use the “cc” (uppercase) command to compile the program. Remember, we use lowercase “cc” for compiling C programs. The command

CC example.C

Turbo C++ and Borland C++

Turbo C++ and Borland C++ provide an integrated program development environment under MS DOS. They provide a built-in editor and a menu bar includes options such as File, Edit, Compile and Run.

We can create and save the source files under the File option, and edit them under the Edit option. We can then compile the program under the compile option and execute it under the Run option. The Run option can be used without compiling the source code. 

Visual C++

It is a Microsoft application development system for C++ that runs under Windows. Visual C++ is a visual programming environment in which basic program components can be selected through menu choices, buttons, icons, and other predetermined methods. Development and execution of C++ programs under Windows are briefly explained in Appendix C.

Programming Structure in C++

A typical C++ program would contain four sections. This section may be placed in separate code files and then compiled independently or jointly.

It is a common practice to organize a program into three separate files. The class declarations are placed in a header file and the definitions of member functions go into another file. This approach enables the programmer to separate the abstract specification of the interface from the implementation details (member function definition).

Finally, the main program that uses the class is places in a third file which “includes: the previous two files as well as any other file required.

Include Files 

Class declaration

Member functions definitions

Main function program
Structure of a C++ program

Comments:

C++ introduces a new comment symbol // (double slash). Comment start with a double slash symbol and terminate at the end of the line. A comment may start anywhere in the line, and whatever follows till the end of the line is ignored. It has no closing symbol.

 The double slash comment is basically a single line comment. Multiline comments can be written as /*,*/

// This is an example of Single line comment in C++


/* 
This is an example of 
multiline comment in
C++ program

*/

Symbol:

It indicates pre-processor directive to the compiler. All C++ programs begin with “#include” directive with specified header file.

The iostream header file:

“#include<iosteam.h>” adds the contents of iostream.h file to the program

intmain()

All C++ programs have at least a function main(). It is entry point of every program. The word before function is the return value of the function main() ie. ‘int’

The Output Operator(using “Cout”)

The statement cout<<“Hi Everybody”; in the program prints the phrase in quotation marks on the screen.

‘Cout’ is prounced as ‘See Out’

<< is called insertion or put to operator

The Input Operator(using “Cin”)

A statement Cin>> a; is an input statement which causes the program to wait for user to type and give some input.

Cin is pronounced as See In

‘>>’ is called extraction or get from operator.

Cascading I/O operators (multiple input/output)

The I/O operators can be used repeatedly in a single I/O statement as follows:

Cout <<a<<b<<c;
Cin >>x>>y>>z;

OOP Program Structure:

#include<iostream.h>
#include<conio.h>
class my first program {
public:
void display() {
Cout<<"My first program";
} } ;
void main() {
My first C++ progra, OOP;
OOP.display();
getch();
}

Additional Data types, token in C++

Built-in-type is also known as basic or fundamental data type. The basic data type may have several modifiers preceding them to serve the needs of various situations except void. The modifier signed, unsigned, long and short may be applied to character and integer basic data types. Long modifier may also be applied to double data type representation in memory in terms of size (Bytes)

Data types in C++ can be classified as:

  1. Built-in-type/Basic Datatype
    • Integer type
      • int
      • chat
    • Void
    • Floating type
      • Float
      • Double
  2. Derived type : array, function, pointer, reference
  3. User-defined type : structure, union, class, enumeration

Insertion and Extraction Operators

Insertion

The insertion operator << is the one we usually use for output, as in:

Cout<< “this is output” <<endl; 

It gets its name from the idea of inserting data into the output stream.

Extraction

The extraction operator >> is the one we usually use for input, as in:

Cin>> X;

It gets its name from the idea of extracting data from the input stream.

Conclusion

Object Oriented Programming in C++ unlocks a powerful approach to software design and development. Embrace the principles and best practices to craft efficient, maintainable, and scalable applications.

Frequently Asked Questions (FAQs)

Any prerequisites Language to learn C++?

Not Compulsory but if you learn first C language then it will become easy to understand. and also have some basic knowledge of backend programming language.

What are the main pillars of OOP in C++?

The pillars of OOP in C++ are encapsulation, inheritance, polymorphism, and abstraction. the cornerstone of Object-Oriented Programming in C++, enabling developers to create efficient, modular, and maintainable code.

What is Object-Oriented Programming (OOP)?

Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of objects, encapsulation, inheritance, and polymorphism.

Why is Object-Oriented Programming important in C++?

OOP in C++ allows for efficient code organization, reusability, and the creation of scalable applications.

What are the key features of OOP in C++?

The key features include classes, objects, encapsulation, inheritance, and polymorphism.

How does OOP in C++ differ from procedural programming?

OOP focuses on objects and their interactions, whereas procedural programming revolves around procedures or functions.

What are the challenges of using OOP in C++?

Challenges include managing complexity in large projects, performance considerations, and potential issues related to overusing inheritance.