[go: up one dir, main page]

0% found this document useful (0 votes)
13 views30 pages

Chapter 2 - Basics of C++

C++

Uploaded by

Shafi Esa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views30 pages

Chapter 2 - Basics of C++

C++

Uploaded by

Shafi Esa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Exit Exam Tutorial

Part 1: Computer Programming


Episode 2: Origin and Basics of C++
1.2 Origin and Basics of C++
- The first major programming language was FORTRAN
(FORmula TRANslator).
- The world’s first programmer was Ada Lovelace++.
1.2 Origin and Basics of C++

C++ programming was developed by Bjarne Stroustrup in 1979.


1.2 Origin and Basics of C++
1.2.1 Features of C++
- C++ is a high-level programming language.
- C++ is an object-oriented and a block-structured
programming language.
1.2 Origin and Basics of C++
WHY SHOULD STUDY C++?
- The invention of C++ was necessitated by one major
programming factor-increasing complexity.
- C++ is efficient and used in operating systems, device
drivers, web servers, cloud-based applications, search
engines etc.
- C++ is often the language of choice for creating other
programming languages.
1.2 Origin and Basics of C++
1.2.2 Typical C++ Development Environment
- C++ programs typically go through 6 phases:
Creating a program

Preprocess

Compile

Link

Load

Execute
1.2 Origin and Basics of C++
1.2.3 C++ basic concepts
- C++ is object-oriented programming language.
- It is case-sensitive language, and the file extension of C++ is
.cpp.
- Examples of C++ IDE text editor: Code blocks, Dev C++,
Turbo C++, Microsoft Visual C++ 2010.

