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

1

Introduction

Dr. Hadeer Ahmed Hassan Hosny

Second term
2021
2 Points to be covered:
 Why This Course?
 Why C++?
 What is development tools?
 Starting to write simple C++ program.
 Review of basics taken before:
 Variables
 Reading and writing to user
 Operators
 Conditional Execution
 Iteration (loops)
 Outline of course content
3 Why This Course?
 To help students improve or acquire the programming
skills they need for research in earth sciences or
engineering.
 Learning how to develop a software is needed in all
fields.
 A computer program is an example of computer
Software. Software makes a computer a truly universal
machine transforming it into the proper tool for the task
at hand.
 Computing professionals known as software engineers
develop software to drive particular systems .
 The software is written using programming languages
like : java , C++, python, C#, and others.
 In this course we will learn C++.
4 Why C++?
 C++ is a MUST for students and working professionals to
become a great Software Engineer. I will list down some
of the key advantages of learning C++:
 C++ is very close to hardware, so you get a chance to work
at a low level which gives you lot of control in terms of
memory management and better performance.
 C++ programming gives you a clear understanding about
Object Oriented Programming.
 C++ is from the most widely used programming languages
in application and system programming. So you can
choose your area of interest of software development.
 C++ really teaches you the difference between compiler,
linker and loader, different data types, classes, variable
types their scopes etc.
5 What is development tools?
 Software can be represented by printed words and
symbols that are easier for humans to manage than
binary sequences (Machine language).
 The development tools exists that automatically convert a
higher-level description of what is to be done into the
required lower-level code.
 The higher-level language code is called Source code.
The compiled machine language code is called the
Target code. The Compiler translates the source code into
the target machine language.
 A debugger allows a programmer to more easily trace a
program’s execution in order to locate and correct errors
in the program’s implementation.
 Examples of known C++ tools:
 Dev C++, Borland, Visual studio, Code blocks, and etc.
6
Starting to write a simple C++
program.
 Properly written C++ programs have a particular structure.
 The syntax must be correct, or the compiler will generate
error messages known as (syntax error) and not produce
executable machine language.
 Types of errors in programming:
 Syntax error.
 Logic error: writing the logic of code wrong.
 Compilation error: results after compiling program like dividing
by zero.
7
Starting to write a simple C++
program.
 Let us look at a simple code that would print the words Hello
World.

#include <iostream>
using namespace std;

// main() is where program execution


begins.
int main() {
cout << "Hello World"; // prints
Hello World
return 0;
}
8
Starting to write a simple C++
program.
 #include <iostream> This line
is a preprocessing directive.
 All preprocessing directives
#include <iostream> within C++ source code
using namespace std; begin with a # symbol. This
one directs the preprocessor
// main() is where program execution to add some predefined
begins. source code to our existing.
int main() {  The line using namespace
cout << "Hello World"; // prints std; tells the compiler to use
Hello World the std namespace.
return 0;  int main() { This specifies the
} real beginning of our
program. Here we are
declaring a function named
main. All C++ programs must
contain this function to be
executable.
9
Review on basics

 Review of basics taken before:

 Variables
 Reading and writing to user
 Expressions and Arithmetic
 Conditional Execution
 Iteration
10
Review on basics (variable)
 While writing program in any language, you need to use
various variables to store various information. Variables are
nothing but reserved memory locations to store values.
 Following table lists down seven basic C++ data types:
11 Review on basics (variable)

#include <iostream>
using namespace std;

int main() {
cout << "Size of char : " <<sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : "<<sizeof(short
int)<< endl;
cout << "Size of long int :"<< sizeof(long int) <<
endl;
cout << "Size of float : " << sizeof(float) <<
endl;
cout << "Size of double : " << sizeof(double) <<
endl;
return 0;
}
12
Review on basics (reading and writing)
 To read a value from user we use cin>>.
 Example
 int x; cin>>x;

 To write (print) value to user we use cout<<.


 We use the escape sequence in “” to print as shown in table
 Example
 Cout<<“hello”<<x;
Review on basics
13
(Operators)
 An operator is a symbol that tells the compiler to perform
specific mathematical or logical manipulations. C++ is rich
in built-in operators and provide the following types of
operators :
 Arithmetic Operators
 Relational Operators
 Logical Operators
Review on basics
14
(Arithmetic Operators)
Review on basics
15
(Relational Operators)
Review on basics
16
(Logical Operators)
Review on basics
17
(Conditional execution)
 Decision making structures require that the programmer specify
one or more conditions to be evaluated or tested by the
program, along with a statement or statements to be executed
if the condition is determined to be true, and optionally, other
statements to be executed if the condition is determined to be
false.
Review on basics
18
(Conditional execution)
 C++ programming language provides following types of
decision making statements.
Review on basics
19
(Conditional execution)
#include <iostream>
If - else
using namespace std;

int main () {
// local variable declaration:
int a = 100;

// check the boolean condition


if( a < 20 ) {
// if condition is true then print the
following
cout << "a is less than 20;" << endl;
} else {
// if condition is false then print the
following
cout << "a is not less than 20;" << endl;
}
cout << "value of a is : " << a << endl;

return 0;
}
#include <iostream>
using namespace std;
20
int main () {
// local variable declaration:
char grade = 'D';
Switch
switch(grade) {
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;

return 0;
}
Review on basics
21
(Iterative)
 A loop statement allows us to execute a statement or group of
statements multiple times and following is the general from of a
loop statement in most of the programming languages:

Review on basics
22
(Iterative)
 C++ programming language provides following types of
Iterative statements.
Review on basics
23 (Iterative)
While
#include <iostream>
using namespace std;

int main () {
// Local variable declaration:
int a = 10;

// while loop execution


while( a < 20 ) {
cout << "value of a: " << a << endl;
a++;
}

return 0;
}
Review on basics
24
(Iterative)
For

#include <iostream>
using namespace std;

int main () {
// for loop execution
for( int a = 10; a < 20; a = a + 1 ) {
cout << "value of a: " << a << endl;
}

return 0;
}
Review on basics
25
(Iterative)
Do-while
#include <iostream>
using namespace std;

int main () {
// Local variable declaration:
int a = 10;

// do loop execution
do {
cout << "value of a: " << a << endl;
a = a + 1;
} while( a < 20 );

return 0;
26
Outline of course
 New contents going to be covered during course:
 Functions
 Arrays
 Pointers
 Structure
 Strings
 Files and exception handling
 Object oriented programming.
 Namespace and Generic

 Finally a project using C++ is required.

You might also like