02 - CSC 201 - Intro To Programming
02 - CSC 201 - Intro To Programming
Computer programming: This is the process of writing a sequential set of instructions using programming
language to control the activity of a computer system.
Programming language: This is an artificial language used in writing a set of instructions to control the
activities of a computer system. Examples are FORTRAN, COBOL, BASIC, Java, C, C++, Python, etc.
Low-level language is closer to the machine than the human natural language. The two major examples
are Machine and Assembly language.
• Machine Language: This is the only language computer understands. The computer directly
executes a program written in machine language. Such programs are coded using strings of 0’s
and 1’s. It doesn’t need a translator.
• Assembly Language: uses MNEMONICS (symbols) to represent data and instructions. Such a
program eliminates problems associated with machine language. Acomputer cannot execute directly a
program written in assembly language. It requires a translator called an assembler. Assembler
is a special program designed to translate program content in assembly language to a machine
language equivalent.
1
• It is cumbersome though less cumbersome than that of machine language.
• Very expensive to develop and
• It consumes time.
Examples of High-level Languages are FORTRAN, COBOL, QBASIC, VISUAL BASIC, JAVA, C,
C++, PYTHON, C#, etc.
2
Phases of Program Development (Programming)
The process of producing a computer program (software) is divided into Eight phases or stages:
1. Problem Definition (Analysis Stage): There is a need to understand the problem that requires a
solution. The need to determine the data to be processed, the form of the data, the volume of the
data, and what needs to be done to the data to produce the expected/required output.
2. Selection or development of an algorithm: An algorithm is the set of steps required to solve a
problem written down in the English language.
3. Designing the program: In order to minimise the amount of time to be spent in developing the
software, the programmer makes use of a flowchart. A flowchart is the pictorial representation of
the algorithm developed in step 2 above. Pseudocode IPO chart (input processing output) and
HIPO chart (Hierarchical- input-processing and output) may be used in place of flowchart or to
supplement flowchart.
4. Coding the statement: This involves writing the program statements. The programmer uses the
program flow chart as a guide for coding the steps the computer will follow.
5. Compiling: There is a need to translate the program from the source code to the machine or
object code. A computer program is fed into the computer first, and then as the source program
is entered, a translated equivalent (object program) is created and stored in the memory.
6. Running, Testing, and Debugging: When the computer is activated to run a program, it may
find it difficult to run it because many syntax errors might have been committed. Manuals are
used to debug the errors. A program that is error-free is tested using some test data. If the
program works as intended, real required data are then loaded.
7. Documentation: This is the last stage in software development. This involves keeping written
records that describe the program, explain its purposes, and define the amount, types, and
sources of input data required to run it. List the Departments and people who use its output and
trace the logic the program follows.
Programming Paradigm
Programming paradigms are different approaches or styles of programming that provide a set of
principles and concepts for designing and structuring code. Each paradigm has its own philosophy and set
of techniques for solving problems and organising software.
• Procedural Programming
3
Procedural Programming
Procedural programming focuses on breaking down a program into smaller procedures or functions.
Programs are structured around sequences of instructions that operate on data. It emphasises modularity,
reusability, and step-by-step execution of instructions.
4
Features of the Object-Oriented Programming
• Emphasis is on doing rather than procedure.
• Programs are divided into what are known as objects.
• Data structures are designed such that they characterise the objects.
• Functions that operate on the data of an object are tied together in the data structure.
• Data is hidden and can’t be accessed by external functions.
• Objects may communicate with each other through functions.
• New data and functions can be easily added.
• Follows bottom-up approach in program design.
5
Class
A class is a blueprint or template that defines the structure and behaviour of objects. It defines the
attributes (data) and methods (functions) that objects of the class will have. Objects are created from
classes.
Objects
An object is an instance of a class. It represents a specific entity with its own set of attributes and
behaviour. Objects can interact with each other by invoking methods and accessing attributes.
Data Abstraction
Abstraction focuses on modelling real-world entities by defining essential characteristics and behaviours
while hiding unnecessary details. It allows the creation of abstract classes and interfaces that provide a
common interface for related classes.
Data Encapsulation
The wrapping up of data and function into a single unit (called class) is known as encapsulation. The data is
not accessible to the outside world, and only those functions which are wrapped in the class can access it.
These functions provide the interface between the object’s data and the program.
Inheritance
Inheritance allows the creation of new classes based on existing classes (superclasses or base classes). The
derived classes (subclasses or derived classes) inherit the attributes and methods of the base class. It
promotes code reuse and enables the creation of hierarchical relationships among classes.
Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It
provides the ability to use a single interface for different implementations. Polymorphism is achieved
through method overriding and method overloading.
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:
• Through inheritance, we can eliminate redundant code and extend the use of existing classes.
• We can build programs from the standard working modules that communicate with one another
6
rather than having to start writing the code from scratch. This leads to saving of development
time and higher productivity.
• This principle of data hiding helps the programmer build secure programs that can’t be invaded
by code in other parts of the program.
• It is possible to have multiple instances of an object co-exist without any interference.
• It is easy to partition the work in a project based on objects.
• Object-oriented systems can be easily upgraded from small to large systems.
• Software complexity can be easily managed.
APPLICATION OF OOP
OOP is extensively used in software development to build robust, modular, and maintainable systems, as
well as in the development of Graphical User Interfaces (GUI). Below are some common applications of
OOP:
• Software Development.
• Graphical User Interfaces
• Game Development
• Simulation and modelling.
• Database Systems.
• Web Development.
• Embedded Systems.
These are just a few examples of how OOP is applied in various domains. OOP's flexibility, code
organisation, and modularity make it a powerful paradigm for developing complex software systems.
INTRODUCTION TO C++
The Basics of C++
C++ is a general-purpose programming language that supports multiple programming paradigms,
including procedural and object-oriented programming. It extends the C programming language and
provides additional features and capabilities. It combines the features of both high-level and low-level
programming languages, allowing developers to write efficient and flexible code.
C++ is commonly used in various domains, including systems programming, game development, embedded
systems, scientific computing, and high-performance computing. Its combination of high-level abstractions
and low-level control makes it a popular choice for developers seeking performance, flexibility, and a
broad range of applications.
7
C++ Development Environment
Integrated Development Environment (IDE) has made it easy to write, test, and debug programs. Some of
the IDEs you can use for C++ development are Code Block, Eclipse for C++, Visual Studio Code,
CodeLite, Sublime Text, NetBeans, Atom, etc.
You can launch an editor, a program that functions like a word processor, in which you can enter your
C++ instructions, then open a console window and type commands to execute your program.
Using the steps that are appropriate to your programming environment, type the above program in your
editor or IDE and save it as firstprogram.cpp (C++ files end with a .cpp extension). As you write this
program, carefully consider the various symbols, and remember that C++ is case-sensitive. You must enter
upper and lowercase letters exactly as they appear in the program listing.
8
Editor/IDE Step 1: Write source codes
Run
Output
• #include <iostream> - Lines beginning with a hash sign (#) are directives for the pre- processor.
#include<iostream> tells the preprocessor to include the iostream (stream input/output) standard
file. This specific file (iostream) includes the declarations of the basic standard input-output
library in C++, and it is included because its functionality wil be used later in the program.
• using namespace std; - tells the compiler to use the “standard namespace.” Namespaces are a
mechanism for avoiding naming conflicts in large programs. Simply add using namespace std; at
the top of every program that you write, just below the #include directives.
9
• int main()
{
...
return 0;
}
Defines a function called “main” that “returns” an “integer” (that is, a whole number without a
fractional part, called int in C++) with value 0. This value indicates that the program finished
successfully. A function is a collection of programming instructions for a particular task. Every
C++ program must have a main function. Most C++ programs contain other functions besides
the main. Simply place the code that you want to execute inside the braces of the main function.
• cout << "My First Program!"; - To display values on the screen, you use an entity called cout
and the << operator (sometimes called the insertion operator). This line is a C++ statement. A
statement is a simple or compound expression that can actually produce some effect. In fact, this
statement performs the only action that generates a visible effect in our first program. The
statement ends with a semicolon (;), which marks the end of all expression statements in all C++
programs. You can send more than one item to cout. Use a << before each one of them. For
example: cout << "The answer is " << 6 * 7;
10