0% found this document useful (0 votes)
20 views14 pages

Lecture 1 - SWE 234 - Progaramming II

Cameroon HND - programming II
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views14 pages

Lecture 1 - SWE 234 - Progaramming II

Cameroon HND - programming II
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

SWE 234 – Programming II

Course Objectives

This courses aims at teaching students the following:

- To teach the student the concepts of Object Oriented Programming


- To differentiate between functions, classes and objects
- To learn to overload functions and operators
- Students will learn how to build and deploy applications
- Introduces students to database programming using ADO.NET

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

Chapter 1: Concepts of Object Oriented programming:

- Object oriented paradigm-differences between Object Oriented Programming and Procedure


oriented programming,
- Basic concepts of Object Oriented Programming
 Encapsulation, Inheritance and Polymorphism, Abstraction,
- Benefits of OOP
- Structure of a C++ program, namespace, Data types, C++ tokens, identifiers, variables, constants,
operators, control structures & loops.

Overview of C language:

- C language is known as structure oriented language or procedure oriented language


- Employs top-down programming approach where a problem is viewed as a sequence of tasks to
be performed.
- All program code of C can be executed in C++ but converse many not be possible
- Function overloading and operator overloading are not possible.
- Local variables can be declared only at the beginning of the block.
- Program controls are through jumps and calls to subroutines.
- Polymorphism, encapsulation and inheritance are not possible. For solving the problems, the
problem is divided into a number of modules. Each module is a subprogram.
- Data abstraction property is not supported by procedure oriented language.
- Data in procedure oriented language is open and can be accessed by any function.

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

- It can be called as an incremental version of C language

Difference between Procedure Oriented Programming (POP) & Object Oriented Programming (OOP)

Procedure Oriented Programming Object Oriented Programming


Program is divided into small parts called program is divided into parts called objects
functions.
Importance is not given to data but to functions as Importance is given to the data rather than
well as sequence of actions to be done. procedures or functions because it works as a real
world.
Follows Top Down approach. OOP follows Bottom Up approach
It does not have any access specifier. OOP has access specifiers named Public, Private,
Protected, etc.
Data can move freely from function to function in objects can move and communicate with each
the system. other through member functions.
To add new data and function in POP is not so easy OOP provides an easy way to add new data and
function
Most function uses Global data for sharing that can In OOP, data cannot move easily from it can be
be accessed freely from function to function to kept public private so we can control the access of
function or function in the system data
It does not have any proper way for hiding data so OOP provides Data Hiding so provides more
it is less secure security
Overloading is not possible In OOP, overloading is possible in the form of
Function Overloading and Operator Overloading
Example of Procedure Oriented Programming are : Example of Object Oriented Programming are : C+
C, VB, FORTRAN, Pascal +, JAVA, VB.NET, C#.NET.

Principles ( or features) of object oriented programming:

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

Different ways to achieving polymorphism in C++ program:

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

Benefits of object oriented programming (OOPs)


 Reusability: In OOP‟ s programs functions and modules that are written by a user can be reused by
other users without any modification.
 Inheritance: Through this we can eliminate redundant code and extend the use of existing classes.
 Data Hiding: The programmer can hide the data and functions in a class from other classes. It helps
the programmer to build the secure programs.
 Reduced complexity of a problem: The given problem can be viewed as a collection of different
objects. Each object is responsible for a specific task. The problem is solved by interfacing the
objects. This technique reduces the complexity of the program design.
 Easy to Maintain and Upgrade: OOP makes it easy to maintain and modify existing code as new
objects can be created with small differences to existing ones. Software complexity can be easily
managed.
 Message Passing: The technique of message communication between objects makes the interface
with external systems easier.
 Modifiability: it is easy to make minor changes in the data representation or the procedures in an
OO program. Changes inside a class do not affect any other part of a program, since the only public
interface that the external world has to a class is through the use of methods.

OOP with C++

Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 5
SWE 234 – Programming II

C++ is a powerful, general-purpose programming language that combines elements of procedural,


object-oriented, and generic programming. It’s widely used for a variety of applicants, including system
software, game development, and data analysis.

Key Features of C++

- Object-Oriented: Support classes, objects, inheritance, polymorphism and encapsulation.


- Procedural: Can be used in a procedural style, with functions and procedures.
- Generic Programming: Allows you to write code that can work with different data types using
templates
- Performance: C++ is known for its efficiency and performance, making it suitable for high-
performance applications.
- Low-level Control: Provides direct access to hardware and memory, making it suitable for system
programming.
- Cross-Platform: C++ code can be compiled and run on various platforms, including windows,
Linux and macOS.

Basic structure of a C++ Program

A C++ program typically consists of the following components:

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:

 Optional but recommended to avoid naming conflicts.


 Declares a namespace to group related entities.

3. Global Variables (Optional):

 Variables declared outside of any function or class are global and can be accessed from anywhere
in the program.

