Lecture 1 - SWE 234 - Progaramming II
Lecture 1 - SWE 234 - Progaramming II
Course Objectives
General Introduction:
Factual programming is not a widely recognized term in Software Engineering. It is programming that
ensures software accurately represent the real world, avoids biases and provides reliable information.
In fields like Data Science or AI, it could refer to programming that focuses on extracting and analyzing
factual data.
Human-Computer Interaction (HCI) is the study of how humans interact with computers and other
technology. It focuses on designing technology that is usable, efficient, and enjoyable for people to use.
HCI encompasses a wide range of topics, including:
User experience (UX): The overall experience a user has when interacting with a product or
system.
User interface (UI): The visual elements and layout of a product or system.
Usability: How easy a product or system is to use.
Accessibility: How well a product or system can be used by people with disabilities.
Cognitive psychology: The study of how people think and learn.
Ergonomics: The study of how people interact with physical objects.
HCI professionals work to create technology that is intuitive, efficient, and enjoyable for users. They use
a variety of methods to gather user feedback and improve the design of products and systems.
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 1
SWE 234 – Programming II
Overview of C language:
- C++ can be considered as an improved version of C language which consists all programming
language constructs with newly added features of object oriented programming.
- C++ is structure (procedure) oriented and object oriented programming language.
- The file extension of C++ program is “.CPP”
- Function overloading and operator overloading are possible.
- Variables can be declared in inline i.e when required
- In c++ more emphasis is give on data rather than procedures
- Polymorphism, encapsulation and inheritance are possible.
- Data abstraction property is supported by C++.
- Data access is limited. It can be accessed by providing various visibility modes both for data and
member functions. there by providing data security by data hiding
- Dymanic binding is supported by C++
- It supports all features of c language
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 2
SWE 234 – Programming II
Difference between Procedure Oriented Programming (POP) & Object Oriented Programming (OOP)
- Encapsulation
- Data abstraction
- Polymorphism
- Inheritance
- Dynamic binding
- Message passing
Encapsulation:
Wrapping of data and functions together as a single unit is known as encapsulation. By default
data is not accessible to outside world and they are only accessible through the functions which are
wrapped in a class. prevention of data direct access by the program is called data hiding or information
hiding.
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 3
SWE 234 – Programming II
Data abstraction:
Abstraction refers to the act of representing essential features without including the back ground
details or explanation. Classes use the concept of abstraction and are defined as a list of attributes such
as size, weight, cost and functions to operate on these attributes. They encapsulate all essential
properties of the object that are to be created. The attributes are called as data members as they hold
data and the functions which operate on these data are called as member functions.
Class use the concept of data abstraction so they are called abstract data type (ADT)
Polymorphism:
Polymorphism comes from the Greek words “poly” and “morphism”. “poly” means many and
“morphism” means form i.e.. many forms. Polymorphism means the ability to take more than one form.
For example, an operation has different behavior in different instances. The behavior depends upon the
type of the data used in the operation.
1. Function overloading
2. Operator overloading
#include<iostream>
using namespace
std;
int main() {
int a=4; a=a<<2
cout << “a=” <<a<<endl;
return 0;
}
Inheritance:
Inheritance is the process by which one object can acquire the properties of another. Inheritance
is the most promising concept of OOP, which helps realize the goal of constructing software from
reusable parts, rather than hand coding every system from scratch. Inheritance not only supports reuse
across systems, but also directly facilitates extensibility within a system. Inheritance coupled with
polymorphism and dynamic binding minimizes the amount of existing code to be modified while
enhancing a system.
When the class child, inherits the class parent, the class child is referred to as derived class (sub
class) and the class parent as a base class (super class). In this case, the class child has two parts: a
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 4
SWE 234 – Programming II
derived part and an incremental part. The derived part is inherited from the class parent. The
incremental part is the new code written specifically for the class child.
Dynamic binding:
Binding refers to linking of procedure call to the code to be executed in response to the call.
Dynamic binding (or late binding) means the code associated with a given procedure call in not known
until the time of call at run time.
Message passing:
An object oriented program consists of set of object that communicates with each other. Objects
communicate with each other by sending and receiving information.
A message for an object is a request for execution of a procedure and therefore invokes the
function that is called for an object and generates result
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 5
SWE 234 – Programming II
1. Preprocessor Directives:
These lines begin with a # symbol and are processed before compilation.
Common preprocessor directives include:
o #include: Includes header files that contain declarations of functions, classes, and other
entities.
o #define: Defines constants or macros.
2. Namespace Declaration:
Variables declared outside of any function or class are global and can be accessed from anywhere
in the program.
4. Functions:
5. main() Function:
int main() {
return 0;
Explanation:
In C++, data types define the kind of values a variable can hold and the operations that can be
performed on them. C++ support the following classes of data types:
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 7
Primary data types Derived Data Types User Defined data Types
SWE 234 – Programming II
1. Integer Types:
- int: Signed integer (typically 4 bytes) the commonly used integer type. It is usually 4 bytes in
size and can store values from -2,147,483,647 to 2,147,483,647
- short: Signed short integer (typically 2 bytes) and has a smaller range than int.
- long: Signed long integer (a longer integer type, typically 4 bytes on 32-bit systems, 8 bytes
on 64-bit systems)
- long long: Signed long long integer (the longest integer type available, typically 8 bytes)
- unsigned int: Unsigned integer – An integer type that can only store positive values
- unsigned short: Unsigned short integer - a shorter unsigned integer type
- unsigned long: Unsigned long integer – a longer unsigned integer type
- unsigned long long: Unsigned long long integer – the longest unsigned integer type
2. Floating-Point Types:
- float: Single-precision floating-point number - 32 bits (4 bytes)
- double: Double-precision floating-point number 64 bits (8 bytes)
- long double: Extended-precision floating-point number 80 bits (10 bytes)
3. Character Type:
- char: Single character (occupies 1 byte of memory for storage of character)
4. Boolean Type:
- Bool: Boolean value (true or false)
5. Void Type: the void type has no values. This is usually used to specify the return type of functions.
The type of the function said to be void when it does not return any value to the calling function.
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 8
SWE 234 – Programming II
NB:
0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0
signed bit Magnitude
-10010 = 11111111100111002
1 1 1 1 1 1 1 1 1 0 0 1 1 1 0 0
= -1*215 + 1*214 +1*213 +1*212 +1*211 +1*210 +1*29 +1*28 +1*27 + 0*26 +0*25 +1*24 +1*23 +1*22 +0*21 + 0*20
C++ Tokens
Identifiers: Identifiers are the names given to various program elements such as variables, functions and
arrays.
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 9
SWE 234 – Programming II
Variables: A named memory location is called variable. Or It is an identifier used to store the value of
particular data type in the memory. Variable names must be unique in the given scope e.g
Variable declaration: a variable is a named storage location that holds a value. To use a variable, you
must first declare it, specifying its data type and name.
data_type variable_name;
Where:
- data_type: the type of data the variable will store (e.g int, float, char, bool)
- variable name: A unique identifier for the variable.
int age = 25; // declares an integer name age and initializes it with the value 25
char initial = ‘B’; // declares a character variable named initial an d initialize it with the character A
bool isStudent = true; //declares a Boolean variable name isStudent and initializes it with the value true
Multiple Declarations:
You can declare multiple variables of the same type in a single statement
int x, y, z;
Initialization:
Variables can be initialized when they are declared. If not initialized, their initial value is undefined.
Scope:
The scope of a variable determines where it can be accessed. Variables declared within a block (e.g a
function or loop) are only accessible within that block. Variables declared outside of any block are global
and can be accessed from anywhere in the program.
Keywords
Keywords in C++ are reserved words that have special meanings within the language. They can’t be used
as identifiers for variables, functions or other program elements. Examples
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 11
SWE 234 – Programming II
- while: Used to create a loop that executes a block of code as long as a condition is true.
Constants: In C++ are variables whose values cannot be changed after they are initialized. They are often used to
define values that remain constant throughout the program. There are two ways to declare constants in C++.
- Readability: Constants make code more readable by providing meaningful names for values.
- Maintainability: If a constant value needs to be changed, it can be easily modified in one place.
- Efficiency: some compilers may optimize code that uses constants, leading to better performance.
const double PI = 3.14159;
int radius = 5;
double area = PI * radius * radius;
An operator is a symbol which represents a particular operation that can be performed on data. An operand is the
object on which an operation is performed. By combining the operators and operands we form an expression. An
expression is a sequence of operands and operators that reduces to a single value.
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
- Increment or Decrement operators
- Conditional operator
- Bit wise operators
- unary operator
- Special operators
- Additional operators in c++
1. Arithmetic Operators: all basic arithmetic operators are present in C
#include<iostream>
using namespace std;
int main() {
/*int a = 10;
int b = 5;
int sum = a + b;
cout << "The sum is : "<<sum;
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 12
SWE 234 – Programming II
return 0;*/
int a, b;
cout<<"Enter any two integers:";
cin >>a>>b;
cout<< "Addition: "<< a + b<<"\n";
cout<< "Substraction : "<<a - b<<"\n";
cout<<"Multiplication : "<<a * b<<"\n";
cout<<"Division : "<<a / b<<"\n";
cout<<"Modulus Division : "<<a % b<<"\n";
}
2. Relational Operators: We often compare two quantities and depending on their relation take
certain decisions for that comparison.
int x = 10;
int y = 5;
if (x == y) {
cout << "x is equal to y" << endl;
} else {
cout << "x is not equal to y" << endl;
}
if (x > y) {
cout << "x is greater than y" << endl;
} else if (x < y) {
cout << "x is less than y" << endl;
} else {
cout << "x is equal to y" << endl;
}
Common Uses:
Conditional statements: Used in if, else, and switch statements to make decisions based on
conditions.
Loops: Used in while, do-while, and for loops to control the number of iterations.
Comparisons: Used to compare values in various contexts, such as sorting and searching
algorithms.
Note:
The == operator is used to compare values for equality, while the = operator is used for
assignment.
Relational operators can be combined with logical operators (&&, ||, !) to create more complex
expressions.
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 13
SWE 234 – Programming II
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 14