#include <iostream>
using namespace std;
int main() {
cout << “Hello World”;
return 0;
}
1.2 Origin and Basics of C++
- #include <iostream> : Directive pre-processor for
input-output stream library.
- using namespace std; : Elements of the C++ standard
library.
- int main() : Beginning and starting execution of the
main function.
- cout << “Hello World”; : Printing the statement on
the screen by adding the symbol (<<) or insertion stream
operator.
- return 0; : The program terminates/exits.
1.2 Origin and Basics of C++
1.2.4 Comments
- Comments are parts of the source code disregarded by the
compiler, C++ supports two ways to insert comments:
- // line comment: discards everything from where the pair of
slash signs (//) is found up to the end of the same line.
- /* block comment: discards everything between the /*
characters and the first appearance of the */ characters
with the possibility of including more than one line.
1.2 Origin and Basics of C++
1.2.5 Types of Errors in C++ Programming
- Syntax error: An error in the format of a statement in a
computer program that violates the rules of programming
language employed.
- Runtime error: It is after the program has successfully
compiled and is running, such runtime errors are abnormal
program termination, division by zero error, and overflow.
- Logical error: An error in programming that is, caused by
faulty reasoning, resulting in the program’s functioning
incorrectly if the instructions containing the error are
encountered.
1.2 Origin and Basics of C++
1.2.6 Variables and Data Types
- A variable is a symbol that represents a storage location in
the computer’s memory.
- A variable can be defined as a portion of memory to store a
determined value.
Variable = expression;
Variable_names = “Identifiers”;
- A valid identifier is a sequence of one or more letters, digits
or underscore symbols (_).
- Although for some compilers only the 32 first characters of
an identifier are significant.
- A valid identifier should begin with the letter or underscore.
- The keywords cannot be used as identifiers.
1.2 Origin and Basics of C++
1.2.7 Keywords
- Keywords are words whose meaning are defined to the
compiler, such like: asm, bool, break, case, class,
cast, continue, goto, try, catch and so on.
1.2 Origin and Basics of C++
1.2.8 Data Types
- When we program, we store the variables in our computer’s
memory, but the computer memory must know what we
want to store in them since it is not occupying the same
space in memory to store a simple number, letter or a larger
number.
- A byte can store a relatively small amount of data, usually
an integer between 0 and 255 or a single character.
- Standard C++ has 14 different fundamental types: 11
integral types and 3 floating-point types.
- The three floating-point types are float, double and long
double.
- The most frequently used fundamental types are bool,
char, int and double.
1.2 Origin and Basics of C++
1.2.8.1 Declaration of Variables
- To use variable in C++, we must declare the variable within
the data types above we want it to be.
- Example:
int a; // The variable a takes integer value.
int x,y,z; // The variables x, y and z take integer
value.
1.2 Origin and Basics of C++
#include <iostream>
using namespace std;
int main() {
char fname[20], lname[20];

cout << “Enter your first name “ << endl;


cin >> fname;
cout << “Enter your last name “ << endl;
cin >> lname;

cout << “Your full name is “ << fname << “ “


<< lname << endl;
return 0;
}
1.2 Origin and Basics of C++
Sample output:
1.2 Origin and Basics of C++
1.2.8.2 Constants
- Constant is any expression that has a fixed value. They can
be divided into:
 INTEGER CONSTANT: The simplest way to write a constant
is to write the integer number, appending for long integer
value. Example, 45, 23, 15L, 15l.
 FLOATING-POINT CONSTANT: For a floating number
constant, the types is always double. Example, 23.4f, 45.3F,
7.46l, 7.46L.
 CHARACTER CONSTANT: It is written in a single quotes.
Example, ‘A’, ‘B’, ‘a’, ‘b’.
 STRING CONSTANT: is a sequence of zero or more
characters enclosed in double quotes. Example, “Hello”,
“Welcome”.
1.2 Origin and Basics of C++
1.2.9 Operators
- Most programs perform arithmetic calculation, logical
calculation, assignment, bitwise and so on.
- ARITHMETIC OPERATOR: Addition (+), Subtraction (-),
Multiplication (*), Division (/), and Modulus (%).
- INCREMENT/DECREMENT OPERATOR: used to increase or
decrease by one the value stored in a variable. (++ or --).
- ASSIGNMENT OPERATOR: Modifying the value of the
variable by performing an operation on the value currently
stored in that variable.
- RELATIONAL OPERATOR: The comparison between two
expressions we can use relational and equality operators.
- LOGICAL OPERATOR: (!, && and ||) represents the
condition statements for Boolean expression.
1.2 Origin and Basics of C++
#include <iostream>
using namespace std;
int main() {
int a, b, sum, diff, mul, div, rem;

cout << “Enter the first number “ << endl;


cin >> a;
cout << “Enter the second number “ << endl;
cin >> b;
sum = a + b;
diff = a – b;
mul = a * b;
div = a / b;
rem = a % b;

cout << “The sum is “ << sum << endl;


cout << “The difference is “ << diff << endl;
cout << “The product is “ << mul << endl;
cout << “The quotient is “ << div << endl;
cout << “The remainder is “ << rem << endl;
return 0;
}
1.2 Origin and Basics of C++
Sample output:
1.2 Origin and Basics of C++
#include <iostream>
using namespace std;
int main() {
int a;

cout << “Enter the number “ << endl;


cin >> a;

cout << ++a << endl;


cout << a++ << endl;
cout << a-- << endl;
cout << --a << endl;
cout << a << endl;
return 0;
}
1.2 Origin and Basics of C++
Sample output:
1.2 Origin and Basics of C++
#include <iostream>
using namespace std;
int main() {
int a, b = 3;

a = b;
a += 2;
cout << a << endl;
return 0;
}
1.2 Origin and Basics of C++
Sample output:
1.2 Origin and Basics of C++
#include <iostream>
using namespace std;
int main() {
bool e, f;
int a = 5;
e = a > 5;
f = a == 5;
cout << e << endl;
cout << f << endl;
return 0;
}
1.2 Origin and Basics of C++
Sample output:

We set into the Boolean data type, if it is set


to 1, it results true. On the other hand, if it
is set to 0, it results false.
1.2 Origin and Basics of C++
#include <iostream>
using namespace std;
int main() {
int p;
int x = 10;
int y = 15;
bool a = true;
bool b = false;
p = (x || y) && !(a && b);
cout << p << endl;
return 0;
}
1.2 Origin and Basics of C++
Sample output:
1.2 Origin and Basics of C++
Special Thanks to the publisher and author with:
TOPICS AND THE CONCEPTS:
What is computer?
What is Programming?
Problem Solving Techniques
Algorithm
Program
Language translation programs
Major programming paradigms
Generation of programming languages

REFERENCES:
C++ Programming: From Problem Analysis to Program Design (D.S. Malik)
Fundamental of Programming in C++ (Walter J. Savitch)

PRESENTED BY:
Mohammed Nebil

HISTORY OF THE PROGRAMMING:


Dennis Ritchie
Bjarne Stroustrup
Anders Hejlsberg

SPECIAL THANKS:
Digital Library of Educations
Federal Democratic Republic of Ethiopia, Ministry of Educations
Ethiopian Education Short Note

You might also like