4. Functions:

 The building blocks of a C++ program.


 Define a specific task or behavior.
 Can have parameters and return values.
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 6
SWE 234 – Programming II

5. main() Function:

 The entry point of the program.


 Execution begins in the main() function.

#include <iostream> // Include the input/output stream library

using namespace std; // Use the standard namespace

int main() {

// Code goes here

cout << "Hello, world!" << endl;

return 0;

Explanation:

- #include<iostream>: include the iostream header file, which provides input/output


functionalities.
- Using namespace std: Brings the std namespace into scope, allowing you to use its members
without qualification
- int main(): Defines the main function, which is the entry point of the program.
- Cout << “Hello, world!” << endl; : Print the message “Hello, world!” to console.
- Return 0; : Indicates successful program termination

C++ Data Types

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:

1. Primary (fundamental) data types


2. Derived data types
3. User-defined data types

C++ 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

Primary or Built-in Data Types

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:

- Signed bit: represents positive integer, 1 represent negative numbers


- A signed integer uses 1 bit for sign and 15 bits for magnitude of the number
- Unsigned integers use all 16 bits to store the magnitude
- E.g x = 100; y = -100
10010 = 000000000011001002

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

= -32768 + 16384 + 8192 + 4096 + 2048 +1024 +512+256+128+0+0+26+8+4+0+0 = -100

Derived Data Types

 Arrays: Ordered collection of elements of the same type.


 Pointers: Variables that store the memory address of another variable.
 References: Aliases to existing variables.

User-defined data type

 Structures: User-defined data types that group related variables.


 Enumerations: User-defined data types that define a set of named constants.

C++ Tokens

Identifiers: Identifiers are the names given to various program elements such as variables, functions and
arrays.

Rules for declaring identifiers

- The first character must be an alphabet or underscore


- It must consist of only letters, digits and underscore
- Identifiers may have any length but only first 31 characters are significant
- It must not contain white space or blank space.
- We should not use keywords as identifiers
- Upper and lower case letter are different e.g ab Ab aB AB are treated differently

Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 9
SWE 234 – Programming II

Examples of valid identifiers: a, x, n, num, SUM, fact, grand_total, sum_of_digits, num1

Examples of invalid identifiers: $amount, 3num, grand-total, sum of digits, 4num

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

int a, b, a // is invalid while

int a, b; //is valid

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

double pi = 3.14159; // declares a double-precision floating-point variable name pi

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.

- Use meaningful variable names that reflect their purpose


- Initialize variables when declaring them to avoid undefined behavior.
Mr. Constantine N. Mbufung - MSc Data Science – Tel: 678659025 / 693101501 - Page 10
SWE 234 – Programming II

- Use appropriate data types to avoid unnecessary memory usage.


- Be mindful of variable scope to prevent unexpected behavior.

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

- auto: Automatically deduces the type of a variable based on its initializer.


- break: Exits a loop or switch statement.
- case: Used within a switch statement to specify a case.
- char: Defines a character data type.
- const: Declares a constant value that cannot be modified.
- continue: Skips the current iteration of a loop and proceeds to the next.
- default: Used within a switch statement as the default case.
- delete: Deletes dynamically allocated memory.
- do: Used with while to create a do-while loop.
- double: Defines a double-precision floating-point data type.
- else: Used with if to specify the alternative action if a condition is false.
- enum: Defines an enumeration data type.
- extern: Declares a variable or function that is defined elsewhere.
- float: Defines a single-precision floating-point data type.
- for: Used to create a loop that executes a block of code a specified number of times.
- if: Used to conditionally execute a block of code.
- int: Defines an integer data type.
- long: Defines a long integer data type.
- new: Allocates memory for dynamically created objects.
- operator: Used to overload operators.
- private: Access modifier for class members that can only be accessed within the class.
- protected: Access modifier for class members that can be accessed within the class and its derived
classes.
- public: Access modifier for class members that can be accessed from anywhere.
- return: Returns a value from a function.
- short: Defines a short integer data type.
- signed: Indicates that a number can be positive or negative.
- sizeof: Returns the size of a data type or variable.
- static: Used to declare static variables and functions.
- struct: Defines a structure data type.
- switch: Used to select one of several code blocks to execute based on a condition.
- template: Used to create generic classes and functions.
- this: Pointer to the current object.
- throw: Throws an exception.
- try: Used with catch to handle exceptions.
- typedef: Creates an alias for a data type.
- union: Defines a union data type that can hold different data types in the same memory location.
- unsigned: Indicates that a number cannot be negative.
- void: Indicates that a function does not return a value.
- volatile: Indicates that a variable's value can be changed by external factors.

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

- Using the const keyword:


const int MAX_VALUE = 100;
- Using the #define preprocessor directive:
#define PI 3.14159

Benefits of using constants

- 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;

OPERATORS AND EXPRESSIONS

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.

C operators can be classified as

- 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

You might also like