0% found this document useful (0 votes)
12 views21 pages

Lecture3 Week 2

The document provides an introduction to C++, including its history and key features. It was developed in the late 1970s as an extension of C by Bjarne Stroustrup. C++ allows programmers to have high-level control over system resources and memory. It also supports object-oriented programming concepts like inheritance and polymorphism. The document discusses various C++ data types including integers, floats, characters, strings, and Boolean values.

Uploaded by

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

Lecture3 Week 2

The document provides an introduction to C++, including its history and key features. It was developed in the late 1970s as an extension of C by Bjarne Stroustrup. C++ allows programmers to have high-level control over system resources and memory. It also supports object-oriented programming concepts like inheritance and polymorphism. The document discusses various C++ data types including integers, floats, characters, strings, and Boolean values.

Uploaded by

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

Lecture 3 SP-22 (CS/SE/AI)

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

C++ has built-in support for 3 major data types:


 Numerical, Alphabetic, and Logical
o Numerical: Integers and floating point
o Alphabetic: character (and string)
o Logical: true or false
 These can be used to save information of the given type by setting aside
a memory location to store the information under a user defined name
Numerical Data
Two broad categories
Integers store “whole” numbers
 No fractions or decimals
 positive and negative “counting” numbers, incl. zero
 Three types: short, int, long

Floating point numbers


 “Fractional” numbers (i.e. those with decimal points)
 Two main types: float and double
 Can be represented in either decimal or scientific notation
o Decimal: 3.14159
o Scientific 2.75e3 (mantissa and exponent) – same as 2.73x10 3
Alphabetic Data
Character data
 Store a single alphanumeric character
 Type is char
o characters are represented in single quotes: ‘x’, ‘A’, or ‘=‘

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

type name[=value], [name2=value2], …;

 Multiple variables can be assigned in one statement


o Separated with commas
Valid variable names
Rules
 Can only contain letters, numbers, and underscore (_)
o Good: name, x, abc123, last_name
o Bad: last-name
 Cannot begin with a number
o Bad: 1letter
 Cannot be a C++ reserved word
o Bad: float, int, main
 Is case-sensitive
o Name is not the same as name
Variable Declaration Examples
int i; // variable (no initialization)
int j, k, l; // multiple variables
int n=10; // variable with initialization
float x, pi= 3.14159; // init and no init
char a= ’A’;
string name= ”John”;
bool maybe= true;
const qualifier
Variables (as the name implies) store information that can be
changed in a program
Occasionally, it is desirable to disallow changes to stored information
 Use keyword const
const type name=value, name2=value2, …

Error message if an attempt is made to change a const variable


 const variables must be initialized on declaration (since they can’t be changed)
const float pi=3.14159;
Compiler Directive
Another way to define a fixed (constant) value
Syntax:
#define NAME value
Example:
#define PI 3.14159
Note – there is NO semicolon “;” after the declaration
 Because this is actually interpreted in a pre-compilation step
o “NAME” gets replaced globally in the program before the actual compilation occurs
 Accepted practice is to capitalize compiler directives
Integers
Can store positive and negative integers
 No decimals!

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;

You might also like