Nothing Special   »   [go: up one dir, main page]

02 - CSC 201 - Intro To Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

CONCEPTS OF COMPUTER PROGRAMMING

Computer program: A computer program is a set of instructions written in a programming language in


sequential order to control the activities of a computer system.

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.

COMPUTER PROGRAMMING LANGUAGES


There are two main types of computer programming languages:
• Low-level language
• High-level language.

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.

Disadvantages of Machine Language


• Very bulky.
• They require much time for writing and reading.
• They are prone to error which is difficult to be detected and corrected.
• Very difficult to learn.
• It can only run on the computer it is designed for (machine dependent)

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

Disadvantages of Assembly Language


• It is machine dependent; the programmer has to be knowledgeable in both the subject
area and the operations of the machine.

1
• It is cumbersome though less cumbersome than that of machine language.
• Very expensive to develop and
• It consumes time.

High-Level Language: is a problem-orientated programming language, whereas a low-level language is


machine oriented. The source programs are written in human-readable languages like English instead of
symbols. In other words, a high-level language is a convenient and simple means of describing the
information structures and sequences of actions required to perform a particular task.
Advantages of High-Level Language.
• The person writing the program does not need to know anything about the
computer in which the program will be run (Machine Independent).
• The programs are portable.
• Very easy to learn and write
• Errors are easy to detect and debug.

Features of High-Level Language


• Machine independent
• Problem-oriented
• Ability to reflect the structure of the program written in it.
• Readability
• Programs are portable.

Examples of High-level Languages are FORTRAN, COBOL, QBASIC, VISUAL BASIC, JAVA, C,
C++, PYTHON, C#, etc.

Characteristics of a Good Program


• Transferability/Machine Independent - Must be able to work on any computer machine.
• Reliability- It can be relied upon to do what is expected.
• Efficiency/cost saving- It must not cost more than its benefits and enables the problem to be
solved appropriately, quickly, and efficiently.
• Simplicity- It should be as simple as possible to understand.
• Understandability/Readability- It must be readable and understandable by other programmers
and end users.
• Flexibility/Adaptability/Maintainability- A good program must be flexible, adaptable, and
maintainable to suit the user’s needs. Modification must be possible and very easy.

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

• Object-Oriented programming (OOP)

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.

The disadvantage of the procedure-oriented programming language is:


• Global data access
• It does not model real word problems very well
• No data hiding

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 a top-down approach in program design
Examples of procedural programming languages are C, Pascal, Fortran and Cobol

Object Oriented Programming


Object-Oriented Programming (OOP) is a programming paradigm that organises code around objects,
which are instances of classes. OOP focuses on creating modular, reusable, and maintainable code by
encapsulating data and behaviour into objects.

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.

Basic Concepts of Object-Oriented Programming


• Classes
• Objects
• Data Abstraction
• Data Encapsulation
• Inheritance
• Polymorphism

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.

Writing the first program


Let us follow the traditional choice of writing the first program in a new programming language.

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.

Behind the Scene


It is useful to know what goes on behind the scenes when your program gets built. First, the compiler
translates the C++ source code (that is, the statements that you wrote) into machine instructions. The
machine code contains only the translation of the code that you wrote. The implementors of your C++
development environment provided a library that includes the definition of “cout” and its functionality. A
library is a collection of code that has been programmed and translated by someone else, ready for
you to use in your program. A linker program takes your machine code and the necessary parts from the
C++ library and builds an executable file. (the below figure gives an overview of these steps.) The
executable file is usually called firstprogram.exe or firstprogram, depending on your computer system.
You can run the executable program even after you exit the C++ development environment.

8
Editor/IDE Step 1: Write source codes

Source codes (.cpp) & Headers (.h)

Preprocessor Step 2: Preprocess

Include files, replace symbols

Compiler Step 3: Compile Build


Object codes (.obj, .o)

Static libs (.lib, .a) Linker Step 4: Link


Executable (.exe)

Shared libs (.dll, .so) Loader Step 5: Load

Run

Input CPU Step 5: Execute

Output

Processing the Program


• Use IDE/text editor to create a source program.
• In C++, statements that begin with the # symbol are called pre-processor directives.
• Compiler:
Ø Checks the program rules (i.e., syntax, BUT not logic), AND...
Ø Translates program into machine language (object program)
• Linker: Combines object program with other programs (libraries) provided by Software
Development Kit (SDK) to create executable code.
• Loader: Loads executable program into main memory
• Program executed

Analysing the First Program

• #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

You might also like