CIR 204 Notes
CIR 204 Notes
Before diving into code, thoroughly grasp your design document or concept. This could involve:
Identify the program's inputs, outputs, functionalities, and data structures needed.
2. Breakdown into Manageable Parts:
Once you understand the overall design, decompose it into smaller, more manageable components. This
might involve:
✓ Start with Setup: Use #include statements to incorporate necessary header files like iostream for
input/output.
✓ Define the Entry Point: Every C++ program has a main function, which is the starting point. This
function defines the program's overall logic and execution flow.
✓ Declare Variables: Use appropriate data types (e.g., in for integers, double for decimals) to
declare variables that will store data.
✓ Control the Flow: Utilize control flow statements like if, else, for, while to control the program's
execution based on conditions and loops.
✓ Utilize Functions: Define functions to perform specific tasks, promoting code reusability. For
object-oriented designs, implement methods within classes.
C++
Use a C++ compiler (like g++) or an IDE (Integrated Development Environment) to translate your C++
code into an executable file.
There will likely be errors (bugs) in your initial code. Use a debugger to identify and fix them.
Write test cases with various input scenarios to ensure your program functions as intended under
different conditions. CLASS DEFINITION
class: It is a collection of data and member functions that manipulate data. The data components of class
are called data members and functions that manipulate the data are called member functions. It can
also known as blue print or prototype that defines the variables and functions common to all objects of
certain kind. It is also known as user defined data type or ADT(abstract data type) A class is declared by
the keyword class.
OBJECT CREATION
Object: An instance of a class. It holds its own set of data (attributes) and functionalities (methods).
Think of it as a real-world entity represented in code.
✓ Class Definition: You define a class using your programming language's syntax. This involves
specifying the attributes and methods the objects of that class will have.
✓ Object Instantiation: You create an object (instance) of the class using the new keyword (or
equivalent in the language) followed by the class name. This allocates memory for the object and
invokes the class's constructor.
✓ Constructor Execution: The constructor is called. It's responsible for initializing the object's
attributes with starting values, often using arguments passed during object creation.
✓ Object Usage: Once created, you can interact with the object by accessing and modifying its
attributes and calling its methods.
ATTRIBUTES
✓ Definition: Attributes are variables that define the characteristics or properties of an object. They
represent the data or state that an object holds.
✓ Location: Attributes are declared within a class definition, acting as a blueprint for all objects
created from that class.
✓ Data Types: Attributes can have various data types depending on the information they store
(e.g., integers for quantity, strings for names, booleans for true/false values, etc.).
METHOD DEFINITION
Methods are functions defined within a class. They represent the functionalities (behaviors) that objects
of that class can perform.
Each method has a specific purpose and can access and manipulate the object's attributes (data) to
achieve its task.
Methods encapsulate the "what" (behavior) of an object, while attributes represent the "what" (data).
✓ Access modifier (optional): Controls access to the method from other parts of your code. Public
methods are accessible from anywhere, private methods are only accessible within the class,
and protected methods are accessible within the class and subclasses.
✓ Return type: Specifies the data type of the value the method returns (can be void if it doesn't
return anything).
✓ Method name: A unique identifier for the method within the class, representing its functionality.
Choose descriptive names.
✓ Parameters (optional): A comma-separated list of variables used to pass data into the method
when it's called. These can influence the method's behavior.
✓ Method body: Contains the code defining the method's logic. This code accesses and
manipulates the object's attributes potentially using the provided parameters to achieve the
desired outcome.
INHERITANCE
Inheritance allows you to create new classes (subclasses) that inherit properties (attributes) and
functionalities (methods) from existing classes (superclasses). This promotes code reusability, reduces
redundancy, and fosters a hierarchical organization of classes.
Key Concepts:
✓ Superclass (Base Class, Parent Class): The original class whose attributes and methods are
inherited by subclasses.
✓ Subclass (Derived Class, Child Class): A new class created from a superclass, inheriting its
properties and potentially adding its own.
✓ Inheritance Relationship: The connection between a subclass and its superclass. A subclass "is-
akind-of" its superclass.
✓ Inheritance Hierarchy: A tree-like structure where classes inherit from each other, forming a
hierarchy with the most general class at the top.
Benefits of Inheritance:
✓ Code Reusability: Subclasses benefit from the functionality already defined in the superclass,
reducing code duplication.
✓ Maintainability: Changes made to the superclass propagate to subclasses, simplifying
maintenance.
✓ Polymorphism: Subclasses can override inherited methods, allowing for flexible behavior based
on the object's type. (We'll discuss this further)
✓ Hierarchical Relationships: Inheritance helps model real-world hierarchical structures (e.g.,
animals inheriting from mammals, mammals inheriting from vertebrates).
Types of Inheritance:
✓ Single Inheritance: A subclass inherits from only one superclass. This is the most common type.
✓ Multilevel Inheritance: A subclass inherits from another subclass, which in turn inherits from
another, forming a chain of inheritance.
✓ Multiple Inheritance: A subclass inherits from multiple superclasses (not as common due to
potential complexity).
✓ Hierarchical Inheritance: Multiple subclasses inherit from a single superclass, creating a
hierarchy.
✓ Hybrid Inheritance: A combination of the above types can be used in some languages, though
discouraged due to potential confusion.
OVERLOADING
Method Overloading: Multiple methods within a class can share the same name as long as they have
different parameter lists (number, types, or order of parameters). The appropriate method is called
based on the arguments provided during the method call. It promotes code reusability and can improve
readability.
Operator Overloading: In some languages, you can redefine the behavior of standard operators (e.g., +, ,
*, /) for custom classes. This allows you to perform operations on objects in a more intuitive way.
Points to Consider:
OVERRIDING
In OOP, overriding refers to the ability of a subclass (derived class) to redefine the behavior of a method
inherited from its parent class (base class). This allows for specialization and customization of behavior
within a class hierarchy.
✓ Inheritance: Overriding is only possible when using inheritance, where a subclass inherits
properties and methods from its parent class.
✓ Method Redefinition: The subclass redefines the inherited method with its own implementation,
providing a specific behavior for that subclass.
✓ Method Signature: The overriding method must have the same name, number of arguments
(parameters), and return type as the method in the parent class. However, the access modifier
(public, private, protected) can be different.