2
STRUCTURED PROGRAMMING
• Precursorto OOP
• Consists of well-structured and separated modules
• User can create its own user-defined functions
• Requires more discipline at the design and logical structuring stage
3.
3
OOP
• Brings togetherdata and functions that execute on them
• Supports encapsulation, abstraction, inheritance, polymorphism, etc
• Includes data hiding feature = it is more secure
• Based on real life entities that focuses on by whom task is to be done rather than
focusing on what to do
5
• Abstraction: Hidingcomplex implementation details and showing only
essential features to the user, e.g using a car without knowing exactly
how the engine works.
• Therefore: Abstraction means showing only what’s necessary and
hiding unnecessary details.
• Driving a car
• You use the steering wheel, pedals, and gear.
• You don’t need to understand how the engine, fuel injection,
or transmission works.
• You interact with simple interfaces.
• The complex implementation is hidden.
• You only care that the car moves when you press the
accelerator—not how combustion works.
6.
6
• Encapsulation: Bundlingdata (attributes) and the methods (functions)
that operate on that data within a single unit (class), controlling
access to protect data from outside interference.
• Encapsulation is about bundling related things together and
hiding the messy details.
• A TV remote control
• You press the power or volume button.
• You don’t need to know how the electronics inside work.
• The complicated stuff is hidden inside the remote.
• Data (information) and methods (actions) are kept together
in a class.
• Users can only access what they’re allowed to, not the
internal details.
• Like using an ATM: you withdraw money, but you never see
how the bank processes it.
7.
7
• Inheritance: Allowingnew classes (subclasses) to inherit properties
and behaviors from existing classes (superclasses), promoting code
reuse (e.g., a Cat inherits from Animal).
• Inheritance allows something new to reuse existing features
instead of starting from scratch.
• A family:
• A child inherits traits like eye color or height from parents.
• But the child can also have unique characteristics.
• A new class (child) can inherit properties and behaviors from
an existing class (parent).
• It can add or modify features.
• Example: Vehicle Car Electric Car
→ →
• An electric car still has wheels and brakes, but adds a
battery and charging..
8.
8
• Polymorphism: Theability for objects to take on many forms, allowing a
single interface to represent different underlying types or methods
performing differently based on the object.
• Polymorphism means the same action works differently depending on
the situation.
• The word “play”
• Play music
• Play football
• Play a movie
• Same word, different actions depending on context.
• The same method name can behave differently for different
objects.
• Like pressing the “Start” button:
• On a washing machine it washes clothes
→
• On a car it starts the engine
→
13
• Encapsulation: Keepthings together
and hide the complexity
• Abstraction: Show only what the user
needs
• Inheritance: Reuse and extend
existing features
• Polymorphism: Same action, different
results
C++ AND C#BOTH ARE
COMMONLY USED
PROGRAMMING
LANGUAGES AND CAME
UP WITH DIFFERENT
POWERFUL FEATURES
USED IN DIFFERENT
USE CASES
16.
16
C++
• C++ codeneeds to be recompiled for each platform.
• Can be run on any platform with the appropriate
compiler
• Used where the application needs to directly
communicate with hardware
19
C#
• Run onthe Common Language Runtime (CLR) which is a
component of the Microsoft .NET Framework
• Compiled = executable code is in an intermediate
language called Common Intermediate Language (CIL)
or Microsoft Intermediate Language (MSIL)
• Not machine-specific & can run on any platform that has
the CLR installed
• CIL code executed = CLR compiles it into machine code
that can be executed by the processor.
22
C#
• using: letsyou use classes from a namespace without
full names
• System: built-in namespace that has basic classes (like
Console)
• class: defines a class (container for code)
• static: method belongs to the class itself, no object
needed
• void: return type meaning “no value returned”
23.
23
C# - CLASS
•At the most basic level, classes are ways to organize code. You
could write your entire program in a single file, but it would be
very difficult to manage
• By grouping code and data together in ways that make sense
to you as the programmer, you make your life much easier
24.
24
C# - CLASS
•Imagine a person. A person has a name, age, address.
• When you make a class that represents a person you have to
see the class as an actual person.
• You write the name of what the class represents, in this case a
person:
• class Person
• Then you write down what a person has, what were those
attributes again?
• class Person
• Name
• Age
• Adres
25.
25
C# - CLASS
•You can also write methods in a class.
• Just think about what a person could do?
• A person might be able to walk.
• So you would write a method inside the Person class that
represents walking.
• Thats a class. Now do this for a chair, an airplaine, a car.
26.
26
C# - CLASS
•If you imagine a video game,
• have a lot of similar things that will have similar properties
and functionality: random things/objects will need location
data to determine where to display them as well as
something that defines what visuals to use, creatures will
need various ways of moving around and interacting with
things, etc.
• Could duplicate the same code a lot, as well as defining
everything in one giant block before you use it or group the
code up and reuse it in multiple places.
• With a squirrel class = can create as many squirrels as you
want without having to write the same code and properties
over and over for each squirrel
• can create a whole bunch of them in a loop with each one
having its own copy of the properties and functionality.