Oops Concepts
Oops Concepts
PROGRAMMING
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING
AND C++
• Disadvantage :
Very little attention is given to the data that are being
used by various functions.
BASIC CONCEPTS OF OBJECT-ORIENTED
PROGRAMMING
Object creation
Dog Boxer=new Dog();
Data Encapsulation
• C++ provides the facility to encapsulate data and the relevant
functions together in the same object.
Example:
• Customer uses the swipe interface provided by the machine to swipe
the card.
• The swipe Machine encapsulates/hides the internal circulatory from
all the users and provides simple interface for access by every user.
Data Hiding
Related to the idea of encapsulation is the concept of data hiding.
Encapsulation hides the data from other classes and functions in other
classes.
• by the use of keywords private, protected, and public
class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
• Abstraction:
Data abstraction refers to, providing only essential
information to the outside world and hiding their
background details, i.e., to represent the needed information
in program without presenting the details.
Example:
Customers need to know only what to purchase and
the mode of payment.
• Inheritance:
One of the most useful aspects of object-oriented
programming is code reusability. As the name suggests
Inheritance is the process of forming a new class from an
existing class that is from the existing class called as base
class, new class is formed called as derived class.
• Polymorphism:
The ability to use an operator or function in different
ways in other words giving different meaning or functions to
the operators or functions is called polymorphism.
• Poly refers to many. That is a single function or an
operator functioning in many ways different upon the
usage is called polymorphism.
Overloading:
• The concept of overloading is also a branch of
polymorphism. When the exiting operator or function is
made to operate on new data type, it is said to be
overloaded.
• Example:Payment mode: cash or credit card.
#include <iostream>
using namespace std;
class Shape {
protected: int width, height;
public: Shape( int a=0, int b=0) {
width = a; height = b;
}
int area() {
cout << "Parent class area :" <<endl; return 0;
}
};
void start() {
cout << "Car started" << endl;
}
};
Object
An object is an instance of a class. It is used to access the members
(variables/functions) of the class.
int main() {
Car myCar; // Create object
myCar.brand = "Toyota";
myCar.year = 2022;
cout << myCar.brand << " " << myCar.year << endl;
myCar.start(); // Call method
return 0;
• }
Encapsulation
Encapsulation means hiding internal details and only exposing necessary parts
through public methods.
class Account {
private:
double balance;
public:
void setBalance(double b) {
if (b > 0)
balance = b;
}
double getBalance() {
return balance;
}
• };
Inheritance
Inheritance allows a class (child) to inherit properties and behaviors from another class (parent).
class Vehicle {
public:
void honk() {
cout << "Beep beep!" << endl;
}
};
Polymorphism means many forms—you can use the same function name but different
behaviors depending on the object
Compile-time (Function Overloading)
class Print {
public:
void show(int i) {
cout << "Integer: " << i << endl;
}
void show(string s) {
cout << "String: " << s << endl;
}
• };
Concept Description
Class Blueprint for objects
Object Instance of a class
Encapsulation Hiding data using access modifiers
Abstraction Hiding complexity, showing only features
Inheritance Acquiring properties from another class
Polymorphism One interface, multiple behaviors