Fundamentals of oops Flashcards
difference between pop and oops
pop has top down approach and oops has bottom up approach
pop focuses on functions and oops on data
pop the code is divided into modules and data can be accessed within this module if not it is declared globally and is prone to changes
in oops data is secured as data and functions are combined into a single unit called as class where the data can be accessed by its associated method it makes the data secure
Procedure-Oriented Programming (POP) and Object-Oriented Programming (OOP) based on the points you mentioned:
- Approach:
POP:
Follows a top-down approach, starting with the main function and breaking down tasks into smaller sub-functions.
OOP:
Uses a bottom-up approach, focusing on creating reusable objects and classes before building the overall program. - Focus:
POP:
Primarily focuses on functions (procedures). The main goal is to create a sequence of tasks that the program performs.
OOP:
Emphasizes data and objects. It focuses on modeling real-world entities using classes and objects. - Data Handling:
POP:
Data is often shared among functions. If data needs to be accessed globally, it is declared as a global variable, making it prone to changes and less secure.
OOP:
Data is encapsulated within objects, and access is controlled via methods. This promotes data security and reduces the chances of unintended modifications. - Modularity:
POP:
Code is organized into modules (functions). However, these modules are not self-contained, as they may rely on global data.
OOP:
Code is structured into classes, which are self-contained units with data (attributes) and methods (functions). This increases modularity and code reusability. - Security:
POP:
Less secure because data is exposed to the entire program.
OOP:
More secure due to data encapsulation. Data is hidden inside classes and only accessible through specific methods (getters and setters).
Characteristics of OOPS
Classes
Objects
Encapsulation
Abstraction
Inheritance
Data hiding
Polymorphism
What are classes and objects?
Implementation of abstract datatype
Classes are user defined datatype
it consist of dm and mf
implements: encapsulation, abstraction and data hiding
it is a blueprint of objects
we can reuse a class in any project
Object is instance of a class
it is variable of class datatype
it has a unique: name, state and behaviour
it has getters and setters
What are modifiers and selectors?
Modifiers/ Setters are methods that changes the state of an objects
Selectors/Getters access the state of an object but does not make any changes
What is encapsulation?
Primary purpose: ignores complexities, focuses on “what”it does rather than “how” it does!”
Encapsulation is combining the data and the function together it supports data security.
The data members can be accessed by the associated member functions only
Encapsulation helps in protecting the internal state of the object from unauthorized access and modification.
Data Hiding: using access modifiers (like private in Java). This ensures that data cannot be accessed directly.
Getter and Setter Methods: we control how the data is accessed or modified.
Encapsulation allows us to focus on what something does without considering the complexities of how it works.
Data Abstraction
Abstraction:
primary purpose :ignores irrelevant information
allows us to consider complex ideas while ignoring irrelevant detail that would confuse us
Only essential features are represented, hides unnecessary information and ADT is implementation of Data abstraction
Inheritance
it is the concept of oops that allows one class to inherit the properties and methods of another class
this enables code reusability and creation of specialized classes based on a general class.
use of inheritance :
code reusability
maintenance
polymorphism
Single Inheritance: One child class inherits from one parent class.
Multilevel Inheritance: A class inherits from a child class, forming a chain (e.g., A → B → C).
Hierarchical Inheritance: Multiple classes inherit from a single parent class.
Multiple Inheritance (Supported in C++): A child class inherits from more than one parent class. In Java, this is achieved using interfaces.
Polymorphysm
a way to perform a single action in different ways.
polymorphism lets objects behave differently even when using the same method name.
Types of Polymorphism
Compile-time polymorphism (Method Overloading):#have different parameter..
Achieved using method overloading.
Multiple methods have the same name but different parameters (number, type, or order).
Decided at compile time.
Runtime polymorphism (Method Overriding):#providing specific implementation of base class.
Achieved using method overriding.
A child class provides a specific implementation of a method that is already defined in its parent class.
Decided at runtime.
Data Hiding
Using access modifers : private public and protected we expose only what is necessary
Model Real world object
include <iostream></iostream>
#include <string></string>
using namespace std;
class Employee {
private:
string name;
int id;
double salary;
public:
// Constructor
Employee(string empName, int empId, double empSalary) {
name = empName;
id = empId;
salary = empSalary;
}
// Getter functions string getName() { return name; } int getId() { return id; } double getSalary() { return salary; } // Setter functions void setName(string empName) { name = empName; } void setId(int empId) { id = empId; } void setSalary(double empSalary) { salary = empSalary; } // Function to display employee details void displayEmployeeDetails() { cout << "Employee Name: " << name << endl; cout << "Employee ID: " << id << endl; cout << "Employee Salary: $" << salary << endl; } };
int main() {
// Create an Employee object
Employee emp1(“John Doe”, 101, 50000);
// Display employee details emp1.displayEmployeeDetails(); // Modify employee details using setter functions emp1.setName("Jane Smith"); emp1.setSalary(55000); cout << "\nUpdated Employee Details:\n"; emp1.displayEmployeeDetails(); return 0; }
types of objects
external obj usfing extern keyword
local obj : in same block
static obj: retains previous value for same call
global obj: used within the same file
Dynamic obj: defined at runtime
Kinds of relationship
association, aggregation, generalisation, specialisation, composition
Association
Definition: # relation independent classes
indicate the relationship between two independent classes; the relationship may be one-to-one, one-to-many, or many-to-many, but it need not indicate ownership.
Association represents a relationship between two objects where both objects can exist independently, and each object can interact with the other.
Example: A Student class and a Course class. A student can enroll in a course, but both objects can exist without the other.
Types of Association:
One-to-One: One instance of a class is associated with exactly one instance of another class (e.g., Person and Passport).
One-to-Many: One instance of a class is associated with multiple instances of another class (e.g., Teacher and Students).
Many-to-Many: Multiple instances of a class are associated with multiple instances of another class (e.g., Student and Courses)
Aggregation
Aggregation: #one obj is part of other and both can exist independently
Definition: Aggregation is a special form of association where one object is a “whole” and the other is a “part”, but the part can exist independently of the whole.
Example: A University class can have multiple Department objects. If the university closes, the departments still exist as individual entities.
Characteristics: It represents a “Has-A” relationship. The “whole” object can exist without the “part”, but the part can exist without the whole.
Specialisation
Specialization:
Definition: Specialization is the reverse of generalization. It is the process of creating a more specialized subclass from a general superclass. A subclass is more specific and can have additional features or methods than the superclass.
Example: A Car class can specialize a Vehicle class, where Car adds specific features like numberOfDoors or engineType.