0% found this document useful (0 votes)
3 views6 pages

Untitled Document

The document provides an overview of Object Oriented Programming (OOP) in C++, highlighting its advantages such as modularity, reusability, and data hiding. It explains fundamental concepts including classes, objects, data encapsulation, polymorphism, inheritance, and access specifiers, along with examples of function overloading and member function definitions. Additionally, it discusses the scope resolution operator and how to define classes and objects in C++.

Uploaded by

blackevil9103
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)
3 views6 pages

Untitled Document

The document provides an overview of Object Oriented Programming (OOP) in C++, highlighting its advantages such as modularity, reusability, and data hiding. It explains fundamental concepts including classes, objects, data encapsulation, polymorphism, inheritance, and access specifiers, along with examples of function overloading and member function definitions. Additionally, it discusses the scope resolution operator and how to define classes and objects in C++.

Uploaded by

blackevil9103
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/ 6

Unit I: Object Oriented Programming in C++

Advantages of OOPs
● Modularity: OOP allows the program to be divided into smaller parts
called objects, making it easier to manage.
● Reusability: Code can be reused through inheritance and polymorphism,
reducing redundancy.
● Data Hiding: OOP allows data to be hidden from the outside world using
access specifiers, providing better security.
● Maintenance: Easier to maintain and modify existing code as new objects
can be created with small differences to existing ones.
● Flexibility: Polymorphism allows for the flexibility of calling different
functions by the same name in different contexts.

Basic Elements of OOPs

Class

A class is a blueprint for creating objects. It defines a datatype by bundling data and
methods that work on the data into one single unit.

Object

An object is an instance of a class. When a class is defined, no memory is allocated until


an object of that class is created.

Data Hiding

Data hiding ensures that the details of the implementation are hidden from the user.
The user can only access the data through public methods.

Data Abstraction

Data abstraction refers to providing only essential information and hiding the
background details. It helps in reducing programming complexity and effort.

www.jkbosenotes.in
Data Encapsulation

Data encapsulation is the concept of wrapping data and the methods that operate on
the data into a single unit. It is the foundation of OOP principles.

Polymorphism

Polymorphism means the ability to take many forms. It allows objects to be treated as
instances of their parent class rather than their actual class.

Inheritance

Inheritance is a mechanism where a new class inherits the properties and behavior of
another class. It helps in code reusability and method overriding.

Implementation of Polymorphism using Function


Overloading
Function overloading is a feature that allows you to have more than one function with
the same name but different parameters. This helps in defining multiple behaviors for a
function based on its parameters.

#include <iostream>
using namespace std;

class Print {
public:
void display(int i) {
cout << "Displaying int: " << i << endl;
}

void display(double f) {
cout << "Displaying float: " << f << endl;
}

void display(char* c) {
cout << "Displaying character: " << c << endl;

www.jkbosenotes.in
}
};

int main() {
Print obj;
obj.display(5);
obj.display(5.5);
obj.display("Hello");
return 0;
}

Implementation of OOP in C++

Defining a Class

To define a class in C++, you use the class keyword followed by the class name and a
pair of curly braces. The data members and member functions are declared inside the
curly braces.

class MyClass {
public:
int myNumber;
void myFunction() {
cout << "Hello World!";
}
};

Members of a Class

Data Members

Data members are the variables that hold data specific to a class.

Member Functions

Member functions are the functions that operate on the data members of the class.

www.jkbosenotes.in
Defining an Object

To define an object of a class, you simply declare a variable of the class type.

MyClass obj;

Array of Objects

To create an array of objects, you declare an array with the class name.

MyClass objArray[10];

Access Specifiers
Access specifiers define the scope of the data members and member functions. The
three types of access specifiers in C++ are:

● Public: Members are accessible from outside the class.


● Private: Members cannot be accessed (or viewed) from outside the class.
● Protected: Members cannot be accessed from outside the class, but they
can be accessed in inherited classes.

Concept of Scope Resolution Operator


The scope resolution operator :: is used to define the function outside the class and to
access a global variable when there is a local variable with the same name.

#include <iostream>
using namespace std;

int x = 10; // global variable

www.jkbosenotes.in
class Test {
public:
int x = 20; // local variable
void display() {
cout << "Global x = " << ::x << endl;
cout << "Local x = " << x << endl;
}
};

int main() {
Test obj;
obj.display();
return 0;
}

Member Function Definition

Inside a Class

When a member function is defined inside the class, it is defined directly within the
class definition.

class MyClass {
public:
void display() {
cout << "Display function called!" << endl;
}
};

Outside a Class

When a member function is defined outside the class, you need to use the scope
resolution operator ::.

www.jkbosenotes.in
class MyClass {
public:
void display();
};

void MyClass::display() {
cout << "Display function called!" << endl;
}

www.jkbosenotes.in

You might also like