Untitled Document
Untitled Document
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.
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
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.
#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;
}
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:
#include <iostream>
using namespace std;
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;
}
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