Lecture3 Week 2
Lecture3 Week 2
Introduction to C++
User Inputs & Data Types
Instructor: Ms. Ayesha
11/05/2023 1
What is C++
C++ is a cross-platform language that can be used to
create high-performance applications.
C++ was developed by Bjarne Stroustrup, as an
extension to the C language.
C++ gives programmers a high level of control over
system resources and memory.
The language was updated 3 major times in 2011,
2014, and 2017 to C++11, C++14, and C++17.
11/05/2023 2
History
C++ was developed by Bjarne Stroustrup at Bell
Laboratories over a period starting in 1979. Since C++ is an
attempt to add object-oriented features (plus other
improvements) to C, earlier it was called as “C with Objects”. As
the language developed, Stroustrup named it as C++ in 1983.
11/05/2023 3
Why use C++
C++ is one of the world's most popular programming
languages.
C++ can be found in today's operating systems, Graphical
User Interfaces, and embedded systems.
C++ is an object-oriented programming language which gives
a clear structure to programs and allows code to be reused,
lowering development costs.
C++ allows exception handling, and function overloading which are
not possible in C. C++ is a powerful, efficient and fast language. It
finds a wide range of applications – from GUI applications to 3D
graphics for games to real-time mathematical simulations.
11/05/2023 4
Why use C++ (cont..)
C++ is portable and can be used to develop
applications that can be adapted to multiple platforms.
C++ is fun and easy to learn!
As C++ is close to C# and Java, it makes easy for
programmers to switch to C++ or vice versa
11/05/2023 5
What are the main features of OOPs?
• Inheritance: The capability of a class to derive properties and
characteristics from another class is called Inheritance
• Encapsulation: Encapsulation also leads to data abstraction or hiding.
• Polymorphism: Polymorphism is composed of two words - “poly” which
means “many”, and “morph” which means “shapes”. process by which
some code, data, method, or object behaves differently under different
circumstances or contexts.
• Data Abstraction: Abstraction means displaying only essential information
and hiding the details. Data abstraction refers to providing only essential
information about the data to the outside world, hiding the background
details or implementation.
11/05/2023 6
C++ format
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
11/05/2023 7
C++ Data Types
Data Type
A key capability of a programming language
Establishes both the type of information that can be stored as well as
operations that can be done on that information
Strings data
Store a “string” of characters, like a name
Type is string – specified in double quotes: “John” or “apple”
Not built-in, comes from a pre-defined library (distributed with C++)
o It is actually a class (which did not exist in the original C language)
o To use strings, must include the string library: #include <string>
Logical Data
Boolean
Named for George Boole
o English mathematician (mid 1800’s) – developed Boolean Algebra
Type is bool – used to represent conditional values
Supports only two values: true and false
o Note: true and false are not strings – no quotes!
Variables/Identifiers
To store information of a given type, a variable (identifier) of that
type must be declared
The type must be specified
A name for the variable must be declared
An initial value may, optionally, be assigned
Three types
short, int, long
o Usually representing different numerical ranges
o We will be using int primarily
Ranges are compiler dependent
o Generally: short <= int <= long
o On our system, int can store values between -2147483648 and 2147483647
o (approx. +/- 2 billion)
Floating Point Numbers
Stores numerical data with decimals
Two types
float and double
o We will be using float primarily
Ranges are compiler and system dependent - on ours:
o float can store pos and neg values between 1e-38 and 1e38 (approx.)
o float has 6 decimal digits of precision
o double has much greater range and precision
Literals and the assignment operator
Literal is a term used to describe an explicit, fixed data value.
As opposed to a value that is the result of a computation (like z=x+y;)
Any data type can be assigned a literal value
Using the assignment (=) operator – i.e. the equals operator
integer assignment
int i, j, k; // declare 3 integers
i= 10;
j= 0;
k= -23;
10, 0 and -23 are literals
Floating point Literals
Floating point data Scientific Notation
float x, y, z; // declare 3 floating point Two parts
variables
Mantissa – the decimal number
x= 3.14159; // decimal notation before the “e”
y= 3.2e5; // scientific notation Exponent – the characteristic
z= -15e-10; (exponent) is the integer after
Numerical values above are floating the “e”
point literals
number= mantissa x 10characteristic
Literals cont.
char • string
A single character enclosed in • One or more characters enclosed in
single quotes double quotes
Examples • Examples
char a, b, c; // declare 3 chars string name; // declare a string
a= ‘A’; name= “John”;
b= ‘v’; “John” is a string literal
c= ‘?’; • bool
‘A’, ‘v’, and “?” are character literals • true or false (no quotes!)
bool maybe=true;