Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary - C++

Rating
-
Sold
-
Pages
7
Uploaded on
30-03-2025
Written in
2024/2025

My document is clearly gives you a clearity to learn c++

Institution
Course

Content preview

c++


Object-Oriented Programming (OOP) Concepts


OOP is a programming paradigm that is based on the concept of "objects", which contain data and
methods that operate on that data. There are four main concepts in OOP: encapsulation,
inheritance, polymorphism, and abstraction.

Encapsulation

Encapsulation is the practice of keeping the data and the methods that operate on that data within
the same unit, or object. This helps to prevent external code from accidentally (or intentionally)
modifying the data in ways that could break the program.

For example, consider a BankAccount class that encapsulates the data and methods related to a
bank account. The class might look something like this:

class BankAccount:
def __init__(self, balance=0):
self.balance = balance # encapsulated data

def deposit(self, amount):
self.balance += amount # encapsulated method

def withdraw(self, amount):
if amount > self.balance:
raise ValueError("Insufficient funds")
self.balance -= amount # encapsulated method
In this example, the balance data is encapsulated within the BankAccount object, and can only be
modified through the deposit and withdraw methods. This helps to ensure that the balance is
always kept consistent and accurate.

Inheritance

Inheritance is the practice of creating a new class that is a modified version of an existing class.
The new class is called the "derived" class, and the existing class is the "base" class. The derived
class inherits all of the data and methods of the base class, and can modify or add to them as
needed.

For example, consider a SavingsAccount class that inherits from the BankAccount class:

class SavingsAccount(BankAccount):
def __init__(self, balance=0, interest_rate=0.01):
super().__init__(balance)
self.interest_rate = interest_rate # new data specific to SavingsAccount

def calculate_interest(self):
return self.balance * self.interest_rate # new method specific to SavingsAccount
In this example, the SavingsAccount class inherits the balance data and the deposit and withdraw
methods from the BankAccount class. It also adds a new interest_rate data field and a
calculate_interest method that are specific to SavingsAccount objects.

, Polymorphism

Polymorphism is the practice of using a single interface to represent multiple different types of
objects. This is often achieved through the use of abstract base classes, which define a set of
methods that must be implemented by any derived classes.

For example, consider an abstract base class Shape that defines a area method:

from abc import ABC, abstractmethod

class Shape(ABC):
@abstractmethod
def area(self):
pass

class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height

def area(self):
return self.width * self.height

class Circle(Shape):
def __init__(self, radius):
self.radius = radius

def area(self):
return 3.14 * self.radius**2
In this example, both the Rectangle and Circle classes inherit from the Shape base class and
implement the area method. This allows us to use a single area method to calculate the areas of
both rectangles and circles.

Abstraction

Abstraction is the practice of hiding the implementation details of an object and exposing only the
necessary interface. This helps to simplify the use of the object and reduce the cognitive load on
the programmer.

For example, consider a LightBulb class that abstracts away the details of how the light bulb
works:

class LightBulb:
def __init__(self, brightness=50):
self.brightness = brightness # encapsulated data

def turn_on(self):
# implementation details of turning on the light bulb are hidden
self.brightness = 100

def turn_off(self):
# implementation details of turning off the light bulb are hidden
self.brightness = 0
In this example, the LightBulb class exposes only the necessary interface (turn_on and turn_off
methods) and hides the implementation details of how the light bulb works. This makes it easy for
programmers to use the class without having to worry about the low-level details.


I hope this summary has been helpful! Let me know if you have any questions.

Written for

Course

Document information

Uploaded on
March 30, 2025
Number of pages
7
Written in
2024/2025
Type
SUMMARY

Subjects

$10.19
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
ssurendharveppankulam

Get to know the seller

Seller avatar
ssurendharveppankulam
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Trending documents

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions