0% found this document useful (0 votes)
14 views5 pages

Oop 023124

The document explains key concepts of Object-Oriented Programming, including encapsulation, inheritance, and reusability. Encapsulation involves wrapping data and methods in a class while restricting access to protect internal states, demonstrated through C++ examples. Inheritance allows a child class to inherit properties from a parent class, promoting code reuse and efficiency, also illustrated with C++ code snippets.

Uploaded by

ajinm006
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)
14 views5 pages

Oop 023124

The document explains key concepts of Object-Oriented Programming, including encapsulation, inheritance, and reusability. Encapsulation involves wrapping data and methods in a class while restricting access to protect internal states, demonstrated through C++ examples. Inheritance allows a child class to inherit properties from a parent class, promoting code reuse and efficiency, also illustrated with C++ code snippets.

Uploaded by

ajinm006
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/ 5

What is Encapsulation?

Encapsulation is one of the core principles of Object-Oriented Programming. It means wrapping


data (variables) and the methods (functions) that operate on the data into a single unit (class)
and restricting direct access to some of the object's components. This helps protect the internal state
of the object.

Access Labels & Data Hiding in C++


In C++, access labels (private, public, and protected) control how class members
(variables and functions) can be accessed. This directly enables data hiding, a key concept of
encapsulation.

Access Labels:
Access Label Accessible From... Purpose
private Only inside the class Used to hide data from outside
public Anywhere (inside or outside the class) Used to expose safe operations
protected Inside class and derived (child) classes Mainly for inheritance purposes

C++ Program Demonstrating Encapsulation


Here’s a simple C++ program that demonstrates encapsulation using a BankAccount class:

#include <iostream>
using namespace std;

// Encapsulated class
class BankAccount {
private:
// Private data members (cannot be accessed directly from outside)
string accountHolder;
double balance;

public:
// Public methods to access and modify private data

void setAccountHolder(string name) {


accountHolder = name;
}

void deposit(double amount) {


if (amount > 0)
balance += amount;
}

void withdraw(double amount) {


if (amount > 0 && amount <= balance)
balance -= amount;
else
cout << "Insufficient balance or invalid amount!" << endl;
}
void displayAccountInfo() {
cout << "Account Holder: " << accountHolder << endl;
cout << "Balance: $" << balance << endl;
}
};

int main()

{
BankAccount account;
account.setAccountHolder("Alice");
account.deposit(1000);
account.withdraw(250);
account.displayAccountInfo();

// The following line will give an error because 'balance' is private:


// account.balance = 5000;

return 0;
}

Key Points:
• accountHolder and balance are private: they cannot be accessed directly from
main().

• All access to them is done using public methods like deposit(), withdraw(), and
displayAccountInfo().

Simple Encapsulation Example in C++

#include <iostream>
using namespace std;

// Encapsulated class
class Student
{
private:
string name;
int age;

public:
void setData(string studentName, int studentAge)
{
name = studentName;
age = studentAge;
}

void displayData()
{
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};

int main()
{
Student s1;
s1.setData("John", 15);
s1.displayData();
return 0;
}

What is Inheritance?
Inheritance means creating a new class (child class) from an existing class (parent class).
The child class inherits the properties and behaviors (variables and functions) of the parent class.
🔹 Why use it?
To reuse code, avoid repetition, and show "is-a" relationships.

Simple C++ Program Demonstrating Inheritance

#include <iostream>
using namespace std;

// Base class (parent)


class Animal
{
public:
void eat()
{
cout << "This animal eats food." << endl;
}
};

// Derived class (child)


class Dog : public Animal
{
public:
void bark()
{
cout << "The dog barks." << endl;
}
};

int main()
{
Dog d;
d.eat(); // Inherited from Animal
d.bark(); // Defined in Dog

return 0;
}

🔍 Explanation:
• Animal is the base class with a function eat().

• Dog is a derived class that inherits from Animal using : public Animal.

• Dog gets access to the eat() function of Animal, and also defines its own bark()
function.
What is Reusability?
Reusability means using existing code again instead of writing it from scratch. This:
• Saves time
• Reduces bugs
• Makes code easier to maintain

How Inheritance Supports Reusability


Inheritance allows a new class (child) to reuse the properties and behavior of an existing class
(parent).

✅ Key Ideas:
• Common features (data members and methods) are written once in the base (parent) class.
• These features are inherited by derived (child) classes automatically.
• The child class can also extend or override the behavior — so we reuse and customize at
the same time.

Example:
Suppose we have a base class Vehicle:

class Vehicle
{
public:
void start()
{
cout << "Vehicle started" << endl;
}
};

Now we create a Car class that inherits from Vehicle:

class Car : public Vehicle


{
public:
void drive()
{
cout << "Car is driving" << endl;
}
};

In main():
Car c;
c.start(); // Reused from Vehicle
c.drive(); // Defined in Car

Inheritance promotes reusability by allowing a new class to use existing functionality from a base
class, avoiding duplication and enabling consistent, clean, and efficient code design.
Function overloading
#include<iostream>
using namespace std;

void area(int r)
{
float Area=3.14*r*r;
cout<<"Area of circle="<<Area;
}
void area(int l,int b)
{
int Area=l*b;
cout<<"Area of rectangle="<<Area;
}
int main()
{
int radius,length,breadth;
cout<<"enter radius";
cin>>radius;
area(radius);
cout<<endl;
cout<<"enter length and breadth ";
cin>>length>>breadth;
area(length,breadth);
return 0;

You might also like