KEMBAR78
Object Oriented Programming in C++ | PDF | Object Oriented Programming | Computer Programming
0% found this document useful (0 votes)
8 views20 pages

Object Oriented Programming in C++

Uploaded by

frustedpatel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views20 pages

Object Oriented Programming in C++

Uploaded by

frustedpatel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Object Oriented Programming in C++

I. Procedural Programming:
Procedural Programming can be defined as a programming model which is derived from
structured programming, based upon the concept of calling procedure. Procedures, also
known as routines, subroutines or functions, simply consist of a series of computational steps
to be carried out. During a program’s execution, any given procedure might be called at any
point, including by other procedures or itself.

Languages used in Procedural Programming: FORTRAN, ALGOL, COBOL, BASIC,


Pascal and C.

II. Object Oriented Programming:


Object oriented programming can be defined as a programming model which is based upon
the concept of objects. Objects contain data in the form of attributes and code in the form of
methods. In object oriented programming, computer programs are designed using the concept
of objects that interact with real world. Object oriented programming languages are various
but the most popular ones are class-based, meaning that objects are instances of classes,
which also determine their types.

Languages used in Object Oriented Programming: Java, C++, C#, Python, PHP,
JavaScript, Ruby, Perl, Objective-C, Dart, Swift, Scala.

III. Advantages and Disadvantages of Procedural Programming

Advantages

• Procedural Programming is excellent for general-purpose programming

• The coded simplicity along with ease of implementation of compilers and interpreters

• A large variety of books and online course material available on tested algorithms,
making it easier to learn along the way

• The source code is portable, therefore, it can be used to target a different CPU as well

• The code can be reused in different parts of the program, without the need to copy it

• Through Procedural Programming technique, the memory requirement also slashes

• The program flow can be tracked easily


Disadvantages

• The program code is harder to write when Procedural Programming is employed

• The Procedural code is often not reusable, which may pose the need to recreate the
code if is needed to use in another application

• Difficult to relate with real-world objects

• The importance is given to the operation rather than the data, which might pose issues
in some data-sensitive cases

• The data is exposed to the whole program, making it not so much security friendly

IV. What are Advantage of OOPs over Procedure-oriented programming language?

1)OOPs makes development and maintenance easier where as in Procedure-oriented


programming language it is not easy to manage if code grows as project size grows.

2)OOPs provides data hiding whereas in Procedure-oriented programming language a global


data can be accessed from anywhere.

3)OOPs provides ability to simulate real-world event much more effectively. We can provide
the solution of real word problem if we are using the Object-Oriented Programming
language.

V. Advantages and Disadvantages of OOPs

Advantages

• Due to modularity and encapsulation, OOP offers ease of management

• OOP mimics the real world, making it easier to understand

• Since objects are whole within themselves, they are reusable in other programs

Disadvantages

• Object-Oriented programs tend to be slower and use up a high amount of memory

• Over-generalization

• Programs built using this paradigm may take longer to be created


VI. Comparision of POP and OOP

OOP POP

OOP takes a bottom-up approach in designing


POP follows a top-down approach.
a program.

Program is divided into objects depending on Program is divided into small chunks based on
the problem. the functions.

Each object controls its own data. Each function contains different data.

Focuses on security of the data irrespective of Follows a systematic approach to solve the
the algorithm. problem.

The main priority is data rather than functions Functions are more important than data in a
in a program. program.

Different parts of a program are interconnected


The functions of the objects are linked via
via
message passing.
parameter passing.

Data hiding is possible in OOP. No easy way for data hiding.

Inheritance is allowed in OOP. No such concept of inheritance in POP.

Operator overloading is allowed. Operator overloading is not allowed.

C++, Java. Pascal, Fortran.

VII. Concepts of OOPS

Object-oriented programming – As the name suggests uses objects in programming. Object-


oriented programming aims to implement real-world entities like inheritance, hiding,
polymorphism, etc in programming. The main aim 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.

1. Class

2. Objects

3. Encapsulation

4. Abstraction
5. Polymorphism

6. Dynamic Binding

7. Message Passing

8. Inheritance

VIII. Characteristics of an Object Oriented Programming language

1. Class: The building block of C++ that leads to Object-Oriented programming is a Class. It
is a user-defined data type, which holds its own data members and member functions, which
can be accessed and used by creating an instance of that class. A class is like a blueprint for
an object.

For Example: Consider the Class of Cars. There may be many cars with different names and
brand but all of them will share some common properties like all of them will have 4 wheels,
Speed Limit, Mileage range etc. So here, Car is the class and wheels, speed limits, mileage
are their properties.

 A Class is a user-defined data-type which has data members and member functions.

 Data members are the data variables and member functions are the functions used to
manipulate these variables and together these data members and member functions
define the properties and behaviour of the objects in a Class.

 In the above example of class Car, the data member will be speed limit, mileage etc
