Object Oriented Programming
Chapter 1: Object Oriented Programming
Introduction
C++ Development Background
C++ is developed from C and influenced by Simula.
C programming follows a monolithic (without functions) approach and a modular (with functions) approach.
C++ continues this trend but adds object-oriented features.
Object Orientation Definition
Core Idea: Combination of attributes (data members or variables) and behaviors (functions) into objects.
Real-world objects categorized into classes; thus, object-oriented programming mimics real-world scenarios.
Naming History: Initially referred to as "C with Classes", the name changed to C++ in 1983. The notation "++" refers to the increment operator, indicating that C++ is a more advanced version of C.
Key Concepts:
Each object has:
Identity: Name of the object (e.g., Aayan).
Attributes: Characteristics (height, weight, age, date-of-birth).
Behaviors: Functions (calculateheight, getage, showdateof-birth).
State: Current condition or value (age changes).
Notations:
Data members are associated with a class and are termed as such, while member functions describe the functions operated by class instances.
Characteristics of Object-Oriented Programming (OOP)
Common features of object-oriented programming languages with slight variations:
Class and Object
Data Encapsulation
Data Abstraction
Inheritance
Polymorphism
Dynamic Binding
Operator Overloading & Function Overloading
Class and Object
Definition: Each object represents properties (data) and behaviors (functions).
Objects with similar properties and behaviors are grouped into classes.
Abstract Concepts: Classes have no physical existence, akin to concepts such as "human being", "teacher", or "student".
Exitence of classes:
What can be seen are the objects that exhibit behaviors and properties of the class, e.g., Aayan and Ravi are objects of the class "Human-being".
Analogy: Honda, Chevrolet, Audi are grouped under the class "Automobile_company".
Data Encapsulation
Definition: "Encapsulation is the process of hiding all the details of an object that do not contribute to its essential characteristics" - Grady Booch.
Key Points:
Mechanism to hide unwanted components, restricting access to certain features.
Combining data and methods into a single unit (class).
Access to wrapped data is provided through designated functions within the class, enforcing security and data integrity.
Data Abstraction
Definition: "An Abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects" - Grady Booch.
Function: Focused on revealing essential features while hiding implementation details.
Example: A cell phone user only interacts with the visible features, while the internal components are abstracted away.
Inheritance
Definition: Process through which one class inherits properties and behaviors from another class (Base class to Derived class).
Main Advantage: Code reusability.
Example: Creating classes "Two wheeler" and "Four wheeler" that derive from the base class "Automobile" to avoid code redundancy.
Notations:
Base class: Contains shared properties/behaviors.
Derived class: Contains unique features alongside inherited ones.
Relationship: Base Class ⟶ Derived Class (inherits properties and behaviors).
Generalization & Specialization:
Generalization: Creating a class by isolating common properties (bottom-up approach).
Specialization: Creating a class with specialized features (top-down approach).
Polymorphism
Definition: Derived from Greek meaning "Many forms".
In OOP allows functions and operators to act in different ways in various contexts.
Types of Polymorphism:
Function overloading: Same function name, different parameters.
Operator overloading: Same operator works differently based on context (type).
Examples: The class Memory has multiple derived classes (Pendrive, CD, HardDisk) that exhibit polymorphism through the showType() function output differing based on the class being referenced.
Advantages and Disadvantages of Object Oriented Programming
Advantages
Real-world relevance aids problem-solving.
Supports extensive code reusability and eliminates redundancy through inheritance.
Reduces development time and enhances productivity.
Improved security: Data encapsulation restricts access to class data.
Easily expandable and maintainable programs.
Flexibility in operation through polymorphism.
Disadvantages
High resource demands may lead to increased processing time.
Less effective for small or simple programs.
Need for programmers to be well-versed with modern tools and techniques to implement OOP effectively.
Procedure Oriented Programming (POP) vs Object Oriented Programming (OOP)
Approach:
POP: Top-down approach.
OOP: Bottom-up approach.
Flexibility in Adding Functionality:
POP requires significant code restructuring to add new functionalities.
OOP allows easier additions without altering the program structure significantly.
Data Accessibility:
POP: Shared global data, unrestricted access.
OOP: Access controlled via private, public, and protected specifications.
Overloading Capabilities:
POP: No support for overloading functions.
OOP: Functions and operators can be overloaded for greater flexibility.
What We Learned
C++ is an object-oriented programming language characterized by:
Encapsulation: Grouping data and methods.
Abstraction: Presentation of relevant features of a class.
Inheritance: Deriving new classes from existing ones, promoting code reuse.
Polymorphism: Same function behaves differently in different contexts.
Multiple Choice Questions (MCQs)
Which was the first purely object-oriented programming language developed?
(a) Java
(b) C++
(c) SmallTalk
(d) Kotlin
Which of the following best defines a class?
(a) Parent of an object
(b) Instance of an object
(c) Blueprint of an object
(d) Scope of an object
Who invented OOP?
(a) Alan Kay
(b) Andrea Ferro
(c) Dennis Ritchie
(d) Adele Goldberg
What is the additional feature in classes that was not in structures?
(a) Data members
(b) Member functions
(c) Static data allowed
(d) Public access specifier
Which is not a feature of OOP?
(a) Code reusability
(b) Modularity
(c) Duplicate/Redundant data
(d) Efficient Code
Can pure OOP be implemented without classes?
(a) True
(b) False
Which feature of OOP illustrates code reusability?
(a) Polymorphism
(b) Abstraction
(c) Encapsulation
(d) Inheritance
How many classes can be defined in a single program?
(a) Only 1
(b) Only 100
(c) Only 999
(d) As many as you want
When did OOP concepts become popular?
(a) 1970's
(b) 1980's
(c) 1993
(d) 1995
Which concept of OOP is incorrect for C++?
(a) Code can be written without using classes
(b) Code must contain at least one class
(c) A class must have member functions
(d) At least one object should be declared.
Which header file is required in C++ for OOP?
(a) iostream.h
(b) stdio.h
(c) stdlib.h
(d) No header file needed.
Which of the two features match each other?
(a) Inheritance and Encapsulation
(b) Encapsulation and Polymorphism
(c) Encapsulation and Abstraction
(d) Abstraction and Polymorphism
Which feature allows open recursion?
(a) Use of this pointer
(b) Use of pointers
(c) Use of pass by value
(d) Use of parameterized constructor
Very Short Questions
Which programming language influenced C++?
What is the fundamental idea behind Object-Oriented Programming?
Who developed C++?
What are data members and member functions in C++?
What are class and object in C++?
What is data encapsulation?
What is inheritance in OOP?
Define polymorphism.
Define inheritance in terms of C++.
What are generalization and specialization?
Short Questions
Describe the basic idea behind C++ Programming language.
State and explain the characteristics of Object-Oriented Programming languages.
Justify "Encapsulation is to hide and Abstraction is to show".
Explain generalization and specialization.
State the advantages and disadvantages of Object-Oriented Programming.
Compare Procedure-Oriented Programming (POP) and Object-Oriented Programming (OOP).
Discuss advantages and disadvantages of inheritance.
Discuss advantages and disadvantages of Object-Oriented Programming.
Application Based Questions
What is monolithic programming? State its features.
Define an object-oriented programming language.
Outline the main features of object-oriented programming languages.
Discuss each feature of object-oriented programming in detail.
What is encapsulation?
What is abstraction?
Explain the statement: "Reusability in OOP is implemented through Inheritance".
Define polymorphism and explain.
Discuss various types of polymorphism.
Chapter 1: Object Oriented Programming
Introduction
C++ Development Background
C++ is developed from C and influenced by Simula.
C programming approaches can be categorized as:
Monolithic (without functions): This approach predominantly utilizes a single code block, making it less organized and harder to maintain.
Modular (with functions): Functions organize code into manageable blocks, enhancing readability and maintainability.
C++ extends these approaches by integrating object-oriented features, which allow for better organization and data management.
Object Orientation Definition
Core Idea: The fundamental notion is to combine attributes (data members or variables) and behaviors (functions) into objects.
Real-world objects are categorized into classes; thus, object-oriented programming mimics real-world scenarios, allowing developers to create relatable models.
Naming History: Initially referred to as "C with Classes", the name evolved to C++ in 1983, hinting at its advanced capabilities over C. The notation "++" denotes an increment, symbolizing the progression of programming capabilities.
Key Concepts:
Each object has:
Identity: Unique name of the object (e.g., Aayan).
Attributes: Specific characteristics (e.g., height, weight, age, date-of-birth).
Behaviors: Functions that perform operations (e.g., calculateheight, getage, showdateof_birth).
State: Represents the current condition or value of the object, which can change over time (e.g., age).
Notations:
Data members are associated with a class, thus termed as such. A member function describes operations performed on or by class instances, creating a clear structure in code organization.
Characteristics of Object-Oriented Programming (OOP)
Key features:
Class and Object: Fundamental building blocks of OOP; classes are blueprints for objects, encapsulating data and behavior.
Data Encapsulation: Ensures that data is hidden from unauthorized access and represented as a single unit (class).
Data Abstraction: Focuses on exposing only the essential aspects of an object, simplifying complexity.
Inheritance: Allows deriving new classes from existing ones, promoting code reusability and organization.
Polymorphism: Empowers functions and operators to behave differently based on the context, enhancing flexibility and functionality.
Dynamic Binding: Enables method resolution during runtime depending on the object being referred to.
Operator Overloading & Function Overloading: Enables defining multiple behaviors for the same operator or function name based on context, promoting more intuitive code.
Class and Object
Definition: Each object represents properties (data) and behaviors (functions), with objects exhibiting similar characteristics being grouped into classes.
Abstract Concepts: Classes themselves don’t have physical manifestations; they represent concepts such as “human being,” “teacher,” or “student.”
Existence of Classes: Actual objects such as Aayan and Ravi are instances of the class "Human-being".
Analogy: Comparing car manufacturers (e.g., Honda, Chevrolet, Audi) grouped under the class “Automobile_company”. This demonstrates how classes encapsulate similar entities and behaviors under a unified concept.
Data Encapsulation
Definition: "Encapsulation is the process of hiding all the details of an object that do not contribute to its essential characteristics” - Grady Booch.
Key Points:
Mechanism to restrict access to unnecessary features, protecting the integrity of the object data.
Combines data and methods into a cohesive unit (class) to ensure modular organization of code.
Access to wrapped data is facilitated through designated functions within the class, which serves to enhance security and data integrity while adhering to the concept of least privilege.
Data Abstraction
Definition: "An Abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects" - Grady Booch.
Function: Designed to highlight key features while concealing implementation specifics, simplifying interaction and focus on necessary attributes.
Example: A cell phone user interacts primarily with visible features (interface), while intricate internal components remain abstracted, allowing ease of use without overwhelming technical details.
Inheritance
Definition: The process where one class inherits properties and behaviors from another class (from Base class to Derived class), promoting structured hierarchy within code.
Main Advantage: Encourages code reusability, thus optimizing development efforts.
Example: Formulating classes “Two wheeler” and “Four wheeler” that inherit from a base class “Automobile”, which minimizes redundancy by allowing shared properties or methods to be defined only once at the base class level.
Notations:
Base class: Holds common attributes/behaviors (e.g., Vehicle).
Derived class: Inherits and can add unique features on top of inherited traits (e.g., Car, Motorcycle).
Relationship: Base Class ⟶ Derived Class (inherits properties and behaviors).
Generalization & Specialization:
Generalization: Developing a class by identifying shared properties, typically using a bottom-up approach to create a more abstract representation.
Specialization: Formulating a class to highlight specific features derived from a more general base class, employing a top-down approach to refine class definitions.
Polymorphism
Definition: Derived from Greek, meaning "Many forms", polymorphism allows for functions and operators to act differently depending on the context within programming.
Types of Polymorphism:
Function overloading: Same function name can be used with different parameter types or counts, enabling versatility.
Operator overloading: Same operator can carry out different functions based on the context or types of operands involved.
Examples: Consider a class Memory with derived classes (e.g., Pendrive, CD, HardDisk) each overriding a showType() function to display unique outputs based on the specific class invoked.
Advantages and Disadvantages of Object Oriented Programming
Advantages
Better alignment with real-world scenarios, enhancing problem-solving efficacy.
Significant support for code reusability via inheritance, decreasing duplication efforts.
Reduced development time and improved productivity through organized code structure.
Enhanced security through encapsulation, limiting unwanted access to class data.
Greater flexibility and easier maintainability of programs, promoting longer software life cycles.
The dynamic nature of polymorphism fosters more adaptable systems, allowing for changes without major disruptions.
Disadvantages
Potentially high resource demands can lead to performance bottlenecks, especially in resource-constrained environments.
Not as effective for simple or small-scale programs where the overhead of OOP may be unnecessary.
Requirement for programmers to be proficient with modern OOP principles and tools to leverage its full benefits effectively.
Procedure Oriented Programming (POP) vs Object Oriented Programming (OOP)
Approach:
POP: Employs a top-down approach, starting from the highest level of the program and breaking it down.
OOP: Utilizes a bottom-up approach, beginning with the creation of objects and gradually building up to more complex systems.
Flexibility in Adding Functionality:
POP typically necessitates extensive code restructuring to add new features, leading to higher maintenance costs.
OOP enables easier feature additions with minimal impact on the existing structure, facilitating smoother updates.
Data Accessibility:
POP operates with shared global data, compromising data integrity through unrestricted access.
OOP’s access control through private, public, and protected designations safeguards data while allowing necessary transparency.
Overloading Capabilities:
POP lacks support for function overloading, resulting in less flexible code designs.
OOP allows for both operator and function overloading, providing enhanced versatility in code expressions.
What We Learned
C++ is categorized as an object-oriented programming language identified by:
Encapsulation: Integrating data and related methods into singular units for better data security.
Abstraction: Highlighting necessary features while abstracting complex internals, enhancing user interaction.
Inheritance: Facilitating new class creation from existing classes, thus elevating code reuse.
Polymorphism: Allowing methods and operators to interact differently depending on the specific context employed.
Multiple Choice Questions (MCQs)
Which was the first purely object-oriented programming language developed?
(a) Java
(b) C++
(c) SmallTalk
(d) Kotlin
Which of the following best defines a class?
(a) Parent of an object
(b) Instance of an object
(c) Blueprint of an object
(d) Scope of an object
Who invented OOP?
(a) Alan Kay
(b) Andrea Ferro
(c) Dennis Ritchie
(d) Adele Goldberg
What is the additional feature in classes that was not in structures?
(a) Data members
(b) Member functions
(c) Static data allowed
(d) Public access specifier
Which is not a feature of OOP?
(a) Code reusability
(b) Modularity
(c) Duplicate/Redundant data
(d) Efficient Code
Can pure OOP be implemented without classes?
(a) True
(b) False
Which feature of OOP illustrates code reusability?
(a) Polymorphism
(b) Abstraction
(c) Encapsulation
(d) Inheritance
How many classes can be defined in a single program?
(a) Only 1
(b) Only 100
(c) Only 999
(d) As many as you want
When did OOP concepts become popular?
(a) 1970's
(b) 1980's
(c) 1993
(d) 1995
Which concept of OOP is incorrect for C++?
(a) Code can be written without using classes
(b) Code must contain at least one class
(c) A class must have member functions
(d) At least one object should be declared.
Which header file is required in C++ for OOP?
(a) iostream.h
(b) stdio.h
(c) stdlib.h
(d) No header file needed.
Which of the two features match each other?
(a) Inheritance and Encapsulation
(b) Encapsulation and Polymorphism
(c) Encapsulation and Abstraction
(d) Abstraction and Polymorphism
Which feature allows open recursion?
(a) Use of this pointer
(b) Use of pointers
(c) Use of pass by value
(d) Use of parameterized constructor
Very Short Questions
Which programming language influenced C++?
What is the fundamental idea behind Object-Oriented Programming?
Who developed C++?
What are data members and member functions in C++?
What are class and object in C++?
What is data encapsulation?
What is inheritance in OOP?
Define polymorphism.
Define inheritance in terms of C++.
What are generalization and specialization?
Short Questions
Describe the basic idea behind C++ Programming language.
State and explain the characteristics of Object-Oriented Programming languages.
Justify "Encapsulation is to hide and Abstraction is to show".
Explain generalization and specialization.
State the advantages and disadvantages of Object-Oriented Programming.
Compare Procedure-Oriented Programming (POP) and Object-Oriented Programming (OOP).
Discuss advantages and disadvantages of inheritance.
Discuss advantages and disadvantages of Object-Oriented Programming.
Application Based Questions
What is monolithic programming? State its features.
Define an object-oriented programming language.
Outline the main features of object-oriented programming languages.
Discuss each feature of object-oriented
Introduction - C++ Development Background - C++ is developed from C and influenced by Simula. - C programming follows two primary approaches: monolithic (without functions) and modular (with functions). The former utilizes a single code block, making it less organized and harder to maintain, while the latter organizes code into manageable blocks, enhancing readability and maintainability. C++ continues this trend, integrating object-oriented features for improved organization and data management.
Object Orientation Definition - Core Idea: The core notion is to combine data (attributes) and functions (behaviors) into objects. Real-world objects are categorized into classes; thus, object-oriented programming mimics real-world scenarios, enabling developers to create relatable models. - Naming History: Initially referred to as "C with Classes," it evolved to C++ in 1983, highlighting its advanced capabilities over C. The notation "++" denotes an increment, symbolizing the evolution of programming capabilities. - Key Concepts: Each object possesses: - Identity: A unique name (e.g., Aayan). - Attributes: Specific characteristics (e.g., height, weight, age). - Behaviors: Functions that perform operations (e.g., calculateheight, getage). - State: The current value of the object, which can change over time (e.g., age).
Notations: Data members associated with a class are termed as such. A member function describes operations performed on or by class instances, creating a structured organization of code.