FPCPP C1
FPCPP C1
FPCPP C1
FUNDAMENTALS OF
1
PROGRAMMING IN C++
INTRODUCTION
C++ and It’s Features 2
• C++ was built on the older language C, and there's a lot of
C code still around.
• C++ embraces the strong quality of C like efficiency and
portability.
• Beside that C++ includes more strong qualities like OOP
features, exception handling and templates.
• Templates are a relatively new addition to C++.
• They allow us to write generic classes and functions that
work for several different data types.
• E.g. demonstrate templates using max function design to
support different data types.
C++ and It’s Features 3
• C had one major problem, it was a procedural-oriented programming language.
• Procedural-oriented programming
• the operations are separated from the data they use.
• lacks reusability, makes debugging complicated and inconsistency are major problems of this paradigm.
• C++ is mainly meant to resolve C’s problem, C++ supports object-oriented programming.
• data abstraction, that is, the creation of classes to describe objects
• data encapsulation for controlled access to object data
• inheritance by creating derived classes (including multiple derived classes)
• polymorphism (Greek for multiform), that is, the implementation of instructions that can have varying
effects during program execution.
Programs from conception to execution 4
• C++ is a high-level programming language that allows a software engineer to efficiently code a
program.
• A program is a set of instruction, written using a programming language, to solve a specific
problem.
• Programming language can be high-level, assembly or low-level.
• Programs starts out as an idea in the programmers mind.
• He writes down his idea in paper, it becomes algorithm.
• Then code the algorithm using a programming language it becomes a program.
•
� Discuss program Vs Algorithms
�
Programs from conception to execution 5
• C++ programs are written in a high-level language using letters, numbers, and the other symbols
you find on a computer keyboard.
• The writing is done using stand alone editors or IDE(integrated development environment).
• Computers actually execute a very low-level language called machine code (a series of numbers).
• Therefore, how letters, numbers and symbols are understood?
• 💡 a program can be used, it must undergo several transformations called compilation and
So, before
execution.
• Compilation is a process of translating a program written in high-level programming language into its
equivalent machine(or object) code.
• Execution is a process of assigning computational resources to the executable machine code and
perform the operations specified on the data provided.
Compilation and executions 6
1. Editors: we use this to create and save the source code to
source file. Modern compilers have integrated editors.
2. Loaders: prior to compilation header files are loaded to
the source file using loader.
3. Compilers: are software, designed to translate a source
file to object file containing machine code.
4. Linkers: finally, the linker combines the object file with
other modules to create the executable file.
Note: compilation starts from the very first line in the source
code, while execution starts from the main function.
Anatomy of C++ programs 7
• The first line, #include <iostream>, is called preprocessor
#include <iostream> directive. It directs the preprocessor to include all the
functionalities from iostream library.
using namespace std;
• E.g. cin and cout functions
int main() • The second line, using namespace std, is a using directive
{ used to allow direct access to the names(key-words)
reserved by C++ from std dictionary.
cout<< “Hello C++!" << endl;
• E.g. if, else, int, for…
return 0; • The third line, int main(), indicates where program
} execution starts from.
• Note: C++ is case-sensitive.
Elements of programming 8
1. Comment: provides the programmer with a clear, easy-to-read description of how and what the
program does.
Comments are not going to be compiled and executed.
Mostly comments contain:
Headings: tells what the program does and how.
Author: Credits for the programmer.
File formats, References, Error handling techniques and etc.
C++ supports two types of comments
i. Single line comment started using double slash (//) and spans a single line
ii. Multiple line comment started using (/*) and spans several lines and terminated with (*/)
For 16 bit computers int is equivalent with short and for 32 bit computers int is
equivalent to long.
Cont… 12
Summary of built-in (fundamental) data types
Data type Size Range Precision
Long
16 bytes ±3.36 x 10-4932 to ±1.18 x 104932 36 digits
double
Initialization
Or other variable…
int age=28; char gender=‘M’;
float c_gpa=3.81;
string name=“Shewangizaw Melaku”;
int pin=age; string ID=1888;
Cont… 15
5. Constants: are also called literals.
Constants can not be declared and assigned values later.
Constants must be initialized.
Constants are created using “const” key-word and can not be modified.
Syntax:
Major categories of constants:
const Type Name = value;
Boolean constant: are initialized with true or false as a value.
Integral constant: are initialized with integers as a value.
Decimal, Octal or Hexadecimal constants.
Floating point constants: are initialized with numbers with fractions as a value.
Character constants: are initialized with characters enclosed by ‘’ as a value.
String constants: are initialized with sequence of characters enclosed by “” as a value.
Cont… 16
6. Operators: Operators are symbols that perform operations on variables and values. There are 6-
types of operators
1. Arithmetic Operators: used to perform arithmetic operations on variables and data.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;
Cont… 18
3. Relational Operators: used to check the relationship between two operands.
Logical AND.
&& expression1 && expression2
True only if all the operands are true.
Logical OR.
|| expression1 || expression2
True if at least one of the operands is true.
Logical NOT.
! !expression
True only if the operand is false.
Cont… 20
5. Bitwise Operators: used to perform operations on individual bits. They can only be used alongside char and int
data types.
Operator Description
| Binary OR
^ Binary XOR
-> used with pointers to access the class or struct variables ptr->marks = 92;
Function Purpose
strcpy(s1, s2); Copies string s2 into string s1.
strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1.