and member functions can apply brakes, increase speed etc.

We can say that a Class in C++ is a blue-print representing a group of objects which shares
some common properties and behaviours.
2. Object: An Object is an identifiable entity with some characteristics and behaviour. An
Object is an instance of a Class. When a class is defined, no memory is allocated but when it
is instantiated (i.e. an object is created) memory is allocated.

Example of Class and Object

class person

char name[20];

int id;

public:

void getdetails(){}

};

int main()

person p1; // p1 is a object

Object take up space in memory and have an associated address like a record in pascal or
structure or union in C. When a program is executed the objects interact by sending messages
to one another. Each object contains data and code to manipulate the data. Objects can
interact without having to know details of each other’s data or code, it is sufficient to know
the type of message accepted and type of response returned by the objects.
3. Encapsulation: In normal terms, Encapsulation is defined as wrapping up of data and
information under a single unit. In Object-Oriented Programming, Encapsulation is defined
as binding together the data and the functions that manipulate them.

Consider a real-life example of encapsulation, in a company, there are different sections like
the accounts section, finance section, sales section etc. The finance section handles all the
financial transactions and keeps records of all the data related to finance. Similarly, the sales
section handles all the sales-related activities and keeps records of all the sales. Now there
may arise a situation when for some reason an official from the finance section needs all the
data about sales in a particular month. In this case, he is not allowed to directly access the
data of the sales section. He will first have to contact some other officer in the sales section
and then request him to give the particular data. This is what encapsulation is. Here the data
of the sales section and the employees that can manipulate them are wrapped under a single
name “sales section”.

Example: Encapsulation in C++

#include<iostream>

using namespace std;

class Encapsulation

private: // data hidden from outside world

int x;

public:

void set(int a) // function to set value of // variable x

{
x =a;

// function to return value of

// variable x

int get()

return x;

};

// main function

int main()

Encapsulation obj;

obj.set(5);

cout<<obj.get();

return 0;

In the above program the variable x is made private. This variable can be accessed and
manipulated only using the functions get() and set() which are present inside the class. Thus
we can say that here, the variable x and the functions get() and set() are binded together
which is nothing but encapsulation.
4. Abstraction: Data abstraction is one of the most essential and important features of object-
oriented programming in C++. Abstraction means displaying only essential information and
hiding the details. Data abstraction refers to providing only essential information about the
data to the outside world, hiding the background details or implementation.

Consider a real-life example of a man driving a car. The man only knows that pressing the
accelerators will increase the speed of the car or applying brakes will stop the car but he does
not know about how on pressing accelerator the speed is actually increasing, he does not
know about the inner mechanism of the car or the implementation of accelerator, brakes etc
in the car.

Example:

using namespace std;

class implementAbstraction

private:

int a, b;

public:

// method to set values of

// private members

void set(int x, int y)

a = x;

b = y;

void display()

cout<<"a = " <<a << endl;


cout<<"b = " << b << endl;

};

int main()

implementAbstraction obj;

obj.set(10, 20);

obj.display();

return 0;

You can see in the above program we are not allowed to access the variables a and b directly,
however one can call the function set() to set the values in a and b and the function display()
to display the values of a and b.
5. Polymorphism: The word polymorphism means having many forms. In simple words, we
can define polymorphism as the ability of a message to be displayed in more than one form.

A person at the same time can have different characteristic. Like a man at the same time is a
father, a husband, an employee. So the same person posses different behaviour in different
situations. This is called polymorphism.

An operation may exhibit different behaviours in different instances. The behaviour depends
upon the types of data used in the operation.

Follow the link: https://www.geeksforgeeks.org/polymorphism-in-c/

C++ supports operator overloading and function overloading.

 Operator Overloading: The process of making an operator to exhibit different


behaviours in different instances is known as operator overloading. For example '+'
operator can be overloaded to perform addition on various data types, like for Integer,
String(concatenation) etc.

 Function Overloading: Function overloading is using a single function name to


perform different types of tasks. Function Overloading is defined as the process of
having two or more function with the same name, but different in parameters is
known as function overloading in C++.
Program of function overloading when number of arguments vary.

1. #include <iostream>

2. using namespace std;

3. class Cal {

4. public:

5. static int add(int a,int b){

6. return a + b;

7. }

8. static int add(int a, int b, int c)

9. {
10. return a + b + c;

11. }

12.};

13.int main(void) {

14. Cal C; // class object declaration.

15. cout<<C.add(10, 20)<<endl;

16. cout<<C.add(12, 20, 23);

17. return 0;

18.}

Example: Suppose we have to write a function to add some integers, some times there are 2
integers, some times there are 3 integers. We can write the Addition Method with the same
name having different parameters, the concerned method will be called according to
parameters.

• Function Overloading

Functions having the same name but different parameters is allowed in C++ and is called
Function Overloading. It is also called compile-time Polymorphism.

For example:

