Escrito por estudiantes que aprobaron Inmediatamente disponible después del pago Leer en línea o como PDF ¿Documento equivocado? Cámbialo gratis 4,6 TrustPilot
logo-home
Resumen

Summary - C++

Puntuación
-
Vendido
-
Páginas
7
Subido en
30-03-2025
Escrito en
2024/2025

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

Institución
Grado

Vista previa del contenido

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.

Escuela, estudio y materia

Grado

Información del documento

Subido en
30 de marzo de 2025
Número de páginas
7
Escrito en
2024/2025
Tipo
RESUMEN

Temas

$10.19
Accede al documento completo:

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Conoce al vendedor
Seller avatar
ssurendharveppankulam

Conoce al vendedor

Seller avatar
ssurendharveppankulam
Seguir Necesitas iniciar sesión para seguir a otros usuarios o asignaturas
Vendido
-
Miembro desde
1 año
Número de seguidores
0
Documentos
1
Última venta
-

0.0

0 reseñas

5
0
4
0
3
0
2
0
1
0

Documentos populares

Recientemente visto por ti

Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes