03 IntroductionToC++
03 IntroductionToC++
TO C++
UCCD1004 Programming Concepts and Practices
Life of a C++ Program
Step 1 – 3
int main(void) {
auto answer = 1 + 3;
cout << answer << endl; //Since C++11
cout << functionA(4) << endl;
return 0;
}
Programming Steps
It is a problem solving process.
• There are many other usages (we will see them along the way).
Namespace
• Namespaces are used to organize code into logical groups and
to prevent name collisions that can occur especially when your
code base includes multiple libraries.
Bjarne Stroustrup says it is not and never has been C++, nor has it
even been C. Even if your compiler accepts "void main()" avoid
it, or risk being considered ignorant by C and C++ programmers.
http://www.stroustrup.com/bs_faq2.html#void-main
Statements vs. Expressions
• Every single line of code can be considered as a statement,
often end with “;”
• Example:
Please plus 1 with 2 then multiply with 3. Later divide the
answer with 3.
Comments
• Comments are portions of the code ignored by
the compiler which allow the user to make
simple notes in the relevant areas of the source
code.
int main(void) {
int x;
double y;
char z;
}
Defining Variables
• Variables of the same type can be defined
In separate statements
• int length;
• int width;
2-22
Named Constants
• Also called constant variables
3-23
Data Types
• In computing, data type is a particular kind of data item, as
defined by the values it can take, the programming language
used, or the operations that can be performed on it.
*https://www.geeksforgeeks.org/data-type-ranges-and-their-macros-in-c/
Primitive Data Type
• A float is a single precision, 32-bit floating-point data type that
accommodates seven digits.
2-30
% Operator
• C++ modulus operator (%) computes the remainder resulting
from integer division
cout << 9 % 2; // displays 1
2-31
HELLO WORLD
#include <iostream>
using namespace std;
int main(void) {
cout << "Hello World" << endl;
return 0;
}
Unary operator
• C++ also supports unary operators for example:-
i++
++i
--j
j--
Combined Assignment operators
• Operators: += -= *= /= %=
• Example:
sum += amt; is short for sum = sum + amt;
3-34
More Examples
x += 5; means x = x + 5;
x -= 5; means x = x – 5;
x *= 5; means x = x * 5;
x /= 5; means x = x / 5;
x %= 5; means x = x % 5;
The right hand side is evaluated before the
combined assignment operation is done.
x *= a + b; means x = x * (a + b);
3-35
Precedence
3-37
Using Mathematical Expressions
■ Can be used in assignment statements, with cout, and
in other types of statements
■ Examples: This is an
expression
area = 2 * PI * radius;
cout << "border is: " << (2*(l+w));
These are
expressions
3-38
Order of Operations
■ In an expression with > 1 operator, evaluate in this order
■ In the expression 2 + 2 * 2 – 2 ,
3-40
Algebraic Expressions
• Multiplication requires an operator
Area = lw is written as Area = l * w;
• There is no exponentiation operator
Area = s2 is written as Area = pow(s, 2);
(note: pow requires the cmath header file)
• Parentheses may be needed to maintain order of operations
y 2 y1
m
x 2 x1
is written as
m = (y2-y1)/(x2-x1);
3-41
Type conversion
• Implicit type coercion – when a value of one data type is
automatically changed to another data type.
int main(void) {
int x = 100.0/3.0;
cout << x << endl;
}
• Implicit type coercion can produce hard to debug code for novice
programmer.
Casting
• Explicit type conversion in C++ can be achieved using the
following syntax:-
int main(void) {
cout << static_cast<int> (7.9) << endl;
cout << static_cast<double> (7) << endl;
}
int main(void) {
char one = 'a';
char two = '2';
char three = ' ';
}
Casting a Character in Integer
• It is possible to cast a char into an integer, vice-versa.
• The integer is the representation of the char in number. Known as
ASCII.
• UTF8 is currently most popular character encoding method.
int main(void) {
char one = 'a';
char two = 'A';
cout << static_cast<int>(one) << endl; // 97
cout << static_cast<int>(two) << endl; // 65
}
String
• How about handling a word or a sentence? A word/sentence is an
array of characters.
• Use the string library.
• We will cover more about string library in coming lectures.
#include <iostream>
#include <string>
int main(void) {
string word1 = "Hello World";
cout << word1 << endl; //Hello
World
cout << word1[0] << endl; //H
cout << word1[1] << endl; //e
cout << word1[12] << endl;
//runtime error
}
String Constant
• Can be stored a series of characters in consecutive memory
locations
"Hello"
• Stored with the null terminator, \0, at end
• Is comprised of characters between the " "
H e l l o \0
[0] [4]
2-47
A character or a string constant?
• A character constant is a single character, enclosed in single quotes:
'C'
• A string constant is a sequence of characters enclosed in double
quotes:
"Hello, there!"
• A single character in double quotes is a string constant, not a
character constant:
"C"
2-48
Input and Output
int main(void) {
string name;
cout << "What is your name?" << endl;
cin >> name; //user enter John
cout << "Hi " << name << endl; //Hi John
}
What about the whitespace?
int main(void) {
string name;
cout << "What is your name?" << endl;
getline(cin, name); //Enter John
cout << "Hi " << name << endl; //Hi John
}