Principles of Programming
Lecture Notes 2
Programming - Lecturer: Semwezi Andrew 1
Low Level Languages (LLL)
• Computers operate using binary digits
• Binary is hard for human beings to work with
• LLL is closest to hardware
• More efficient (but also dangerous!)
• E.g.
Binary Code
Assembly
Programming - Lecturer: Semwezi Andrew 2
Binary code (ASCII)
Programming - Lecturer: Semwezi Andrew 3
Assembly Program
Programming - Lecturer: Semwezi Andrew 4
High Level Languages (HLL)
• Simpler to write & understand
• More library support, data structures
Examples:
Language Type
Procedural Language C, Fortran, COBOL, BASIC
Object Oriented Language C++, Java, PHP, etc
Programming - Lecturer: Semwezi Andrew 5
Algorithm: Pseudo code
A PROGRAM FOR ADDING TWO NUMBERS AND DISPLAY TOTAL
Start
Declare integer Variable Num1
Declare integer Variable Num2
Declare integer Variable Total
Assign a value 500 to Num1
Assign a value 650 to Num2
Add Num1 and Num2 and store the answer in variable Total
Display the value of Total on the screen
End
Programming - Lecturer: Semwezi Andrew 6
C Program
//header files
#include<stdio.h>
//start of program
int main()
{
printf(“A program for adding two numbers and display total“);
//Declaration
int Num1;
int Num2;
Int Total;
//definition or assignment
Num1 = 500;
Num2 = 650;
//operation
Total = Num1+Num2;
//output
printf(“The Total of %d and %d is: %d“ Num1,Num2,Total);
//End of program
return 0;
} Programming - Lecturer: Semwezi Andrew 7
C++ Program
//header files
#include<iostream.h>
//start of program
int main()
{
cout<<“A program for adding two numbers and display total“;
//Declaration
int Num1;
int Num2;
Int Total;
//definition or assignment
Num1 = 500;
Num2 = 650;
//operation
Total = Num1+Num2;
//output
cout<<“The Total of “<<Num1<<“and”<<Num2<<“Is”<<Total;
//End of program
return 0; Programming - Lecturer: Semwezi Andrew 8
}
PHP Program
<? Php
echo “A program for adding two numbers and display total“;
//Declaration
$Num1;
$Num2;
$Total;
//definition or assignment
$Num1 = 500;
$Num2 = 650;
//operation
$Total = $Num1+$Num2;
//output
echo“The Total of “+$Num1+“and”+$Num2+“Is”+$Total;
//End of program
?>
Programming - Lecturer: Semwezi Andrew 9
C++ Programming
Programming - Lecturer: Semwezi Andrew 10
History of C++
• In 1970 Dennis Ritchie created C Programming language.
• In the early 1980’s, also at Bell Laboratories, another
programming language was created which was based upon
the C language.
• Bjarne Stroustrup identified that OOP features could be
included in the software development.
• C++ Development started by Stroustrup in 1979.
• C++ is also called as C with classes
• Stroustrup states that the purpose of C++ is to make writing
good programs easier and more pleasant for the individual
programmer.
• C++ programming language is extension to C Language.
Programming - Lecturer: Semwezi Andrew 11
Compilers for C++
• A compiler is a special program that processes
statements written in a particular programming
language and turns them into machine language
code (binary code) that a computer's processor
can execute. Typically, a programmer writes
language statements in a language such as Pascal
or C one line at a time using an editor.
• Examples of compilers for C++
– Borland C++ (Windows) also an IDE
– GCC (linux)
Programming - Lecturer: Semwezi Andrew 12
Editors
This is a program that is used to type the source
code of a computer program. Sometimes they
are called text editors.
Examples of editors include:
• Notepad
• Borland C++
Programming - Lecturer: Semwezi Andrew 13
Debugger
• This is a program that checks
and reveals errors in a
computer program. Borland
C++ has an inbuilt debugger.
Programming - Lecturer: Semwezi Andrew 14
Compiling a program
Once errors have been corrected in the source
code, it is converted to executable code by the
compiler.
e.g. sum.cpp sum.exe
Programming - Lecturer: Semwezi Andrew 15
C++ programming
• C++ Programming language is most popular
language after C Programming language.
• C++ was one of the first Object oriented
programming languages.
• The structure of C++ Program is summarized
in the following Diagram
Programming - Lecturer: Semwezi Andrew 16
Header File Declaration
• Header File provides Prototype declaration for
different library functions.
• These libraries (in header files) provide functions
(pieces of programs) which were by other
programmers to bring added functionality to your
program.
• Some header files can be inbuilt or user define.
• E.g. “# include<iostream.h> “ which is used to
support input and output operations of a program.
Programming - Lecturer: Semwezi Andrew 17
Variables
• A variable is a symbolic name for (or reference to)
information.
• The variable's name represents what information
the variable contains.
• When a variable is created, space to keep that
information is reserved in memory (RAM)
• They are called variables because the represented
information can change but the operations on
the variable remain the same.
• Each variable has a data type.
• E.g. int age;
Programming - Lecturer: Semwezi Andrew 18
Rules for writing variable name in C
• A variable name can have letters (both uppercase and
lowercase letters), digits and underscore only.
• The first letter of a variable should be either a letter or
an underscore. However, it is discouraged to start
variable name with an underscore. It is because
variable name that starts with an underscore can
conflict with system name and may cause error.
• There is no rule on how long a variable can be.
However, the first 31 characters of a variable are
discriminated by the compiler. So, the first 31 letters of
two variables in a program should be different.
Programming - Lecturer: Semwezi Andrew 19
Constants
• In computer programming, a constant is an
identifier with an associated value which
cannot be altered by the program during
normal execution – the value is constant.
• E.g. const double PI = 3.14;
Programming - Lecturer: Semwezi Andrew 20
Contrasting between a variable and a
constant
Programming - Lecturer: Semwezi Andrew 21
Data types
• C is a strongly typed language. What this
means it that, the type of a variable cannot be
changed. Suppose, you declared an integer
type variable. You cannot store character or a
decimal number in that variable.
• E.g.
int age;
Programming - Lecturer: Semwezi Andrew 22
Data Types
Name Description Size Range
char Character 1 byte unsigned: 0 to 255
short int Short integer 2 bytes signed: -32768 to 32767
unsigned: 0 to 65535
int Integer 4 bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
long int Long integer 4 bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
float Floating point 4 bytes 3.4e +/- 38 (7 digits)
number
double Double precision 8 bytes 1.7e +/- 308 (15 digits)
floating point
number
long double Long double 8 bytes 1.7e +/- 308 (15 digits)
precision floating
point number
Programming - Lecturer: Semwezi Andrew 23
Return type
• In computer programming, the return type (or
result type) defines and constrains the
data type of the value returned from a
subroutine or method. The type of data
returned must be explicitly specified when
declaring a function.
• E.g. int main() or void main()
Programming - Lecturer: Semwezi Andrew 24
cin operator
• In C++ cin is used to accept value from the
user (keyboard). It can be able to accept a
value from user and that value gets stored
inside value container i.e. Variable.
• E.g.
Cin>>age;
Programming - Lecturer: Semwezi Andrew 25
cout operator
• In C++ cout operator is used to display value
to the user. The value may be some message
in the form of string or variable.
• E.g.
cout<<“Welcome to C++";
cout<<age;
cout<<“The age is”<<age<<endl;
Programming - Lecturer: Semwezi Andrew 26
Comments in C++
A comment in C++ Programming is similar as that of
in C. Each and every language will provide this great
feature which is used to document source code. We
can create more readable and eye catching program
structure using comments. We should use as many
comments as possible in C++ program. A comment
is non executable Statement in the C++.
Types of Comments:
– Single Line Comment
– Multiple Line Comment
Programming - Lecturer: Semwezi Andrew 27
Single line comments
• Single Line comment starts with // symbol.
• Remaining line after // symbol is ignored by
browser.
• End of Line is considered as End of the
comment.
• E.g.
//Print Hello World
cout<<"Hello World";
cout<<"www.c4learn.com"; //A Website
Programming - Lecturer: Semwezi Andrew 28
Multiple Line Comment
• Multi Line comment starts with /* symbol.
• Multi Line comment ends with */ symbol.
• E.g.
/* The statement below displays the
message in quotes on the screen*/
cout << "Hello C++ Programming";
Programming - Lecturer: Semwezi Andrew 29
Operators
• An operator is a symbol which tells compiler to take an action
on operands and yield a value. The Value on which operator
operates is called as operands. C++ supports wide verity of
operators. Supported operators are listed below –
Operator Explanation
Arithmetic Operators Used for arithmetic operations
Used for specifying relation
Relational Operators
between two operands
Used for identifying the truth
Logical Operators
value of the expression
Bitwise Operators Used for shifting the bits
Used for assigning the value to
Assignment Operators
the variable
Programming - Lecturer: Semwezi Andrew 30
Object Oriented Programming
1. OOP provides a clear modular structure for programs.
2. It implements real life scenario.
3. More reliable software development is possible.
4. Much suitable for large projects.
5. Fairly efficient languages.
6. It has the feature of memory management.
7. Implementation details are hidden from other modules
and other modules has a clearly defined interface.
8. It is easy to maintain and modify existing code as new
objects can be created with small differences to existing
ones.
Programming - Lecturer: Semwezi Andrew 31
OOP concepts
• Class: This is a programmer-defined data type, which
includes local functions as well as local data. You can think
of a class as a template for making many instances of the
same kind called objects.
• Object: An individual instance of the data structure defined
by a class. You define a class once and then make many
objects that belong to it. Objects are also known as
instance.
• Member Variable: These are the variables defined inside a
class. This data will be invisible to the outside of the class
and can be accessed via member functions. These variables
are called attribute of the object once an object is created.
• Member function: These are the function defined inside a
class and are used to access object data.
Programming - Lecturer: Semwezi Andrew 32
OOP concepts
• Inheritance: When a class is defined by inheriting
existing function of a parent class then it is called
inheritance. Here child class will inherit all or few
member functions and variables of a parent class.
• Parent class: A class that is inherited from by
another class. This is also called a base class or
super class.
• Child Class: A class that inherits from another
class. This is also called a subclass or derived
class.
Programming - Lecturer: Semwezi Andrew 33
OOP concepts
• Polymorphism: This is an object oriented concept where
same function can be used for different purposes. For
example function name will remain same but it may take
different number of arguments and can do different task.
• Operator Overloading: a type of polymorphism in which
some or all of operators have different implementations
depending on the types of their arguments.
• Data Abstraction: Any representation of data in which the
implementation details are hidden (abstracted).
• Encapsulation: refers to a concept where we encapsulate
all the data and member functions together to form an
object.
Programming - Lecturer: Semwezi Andrew 34
Classes
• Classes model elements of the problem
context.
• Each class has:
– Characteristics/Properties
– Responsibilities/Function (or required behaviors)
Programming - Lecturer: Semwezi Andrew 35
Example of a class
• Class: Student
• Characteristics: (what it has)
– Age
– RegNo
• Functions: (what it does)
– SetAge (age)
– SetRegNo (regno)
Programming - Lecturer: Semwezi Andrew 36
An Object
• An object is an instance of a class
• Class: Student
• Object: Johnson
• Characteristics:
– Age =20
– RegistrationNo =201230
• Functions:
– SetAge (20)
– SetGender (Male)
Programming - Lecturer: Semwezi Andrew 37
Access controls
• Private
This means the member of a class can only be
used inside the class it is define
• Public
This means that a member of a class can be
used by members of other classes.
Programming - Lecturer: Semwezi Andrew 38
Global and Local Declarations
• Global declarations are accessible anywhere
with in the program.
• Local declarations are accessible in specific
sections of the program
Programming - Lecturer: Semwezi Andrew 39
Structure of an OO C++ Program
Programming - Lecturer: Semwezi Andrew 40
Section 1 : Header File Declaration Section
• Header files used in the program are listed
here.
• Header File provides Prototype declaration for
different library functions.
• We can also include user define header file.
• Basically all preprocessor directives
are written in this section.
• E.g. # include<iostream.h>
Programming - Lecturer: Semwezi Andrew 41
Section 2: Global Declaration Section
• Global Variables are declared here.
• Global Declaration may include –
–Declaring Structure
–Declaring Class
–Declaring Variable
E.g. int Age;
Programming - Lecturer: Semwezi Andrew 42
Section 3 : Class Declaration and
Function Definition Section
• Actually this section can be considered as sub
section for the global declaration section.
• Class declaration and all methods of that class
are defined here.
• Function/Method Definitions. Used to
describe or write statements for the work
performed by the class functions
Programming - Lecturer: Semwezi Andrew 43
Section 4 : Main Function
• Each and every C++ program always starts with
main function.
• This is entry point for all the function. Each and
every method is called indirectly through main.
• We can create class objects in the main.
• Operating system call this function automatically.
• E.g. int main ()
{
…
}
Programming - Lecturer: Semwezi Andrew 44
A Simple OOP Program
// Header Files
#include <iostream.h>
#include<conio.h>
// Class Declaration
class student
{
//Access - Specifier
public:
//Varibale Declaration
int age;
int RegNo;
};
//Main Function
int main()
{
// Object Creation For Class
student John;
//Get Input Values For Object Variables
cout<<"Enter the Student age :";
cin>>John.age;
cout<<"Enter the Student Registration Number :";
cin>>John.RegNo;
//Show the Output
cout << “Age: ”John.age;
cout << “Registration Number:"<< John.RegNo ;
getche();
Programming - Lecturer: Semwezi Andrew 45
return 0;
Escape Sequences
• Sometimes, it is necessary to use characters
which cannot be typed or has special meaning in
C programming. For example: newline(enter),
tab, question mark etc. In order to use these
characters, escape sequence is used.
• For example: \n is used for newline. The
backslash ( \ ) causes "escape" from the normal
way the characters are interpreted by the
compiler.
Programming - Lecturer: Semwezi Andrew 46
Escape Sequency
ESCAPE SEQUENCES CHARACTER
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character
Programming - Lecturer: Semwezi Andrew 47