0% found this document useful (0 votes)
11 views6 pages

CIR 204 Notes

The document discusses object-oriented programming concepts like classes, objects, attributes, methods, inheritance, polymorphism, overloading and overriding in C++. It provides examples and explanations of how to define classes and objects and use various OOP principles when building programs.

Uploaded by

odumbedavis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

CIR 204 Notes

The document discusses object-oriented programming concepts like classes, objects, attributes, methods, inheritance, polymorphism, overloading and overriding in C++. It provides examples and explanations of how to define classes and objects and use various OOP principles when building programs.

Uploaded by

odumbedavis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

GROUP 8

CATHERINE MORAA- CCS/00060/022


KEVIN DAVIS- CCS/00118/022
MACKENZY MAXWELL- CCS/00004/022 DENLISON
KIRERA- CCS/00061/022
VOLKA BARASA-CCS/00066/022

INTRODUCTION TO OBJECTS AND CLASSES


DEMONSTRATION ON HOW TO TRANSLATE THE DESIGN INTO AN EXECUTABLE PROGRAM USING C++

1. Understanding the Design:

Before diving into code, thoroughly grasp your design document or concept. This could involve:

✓ Flowcharts outlining program flow.


✓ User stories detailing functionalities.
✓ Class diagrams for object-oriented designs.

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:

✓ Defining functions that perform specific tasks.


✓ Creating classes that represent real-world entities with attributes and behaviors. ✓ Planning the
interactions between different program components.

3. Translating to C++ Code:

Here's the coding phase in a nutshell:

✓ 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.

4. Example: (Calculating Area of a Shape)

This simplified example demonstrates translating a basic design into code:

C++

#include <iostream> double calculate Area (double

length, double width) {

// Function to calculate area (replace with your specific formula)

return length * width;}

in main () { double length,

width, area; // Get input from

the user stud: coot << "Enter

length: "; stud: can >> length;

stud: coot << "Enter width: ";

stud: can >> width;


// Call the calculation function area

= calculate Area (length, width);

// Display output stud: coot << "Area: " <<

area << stud: end; return 0;

5. Building and Running the Program:

Use a C++ compiler (like g++) or an IDE (Integrated Development Environment) to translate your C++
code into an executable file.

Run the executable file to bring your program to life!

6. Debugging and Testing:

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.

Object Creation Process:

✓ 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).

Method Definition Structure:

access_modifier return_type method_name(parameters) {

// Method body containing statements defining the behavior

Let's break down the components:

✓ 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:

✓ Not Universally Supported: Overloading isn't available in all OOP languages.


✓ Disambiguation: The compiler uses parameter lists to determine the correct method to call,
ensuring there's no ambiguity.
✓ Benefits: Overloading can improve code readability and maintainability by allowing methods
with the same name to perform different tasks based on the arguments provided.

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.

Key Points about Overriding:

✓ 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.

You might also like