0% found this document useful (0 votes)
114 views8 pages

CSC 202 Session 1

The document provides an introduction to Object-Oriented Programming (OOP) using C++, highlighting its advantages over procedural programming languages like C. It explains key concepts such as classes, objects, inheritance, code reusability, and polymorphism, emphasizing how OOP models real-world scenarios more effectively. Additionally, it includes study questions to reinforce understanding of the material presented.

Uploaded by

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

CSC 202 Session 1

The document provides an introduction to Object-Oriented Programming (OOP) using C++, highlighting its advantages over procedural programming languages like C. It explains key concepts such as classes, objects, inheritance, code reusability, and polymorphism, emphasizing how OOP models real-world scenarios more effectively. Additionally, it includes study questions to reinforce understanding of the material presented.

Uploaded by

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

Ladoke Akintola University of Technology

Open and Distance Learning Centre


Computer Science Department
CSC 202- Computer Programming II
(Object Oriented Programming using C++)

FIRST LECTURE NOTE


1. INTRODUCTION
If there were no limitations in the earlier programming languages, there won’t be a need
for another programming language. Object-Oriented Programming (OOP) was developed
because limitations were discovered in earlier approaches to programming. A review of
these limitations will help us to appreciate what OOP has to offer.
2. DIFFERENCES BETWEEN C AND C ++
• C++ could be called the advanced version of C language. Therefore, it could be called
a superset of C as almost every correct statement in C is also a correct statement in
C++, although the reverse is not true.
• C++ was originally called “C with classes. The most important elements added to C to
create C++ are classes, objects, and object oriented programming.
• C++ also has many other new features such as improved approach to input/output
(I/O), references, templates, exception handling and a new way to write comments.
• Most importantly, C is a procedural programming language while C++ is an object
oriented programming language. As a procedural language, C prepares a list of
instructions for the compiler to interpret. However, when these instructions become
larger, they become difficult to manage. To curtail this effect, function was introduced.
With this, each function contains a group of instructions with clearly defined purpose
and a clearly defined interface needed to interact with other functions in the program.

1|Page
Despite the introduction of functions, large programs in procedural programming
languages are still excessively complex.

The followings are responsible:


A. Functions have unrestricted access to global data
Data in a procedural program can either be a local data or a global data. A local data is a
data that is defined within a function and can only be exclusively used within the function.
A local data cannot be accessed or modified by other functions. In contrast, a global data
can be accessed by any function in the program. The relationship between a local and
global data is as illustrated below:

Figure 1: Global and Local Variables

In a large program, there could be many functions and many global data items.
This results to a large number of potential connections between functions and
global data as illustrated in Figure 2.

2|Page
Figure 2: Connections between Functions and Data
This large number of connections create problems in several ways:
i. Firstly, it makes a program’s structure difficult to conceptualize. Fewer connections
make it easier to capture data and functions that will be needed in a program.
ii. Secondly, it makes the program difficult to modify. A change made in a global data
may necessitate rewriting all the functions that access that data. When data items
are modified in a large program it may not be easy to identify the exact functions
that is accessing the data, and even when you figure this out, modifications to the
functions may cause them to work incorrectly with other global data items.
Everything is related to everything else, so a modification anywhere has far-
reaching, and often unintended consequences.

B. The separation of data and functions in procedural programs make it difficult to


model real world scenario
In the physical world we deal with objects such as people, cars and the relationships
between them. They have both attributes and behavior that must be well represented and
captured in our programs. Hence, separating data and their functions is not the best
approach to modeling real world data.

3|Page
3. The Object-Oriented Approach
Object oriented languages attempt to combine both data and the functions that operate
on that data into a single unit called an object. An object could be any entity of interest
being studied. This entity will have an attribute (which are its unique characteristics) and
behaviours (which defines operations that can be performed on the object) e.g. an object
could be a STUDENT, its attributes could be: name, matric_no, age, level, dept etc while
its behaviours could be course_registration, room_application, school_fees_payment,
medical_clearrance etc.
The data of an object in OOP cannot be accessed directly i.e. they are hidden, so they are
safe from accidental alteration. They can only be accessed by calling its member functions.
This feature is called data/information hiding

Figure 3: Relationship among Objects in OOP

4|Page
4. Characteristics/ Features of Object-Oriented Languages
i. Objects: these are any entity of interest in any scenario being considered. This
could be physical objects, Human entities (Employees, Students, Customers),
Collections of data (An inventory, A personnel file) etc.
ii. Classes: these are collection of similar objects, therefore, an object is called an
instance of a class. E.g. The vehicle class could have cars, trucks, buses, motorcycles
as objects. A University_personnel class could have students, lecturers,
admin_officers, Typist, drivers, security_man as objects.
A class can be divided into sub-classes, where the original class is called the base class
and the sub-classes that share the characteristics of the base class, but add their own
as well are called the derived classes.

5|Page
Figure 4: Relationship between a Base Class and Derived Classes
iii. Inheritance: the ability of a derived class to inherit some characteristics from their
base class in addition to their own unique characteristics. Since I know that a
derived class can automatically share and inherit the features of a base class, I don’t
need to declare these features for the derived class again.
iv. Code Reusability: another important feature of OOP is code reusability. In this
instance, a class that has been written, created, and debugged can be distributed to
other programmers for use in their own programs. This is similar to the way a library
of functions in a procedural language can be incorporated into different programs.

6|Page
A programmer can take an existing class and, without modifying it, add additional
features and capabilities to it. This is done by deriving a new class from the existing
one. The new class will inherit the capabilities of the old one, but is free to add new
features of its own.

Example
You might have written (or purchased from someone else) a class that creates a
menu system, such as that used in Windows or other Graphic User Interfaces (GUIs).
This class works fine, and you don’t want to change it, but you want to add the
capability to make some menu entries flash on and off. To do this, you simply create
a new class that inherits all the capabilities of the existing one but adds flashing
menu entries.

v. Polymorphism: It is the ability of an object to respond differently to the same


function call. For example, C++ has 45 operators that can be used in different ways
depending on what they are operating on. This could be achieved by defining new
behaviors for these operators. The process of doing this is called Operator
Overloading. For example, the assignment operator = can be used to copy one
object to another. Therefore, Operator Overloading is a type of polymorphism.
vi. Modelling: OOP uses Unified Modeling Language (UML) to have a better
understanding of the relationships that exist among program elements. To achieve
this, several UML diagrams could be used. UML diagrams provide a standardized
way to visualize a program’s structure and operation.

7|Page
UML Diagrams
Class diagrams show the relationships among classes, object diagrams show how specific
objects relate
Sequence diagrams show the communication among objects over time.
Use case diagrams show how a program’s users interact with the program, and so on.

Study Questions
i. Enumerate the advantages of OOP over Procedural programming languages.
ii. Why is it difficult to manage large programs in procedural programming language?
iii. Establish the relationship between class and objects using real life scenario excluding
those used in the textbook.
iv. Explain the relationship among classes, sub-classes and inheritance.
v. How is code reusability related to inheritance?
vi. Explain the concept behind Polymorphism.

References
Robert Lafore (2002), “Object-Oriented Programming in C++”, Sams Publishing Fourth
Edition

8|Page

You might also like