0% found this document useful (0 votes)
35 views

C++ Programming Language NOTICE LECTURE 1 & 2 AT UAUT - NYAMU

C++ Programming Language NOTICE LECTURE 1 & 2 AT UAUT -NYAMU

Uploaded by

ezekiel nyamu
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

C++ Programming Language NOTICE LECTURE 1 & 2 AT UAUT - NYAMU

C++ Programming Language NOTICE LECTURE 1 & 2 AT UAUT -NYAMU

Uploaded by

ezekiel nyamu
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

Programming Language

A programming language is a formal language comprising a set of instructions that can be


used to produce various kinds of output. It is used by developers to communicate with
computers and write software programs, scripts, or other sets of instructions.

 Programming languages are used to create software applications, system programs,


websites, etc.
 These languages have syntax and semantics that must be followed in order for the
code to be valid and executable.
 They can be categorized into low-level (closer to machine code) and high-level
(abstracted from hardware) languages.

Examples:

 Low-level Language: Assembly language, which is closer to machine code.


 High-level Language: Python, Java, C++, which allow for complex tasks to be
performed with relatively simple syntax.

2. Programmer

A programmer, also known as a software developer or coder, is a person who


writes, tests, debugs, and maintains the source code of computer programs.

 Programmers use various programming languages to create software


applications, tools, and systems that perform specific tasks.
 They write algorithms and translate them into code that can be executed by a
computer.
 In addition to coding, programmers often troubleshoot, debug, and optimize
code to ensure it runs efficiently and correctly.

1. Machine-Level Language (Machine Code)


Definition: Machine-level language, also known as machine code, is the lowest-level
programming language directly understood by a computer's central processing unit (CPU).
It consists of binary instructions (0s and 1s) that communicate directly with the hardware.

 Machine-level language is hardware-specific. This means the machine code for one
type of processor may not work on another because each processor architecture has its
own set of machine instructions.
 Programs written in machine language are fast and efficient since they execute
directly on the hardware without any need for translation or interpretation.
 It’s highly error-prone and difficult to write due to its complexity and the need for
programmers to manage intricate details such as memory allocation and hardware
registers manually.

Eg processor that uses a simple instruction set, consider the following


binary machine code: 10110000 01100001

This code could be interpreted as a command for the processor to load a specific value into a
register. For example, it may instruct the CPU to load the value 61 (in hexadecimal) into the
register AL.

2. Assembly-Level Language

Definition: Assembly language is a low-level programming language that uses human-


readable mnemonics (symbols) to represent machine-level instructions. It’s a step above
machine language and provides a symbolic representation of the binary code.

 Assembly language is more understandable for humans compared to machine code


but still maintains a close relationship with the hardware.
 Each assembly language instruction corresponds directly to a machine language
instruction.
 Assemblers are used to convert assembly code into machine code. An assembler is a
tool that translates the mnemonics and symbolic operands into the binary equivalent
understood by the machine.
 While easier to read than machine code, assembly is still difficult and is typically
used for tasks that require very fine control over hardware (e.g., operating system
kernels, device drivers)

Assembler

Definition: An assembler is a program that translates assembly language code into machine
code, which can be executed by the computer's processor.

Explanation:

 Assembly language is a low-level language that is more human-readable than


machine code but still closely tied to the architecture of the hardware.
 The assembler converts the mnemonics (e.g., MOV, ADD, SUB) into binary machine code
that the processor can understand and execute.
 Assemblers are essential tools for programming in low-level languages, especially
when fine control over hardware is needed.

Example:

assembly MOV AL, 61h

This instruction means "Move the hexadecimal value 61 into the AL register." This is a more
understandable form of the machine code example given earlier.

3. High-Level Language

Definition: High-level programming languages are abstracted from the machine's hardware
and use human-readable syntax, which is easier for programmers to understand and use.
These languages focus on problem-solving rather than worrying about hardware specifics.

 High-level languages allow developers to write complex programs more quickly


