1 - C++
1 - C++
1 - C++
Presented by:
Abubakar Elsafi.
Course Outline
1. Introduction
2. Flow of control
3. Loops
4. Arrays and strings
5. Functions
6. Pointers
7. Classes
8. Member Functions
9. Inheritance
10. Overloading
11. Polymorphism
12. Files
13. Dynamic Memory Allocation
Course Assessment
40% assignments + midterm test
60% final exam
Output:
Hello World!
Line-By-Line Explanation
1. // Comments: no effect on the behavior of the program.
C++ supports two ways to insert comments:
// line comment
/* break comment */
2. #include <iostream.h>: tells the preprocessor to include
the iostream standard file
3. int main (): starts the main function.
5. cout << "Hello World!“: prints the string.
6. return 0: causes the main to finish.
The main() function
•Execution of the program starts here.
•The return type of main must be int.
•main may not be called from inside the program, only
the system may call the main function.
•int main() { }.
C++ character set
•0 1 2 3 4 5 6 7 8 9 •
•A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
•a b c d e f g h i j k l m n o p q r s t u v w x y z
•_ $ { } [ ] # ( ) < > % : ; . ? * + / ^ & | ~ ! = , \ " ‘ <SPACE>
A Hello World Program
/* my second program in C++
with more comments */
#include <iostream>
int main ()
{
cout << "Hello World! "; // prints Hello World!
cout << "I'm a C++ program"; // prints I'm a
C++ program
return 0;
}
Output:
Hello World! I'm a C++ program
C++ Keywords
•This is a list of reserved keywords in C++. Since they
are used by the language, these keywords are not
available for use by programmers.
auto const double float int short struct unsigned
unsigned break break continue continue else else for
for long long signed signed switch void case default
enum goto register sizeof typedef volatile char do
extern if return static union while ,ETC.
•You cannot define variables, functions, or classes that
have these keywords as their names.
Operators in C++
•An operator is a symbol that causes the compiler to take
an action. Operators act on operands, and in C++ all
operands are expressions.
•Types of operators:
•Assignment operator: x = a + b;
•Mathematical operator: +, -, *, /, %
• Integer Division : 21 / 5 = 4
• Modulus : Gives the remainder. 21 % 4 = 1
Data types in C++
•A formal description of what kind of data its value is.
Data types in C++ (Cont.)
Variable declaration
•A variable is a named location in memory.
•may contain numbers, letters, and underscores(_), and
may not start with a number.
•Write the data type first, then the variable name:
int a;
char ch = ‘a’ ;
float x , y= 5.2 ; //Separated by a comma
•Clarity and readability. Relevance. Verbs for functions.
line, savingsAccount , getName()
•Constants are all in uppercase.
float PI = 3.141592653;
Example:
# include <iostream.h>
int main ()
{
int x;
x = 4+2;
cout << x / 3 <<' '<< x * 2;
return 0;
}
Output:
2 12
Example:
#include <iostream.h>
int main ()
{
// declaring variables:
int a, b, result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;
cout << result; // print out the result:
return 0; // terminate the program:
}
Output:
4
Constants
•Constants are expressions with a fixed value.
#define identifier value
#define PI 3.14159
•
Example:
// defined constants: calculate circumference
#include <iostream.h>
#define PI 3.14159
#define NEWLINE '\n'
int main ()
{
double r = 5.0; // radius
double circle;
circle = 2 * PI * r;
cout << circle;
cout << NEWLINE;
return 0;
}
Output:
31.4159
Input
•cin>> is the syntax for inputting values.
Example:
Output:
Please enter an integer value: 702
The value you entered is 702 and its double is
1404.