1 sum( int a, float b)

2 sum(int a, int b)

3 sum(int a, int b, int c)


Here, there are three functions with the same name but the only thing that differentiates them
is that the parameters are different on each. So, depending on the parameters passed, a
function is called.

• Function Overriding

When a derived class has a function with the same name as a function of the base class, it is
called Function Overriding. Both functions must have the same parameters in both classes.

6. Dynamic Binding: In dynamic binding, the code to be executed in response to function


call is decided at runtime. C++ has virtual functions to support this.

7. Message Passing: Objects communicate with one another by sending and receiving
information to each other. A message for an object is a request for execution of a procedure
and therefore will invoke a function in the receiving object that generates the desired results.
Message passing involves specifying the name of the object, the name of the function and the
information to be sent.
8. Inheritance: The capability of a class to derive properties and characteristics from another
class is called Inheritance. Inheritance is one of the most important features of Object-
Oriented Programming.

 Sub Class: The class that inherits properties from another class is called Sub class or
Derived Class.

 Super Class:The class whose properties are inherited by sub class is called Base
Class or Super class.

 Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to


create a new class and there is already a class that includes some of the code that we
want, we can derive our new class from the existing class. By doing this, we are
reusing the fields and methods of the existing class.

Example: Dog, Cat, Cow can be Derived Class of Animal Base Class.

Implementing inheritance in C++: For creating a sub-class which is inherited from the base
class we have to follow the below syntax.
Syntax:

class subclass_name : access_mode base_class_name

//body of subclass

};

C++ program to demonstrate implementation of Inheritance

using namespace std;

class Parent //Base class

public:

int id_p;

};
class Child : public Parent // Sub class inheriting from Base Class(Parent)

public:

int id_c;

};

//main function

int main()

Child obj1;

// An object of class child has all data members

// and member functions of class parent

obj1.id_c = 7;

obj1.id_p = 91;

cout << "Child id is " << obj1.id_c << endl;

cout << "Parent id is " << obj1.id_p << endl;

return 0;

In the above program the ‘Child’ class is publicly inherited from the ‘Parent’ class so the
public data members of the class ‘Parent’ will also be inherited by the class ‘Child’.

To Undertand Inheritance Program we need to Study Access Modifiers


• Access Modifiers or Access Specifiers in a class are used to set the accessibility of
the class members. That is, it sets some restrictions on the class members not to get
directly accessed by the outside functions.

1. Public: All the class members declared under public will be available to everyone. The
data members and member functions declared public can be accessed by other classes too.
The public members of a class can be accessed from anywhere in the program using the
direct member access operator (.) with the object of that class.

Example
using namespace std;
class Circle // class definition

public:

double radius;

double compute_area()

return 3.14*radius*radius;

} };

int main()

Circle obj;

// accessing public datamember outside class

obj.radius = 5.5;

cout << "Radius is: " << obj.radius << "\n";

cout << "Area is: " << obj.compute_area();

return 0; }

Output: Radius is: 5.5


2. Private: The class members declared as private can be accessed only by the functions
inside the class. They are not allowed to be accessed directly by any object or function
outside the class. Only the member functions or the friend functions are allowed to access the
private data members of a class.

Example

using namespace std;

class Circle

// private data member

private:

double radius;

// public member function

public:

double compute_area()

{ // member function can access private

// data member radius

return 3.14*radius*radius;

};

// main function

int main()

// creating object of the class

Circle obj;

// trying to access private data member


// directly outside the class

obj.radius = 1.5;

cout << "Area is:" << obj.compute_area();

return 0;

Output: With Error Due to Private Condition

In function 'int main()':

11:16: error: 'double Circle::radius' is private

double radius;

31:9: error: within this context

obj.radius = 1.5;

Corrected Program of Private Access Modifier

using namespace std;

class Circle

// private data member

private:

double radius;

// public member function

public:

void compute_area(double r)
{ // member function can access private

// data member radius

radius = r;

double area = 3.14*radius*radius;

cout << "Radius is: " << radius << endl;

cout << "Area is: " << area;

};

// main function

int main()

// creating object of the class

Circle obj;

// trying to access private data member

// directly outside the class

obj.compute_area(1.5);

return 0;

}
3. Protected: Protected access modifier is similar to that of private access modifiers, the
difference is that the class member declared as Protected are inaccessible outside the class but
they can be accessed by any subclass(derived class) of that class.

Examples

using namespace std;

// base class

class Parent

// protected data members

protected:

int id_protected;

};

// sub class or derived class

class Child : public Parent

public:

void setId(int id)

// Child class is able to access the inherited

// protected data members of base class

id_protected = id;

void displayId()

cout << "id_protected is: " << id_protected << endl;


}

};

// main function

int main()

Child obj1;

// member function of the derived class can

// access the protected data members of the base class

obj1.setId(81);

obj1.displayId();

return 0;

You might also like