and efficiently since they handle much of the complexity involved in memory
management, registers, and hardware control.
 These languages are portable, meaning that programs written in them can usually run
on multiple types of computer systems with little or no modification.
 Compilers and interpreters translate high-level languages into machine code that
the processor can execute.
 Examples of high-level languages include Python, Java, C++, and PHP.

Compiler

Definition: A compiler is a program that translates code written in a high-level programming


language into machine code or intermediate code that can be executed by the computer.

Explanation:

 Compilers read the entire source code of a program and convert it into machine code,
often creating an executable file (.exe, .out, etc.).
 The compiler checks the source code for errors, performs optimizations, and generates
the final machine code in one go, after which the program can be executed.
 Since the compilation is done before execution, compiled programs generally run
faster than those interpreted at runtime.

Examples:

 The GCC (GNU Compiler Collection) compiles C and C++ programs into machine
code.
 javac is the compiler for Java, which converts Java source code into bytecode that can
run on the Java Virtual Machine (JVM).
5. Interpreter

Definition: An interpreter is a program that directly executes instructions written in a high-


level programming language without converting them into machine code beforehand.

Explanation:

 Instead of translating the entire program at once, as a compiler does, an interpreter


reads and executes the code line by line or instruction by instruction at runtime.
 This makes interpreted languages generally slower than compiled ones, as the
interpreter must translate code during execution.
 Interpreters are useful for rapid testing and development, as they allow immediate
execution of code without the need for a separate compilation step.

Examples:

 Python Interpreter: Python code is executed line by line by the Python interpreter
(python command).
 JavaScript Interpreter: JavaScript code is interpreted and executed directly by web
browsers.

Example: A simple "Hello, World!" program in Python:

print("Hello, World!")

THE HIGH-LEVEL PROGRAMMING LANGUAGES ARE BROADLY


CATEGORIZED IN TO TWO CATEGORIES:
(iv) Procedure oriented programming(POP) language.
(v) Object oriented programming(OOP) language.

Procedure-Oriented Programming (POP) Language


Procedure-Oriented Programming (POP) is a programming paradigm focused on functions,
procedures, or routines that operate on data. It is structured around a sequence of instructions
that tell the computer what to do, step by step, in a specific order. In POP, the program is
divided into smaller, manageable units called functions, which are then used to solve a
problem collectively.

Examples of POP Languages:


 C Language: One of the most popular POP languages, C is known for its efficient
execution, close hardware interaction, and ease of use.
 BASIC (Beginner's All-purpose Symbolic Instruction Code): A simple POP
language that emphasizes procedural methods, often used in early education for
teaching programming basics.
 FORTRAN (Formula Translation): Mainly used in scientific and engineering
applications, FORTRAN focuses on processing numeric data through procedures.

Structure of Procedure-Oriented Programming

The structure of a POP language follows a specific approach, where each step or function
contributes to solving the main problem. It typically involves:

1. Main Program: The core of a POP application starts with a main() function (as in
C), which acts as the starting point for executing other functions.
2. Procedures (or Functions): The main program is divided into smaller functions that
perform specific tasks. Each function is defined separately and performs a particular
role, taking input data, processing it, and returning results.
3. Modular Approach: Procedures are modular, meaning they can be reused within the
program or across programs, helping to make the code more organized and readable.
4. Data Movement: In POP, data is passed between functions using parameters, making
data handling central to the program structure.
5. Top-Down Design: The program design generally follows a top-down approach
where high-level tasks are divided into smaller, more manageable subtasks.

Example Structure in C:
#include <stdio.h>

// Function declaration
int add(int a, int b);
int main() {
int result = add(5, 10); // Call to add function
printf("The result is %d\n", result);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
In this example, the main function calls add, a separate function that adds two integers. Each
function is defined to perform a specific action, contributing to the program's overall functionality.

Disadvantages of Procedure-Oriented Programming


While POP is useful, it comes with some limitations:
 Poor Data Security: POP lacks the encapsulation feature found in Object-Oriented
Programming (OOP), making it difficult to protect data. Functions have
unrestricted access to global data, making data more vulnerable to accidental
modifications or security issues.
 Difficulty in Managing Large Programs: As programs grow in size, it becomes
harder to manage code in POP due to its sequential nature and high
interdependence among functions. In complex programs, this leads to issues with
maintainability and readability.
 Limited Reusability: Functions in POP are specific to tasks and often lack
flexibility. Unlike objects in OOP, which can inherit properties and methods, POP
functions cannot inherit or extend capabilities, which limits code reusability.
 Top-Down Approach May Be Restrictive: In some cases, the top-down approach
of POP can be restrictive when programs need frequent changes, especially if
modifications are required in lower-level functions.
Characteristics of procedure oriented programming:
 Emphasis is on doing things(algorithm)
 Large programs are divided into smaller programs known as
functions.
 Most of the functions share global data
 Data move openly around the system from function to function
 Function transforms data from one form to another.
 Employs top-down approach in program design
Note: The primary concern of procedural programming was that it views
data and functions in two separate entities,

Object-Oriented Programming (OOP)


Object-Oriented Programming (OOP) is a programming paradigm built around the concept of
"objects." These objects represent real-world entities with properties (data attributes) and
behaviors (methods). In OOP, programs are organized into objects rather than functions and
logic, as in Procedure-Oriented Programming. The focus is on defining classes, which are
blueprints for creating objects, promoting code reuse and modularity.

Examples of OOP Languages:

 Java: Known for its platform independence and extensive use in enterprise
applications, Java is widely used for its OOP capabilities.
 Python: Popular for its readability and simplicity, Python’s OOP features are integral
to its design and make it versatile for many applications.
 C++: An extension of C that introduces OOP, making it suitable for high-
performance applications with reusable and modular code.

Structure of Object-Oriented Programming

In OOP, a program is structured around objects and their interactions. The main elements of
this structure are:

1. Classes: A class acts as a blueprint for objects, defining the properties (data) and
behaviors (methods) common to all objects created from that class.
2. Objects: An object is an instance of a class. Each object has its own set of attributes
and can perform actions defined by the class’s methods.
3. Methods: Functions defined within a class that represent the behaviors or actions that
objects of that class can perform.
4. Encapsulation: Data and methods are encapsulated within classes, providing a secure
way to bundle and manage data and functions.
5. Modularity: OOP programs are modular, where each class operates independently,
making code easier to maintain and understand

Basic Idea Behind Object-Oriented Programming (OOP)


Fig 2 Relationship between data and function

In Object-Oriented Programming (OOP), data (also called attributes or properties) and functions
(also called methods) have a close relationship within the structure of classes and objects. This
relationship, known as encapsulation, binds data and the functions that operate on it within a single
class. The functions in a class manipulate or access the data of that class’s objects, creating a
cohesive structure that mirrors real-world entities

The basic idea behind Object-Oriented Programming (OOP) is to model real-world entities as
"objects" that combine both data and functionality in a way that reflects how they work in
the real world. OOP aims to simplify complex programs by breaking them into smaller,
reusable, and manageable objects, making the software more modular and easier to maintain.

In OOP, everything revolves around classes and objects:

 Classes: These act as blueprints or templates for creating objects. A class defines the
properties (attributes) and behaviors (methods) that the objects created from it will
have.
 Objects: These are individual instances created from classes. Each object can have its
own unique set of attributes, even if they share a class structure.

Note: hold the values that describe the entity (data) and can perform actions (methods) that
represent the entity's behavior

OOP emphasizes four key principles—encapsulation, inheritance, polymorphism, and


abstraction—that make it highly effective for building scalable, reusable, and easy-to-manage
software systems.

Data and Functions in OOP

In OOP, data and functions are tightly integrated within classes to provide structure and
encapsulation. This approach ensures that:
1. Data (attributes) and functions (methods) are encapsulated within classes.
2. Functions operate on the data within their objects, giving each object unique behavior.
3. Access to data can be controlled, preventing unwanted interference or misuse.

Features of the Object Oriented programming

1. Emphasis is on doing rather than procedure.


2. programs are divided into what are known as objects.
3. Data structures are designed such that they characterize the objects.
4. Functions that operate on the data of an object are tied together in the data structure.
5. Data is hidden and can’t be accessed by external functions.
6. Objects may communicate with each other through functions.
7. New data and functions can be easily added.
8. Follows bottom-up approach in program design.

Procedural Programming vs Object-Oriented Programming

Below are some of the differences between procedural and object-oriented programming:

Procedural Oriented Programming Object-Oriented Programming


Program Is Divided Into Small Parts Called Program Is Divided Into Small Parts Called
Functions. Objects.
Follows A Top-Down Approach. Follows A Bottom-Up Approach.
Has Access Specifiers Like Private, Public,
There Is No Access Specifier
Protected, Etc.
Adding New Data And Functions Is Not Easy. Adding New Data And Function Is Easy.
Does Not Have Any Proper Way Of Hiding Provides Data Hiding(Encapsulation) So It Is
Data So It Is Less Secure. More Secure.
Overloading Is Not Possible. Overloading Is Possible
There Is No Concept Of Data Hiding And The Concept Of Data Hiding And
Inheritance. Inheritance Is Used.
Function Is More Important Than The Data. Data Is More Important Than Function.
Is Based On The Unreal World. Is Based On The Real World.
Used For Designing Large And Complex
Used For Designing Medium-Sized Programs.
Programs.
Uses The Concept Of Procedure Abstraction. Uses The Concept Of Data Abstraction.

BASIC CONCEPTS OF OBJECTS ORIENTED PROGRAMMING


To understand the concept of object-oriented programming, it is necessary to know
the fundamental PRINCIPLES terms and concepts of this approach. These include objects,
classes, data abstraction, encapsulation, inheritance, polymorphism , Dynamic binding and
message passing. These concepts together form the foundation of OOP, enabling developers
to create modular, reusable, and easily maintainable code.

Object:

An object is a core component in Object-Oriented Programming, representing real-world


entities. It is created as an instance of a class, which doesn’t use memory until the object
itself is instantiated. Each object has its own identity, state, and behavior, holding data and
the code necessary to manipulate it. Objects can interact with each other without needing to
know the specifics of each other’s data or code; they only need to understand the type of
messages exchanged and responses expected.

Note: Note that the main objective of breaking down complex software projects into
objects is that changes made to one part of the software should not adversely affect the
other parts.

In Object-Oriented Programming (OOP), identity, state, and behavior are core attributes of
an object, defining how it uniquely represents and interacts with real-world entities.

1. Identity

Identity is what makes each object unique from other objects, even if they have the same
attributes and behaviors. In technical terms, the identity is often represented by the object’s
memory address or a unique identifier. It allows each object to be individually recognized
and referenced, making it possible to differentiate one object from another.

 Example: Imagine two Car objects, each created with the same brand and model but
stored as different objects in memory. Their unique identities allow them to be
referenced independently.

2. State

State refers to the attributes or properties of an object that represent its condition or current
data at a particular time. This data defines the characteristics of the object and can change
over time, meaning an object’s state can evolve as its attributes are modified.

 Example: In a Car object, the state might include attributes like color, speed, and
fuel_level. The fuel_level could change as the car is driven, modifying its state.

3. Behavior

Behavior defines what an object can do, represented by the methods or functions within the
object’s class. It dictates how the object interacts with data, other objects, and its
environment, allowing it to perform actions and exhibit functionality.

 Example: For a Car object, behaviors might include methods like accelerate(),
brake(), and refuel(). These methods alter the object’s state (e.g., accelerate()
increases the car’s speed, changing its state).
Attribute Description Example
Identity Unique identifier for the object Car1 and Car2 have unique identities
State Attributes that define the object’s condition speed = 60 km/h, fuel_level = 50%
Behavior Methods defining the object’s actions accelerate(), brake(), refuel()

For example “Dog” is a real-life Object, which has some characteristics like color, Breed,
Bark, Sleep, and Eats.

example, an object called Biscuit could have data attributes like product code P001, product
name Britannia Biscuits, price 20, and quantity in stock 50. These values define the object’s
characteristics or attributes.
consider another object, Maggi, with product code P002, product name Maggi Noodles, price
10, and quantity in stock 20. Additionally, functions like check_qty() and display_product()
can operate on the data within the object, defining the actions that can be performed on these
attributes.

Classes:

A class is defined as a user-defined data type that encompasses a complete set of similar
data and the functions associated with those objects. In essence, a class in OOP represents
a category of similar objects. In the real world, countless objects exist, each with its own
unique identity, but they can be grouped based on shared properties and functions. For
instance, cars, scooters, motorbikes, and buses can all be categorized as vehicles, while dogs,
cats, and horses belong to the animal category. Therefore, vehicles and animals can be
considered classes.

A class acts as a blueprint or template for its objects. Once a class is defined, any number of
objects belonging to that class can be created. The objects created from a class are referred to
as instances or variables of that class, and the process of creating these objects is called
instantiation. It’s important to note that a class itself does not represent a specific object;
rather, it defines the data and functions that an object will possess.
For example, a class Product consists of data such as p_code, p_name,p_price and
qty_in_hand, which specify the attributes or features of the objects of the Product class. In
addition, it consists of functions such as display_product() and check_qty() that specify the
actions that can be performed on data (see Figure)

Note that the data belonging to a particular class is known as its data members and the
functions of the class are known as the member functions and both collectively are known as
the members of the class

Data Abstraction:

Abstraction is a technique that conceals unnecessary details while highlighting only the
essential features, allowing a focus on what is important at any given time. It simplifies the
management of complex systems by emphasizing key aspects. For example, when driving a
car, a driver needs to understand only the fundamental controls, such as how to operate the
clutch, brake, accelerator, gears, and steering. They do not concern themselves with the car’s
internal mechanisms like the motor, engine, or wiring.

Abstraction can be categorized into two types: data abstraction and control abstraction.
Data abstraction (also known as data hiding) involves concealing details about the data,
whereas control abstraction pertains to hiding implementation details. In the object-oriented
paradigm, both data and functions can be abstracted. Typically, classes in OOP are structured
so that the data is hidden from the outside world, while the functions create a public interface.
This means that other functions can directly access the class’s functions, but hidden data can
only be accessed indirectly through these functions. It’s important to note that the values of
the hidden data members cannot be shared externally unless specific functions are written to
provide that information. By keeping the internal workings of a class concealed, data
abstraction enhances data security by protecting it from unintended modifications or
interference by other parts of the program.

Note: In object-oriented programming, classes are also referred to as Abstract Data Types
(ADT) because they utilize the concept of abstraction.

Encapsulation
Encapsulation is the technique of binding or keeping the data and functions (that
operate on them) together in a single unit called class. Encapsulation is the way to
implement data abstraction. A well-encapsulated object acts as a ‘black box’ for
other parts of the program. That is, it provides services to the external functions or
other objects that interact with it. However, these external functions or the objects
do not need to know its internal details. For example, in Figure above the data
p_code, p_name, p_price and qty_in_hand and the functions display_product()
and check_qty are encapsulated in a class Product.

Consider a real-life example of encapsulation, in a company, there are different sections like the
accounts section, finance section, sales section, etc. The finance section handles all the financial
transactions and keeps records of all the data related to finance. Similarly, the sales section handles
all the sales-related activities and keeps records of all the sales. Now there may arise a situation
when for some reason an official from the finance section needs all the data about sales in a
particular month. In this case, he is not allowed to directly access the data of the sales section. He will
first have to contact some other officer in the sales section and then request him to give the
particular data. This is what encapsulation is. Here the data of the sales section and the employees
that can manipulate them are wrapped under a single name “sales section”
Inheritance
Inheritance can be defined as the process whereby an object of a class acquires characteristics from
the object of another class. As stated earlier, all objects of similar type are grouped together to form a
class. However, sometimes a situation arises when different objects cannot be combined together
under a single group as they share only some common characteristics. In this situation, the classes are
defined in such a way that the common features are combined to form a generalized class and the
specific features are combined to form a specialized class. The specialized class is defined in such a
way that in addition to the individual characteristics and functions, it also inherits all the properties
and the functions of its generalized class.
For example, in the real world, not all the vehicles can be automobiles; some of them are pulled-
vehicles also. Thus, car and scooter both are vehicles that come under the category of automobiles.
Similarly, rickshaw and bicycle are the vehicles that come under the category of pulled-vehicles.
Thus, automobiles and pulled-vehicles inherit the common properties of the vehicle class and also
have some other properties that are not common and differentiate them. Thus, the vehicles class is the
generalization of automobiles and the pulled-vehicles class, and automobiles and pulled-vehicles
classes are the specialized versions of the vehicles class. Note that while inheriting the vehicle class,
the automobiles and pulled-vehicles do not modify the properties of the vehicle class; however, they
can add new properties that are exclusive for them (see Figure 1.6)
Polymorphism:

The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form. For
example, A person at the same time can have different characteristics. Like a man at the same
time is a father, a husband, an employee. So the same person posses different behavior in
different situations. This is called polymorphism.

https://www.geeksforgeeks.org/cpp-programming-examples/

BASICS OF C++
Dynamic Binding:

In dynamic binding, the code to be executed in response to the function call is decided at
runtime. Dynamic binding means that the code associated with a given procedure call is not
known until the time of the call at run time. Dynamic Method Binding One of the main
advantages of inheritance is that some derived class D has all the members of its base class B.
Once D is not hiding any of the public members of B, then an object of D can represent B in
any context where a B could be used. This feature is known as subtype polymorphism.

Message Passing:

is a communication method used in object-oriented programming where objects communicate


with each other by sending messages. When an object sends a message, it is typically
requesting that another object perform a specific action or retrieve information. This
mechanism promotes loose coupling between objects, as they do not need to know the details
of each other's internal implementation.

BENEFITS OF OOP:
Oop offers several benefits to both the program designer and the user. Object-oriented
contributes to
the solution of many problems associated with the development and quality of software
products.
The principal advantages are :
1. Through inheritance we can eliminate redundant code and extend the use of existing
classes.
2. We can build programs from the standard working modules that communicate with one
another, rather than having to start writing the code from scratch. This leads to saving of
development time and higher productivity.
3. This principle of data hiding helps the programmer to build secure programs that can’t be
invaded by code in other parts of the program.
4. It is possible to have multiple instances of an object to co-exist with out any interference.
5. It is easy to partition the work in a project based on objects.
6. Object-oriented systems can be easily upgraded from small to large systems.
7. Message passing techniques for communication between objects makes the interface
description with external systems much simpler.
8. Software complexity can be easily managed.
APPLICATION OF OOP:
The most popular application of oops up to now, has been in the area of user interface
design such as windows. There are hundreds of windowing systems developed using oop
techniques
Real business systems are often much more complex and contain many more objects
with complicated attributes and methods. Oop is useful in this type of applications because it
can simplify a complex problem. The promising areas for application of oop includes.
1. Real – Time systems.
2. Simulation and modeling
3. Object oriented databases.
4. Hypertext,hypermedia and expertext.
5. Al and expert systems.
6. Neural networks and parallel programming.
7. Dicision support and office automation systems.
8. CIM / CAM / CAD system.

You might also like