SlideShare a Scribd company logo
Introduction to C++
(CS1123)
By
Dr. Muhammad Aleem,
Department of Computer Science,
Mohammad Ali Jinnah University, Islamabad
Fall 2013
History
• C evolved from two languages (BCPL and B)
• 1980: “C with Classes”
• 1985: C++ 1.0
• 1995: Draft standard
• Developed by Bjarne Stroustrup at Bell Labs
• Based on C, added Object-Oriented
Programming concepts (OOP) in C
• Similar program performance (compared to C)
C vs C++
• Advantages:
– 1. Faster development time (code reuse)
– 2. Creating / using new data types is easier
– 3. Easier memory management
– 4. Stricter syntax & type checking => less bugs
– 5. Easier to implement Data hiding
– 6. Object Oriented concepts
C++ Program Structure
//My first program in C++ First.cpp
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
Preprocessor
Directive (no ;)
IDE and Compilation Steps
C++
Preprocessor
First.cpp
C++ Compiler Linker First.exe
C++ Header
Files
Object code for
library function
Integrated Development Environments (IDEs)
• An IDE is a software application which provides a
comprehensive facility to programmers to Write /Edit
/Debug /Compile the code
• Main components:
–Source code editor
–Debugger
–Complier
–…
IDEs on Windows platform
• Turbo C++
• Microsoft Visual C++
• Dev C++
Input / Output Example
#include <iostream>
#include <string>
using namespace std;
int main ( )
{
string name; //Name of student
cout<< “Enter you name";
cin>>name;
/* Now print hello , and students name */
cout<< “Hello “ << name;
return 0;
}
Comments
• Two types of comments
1. Single line comment //….
2. Multi-line (paragraph) comment /* */
• When compiler sees // it ignores all text after this on
same line
• When it sees /*, it scans for the text */ and ignores
any text between /* and */
Preprocessor Directives
• #include<iostream>
• # is a preprocessor directive.
• The preprocessor runs before the actual compiler and
prepares your program for compilation.
• Lines starting with # are directives to preprocessor to
perform certain tasks, e.g., “include” command
instructs the preprocessor to add the iostream library
in this program
Header files (functionality declarations)
(Turbo C++) (Visual C++)
• #include<iostream.h> or #include <iostream>
• #include<stdlib.h> or #include<stdlib>
• …
std:: Prefix
• std::cout<“Hello World”;
• std::cout<<Marks;
• std::cout<<“Hello ”<<name;
• Scope Resolution Operator ::
• std is a namespace, Namespaces ?
using namespace std;
cout<<“Hello World”;
cout<<Marks;
cout<<“Hello ”<<name;
Namespaces
• Namespace pollution
– Occurs when building large systems from pieces
– Identical globally-visible names clash
– How many programs have a “print” function?
– Very difficult to fix
Namespaces
namespace Mine
{
const float pi = 3.1415925635;
}
void main(){
float x = 6 + Mine::pi;
cout<<x;
}
Multiple Namespaces
• Example…
Omitting std:: prefix
- using directive brings namespaces or its sub-items
into current scope
#include<iostream>
using namespace std;
int main()
{
cout<<“Hello World!”<<endl;
cout<<“Bye!”;
return 0;
}
main( ) function
• Every C++ program start executing from main ( )
• A function is a construct that contains/encapsulates
statements in a block.
• Block starts from “{“ and ends with “}” brace
• Every statement in the block must end with a
semicolon ( ; )
• Examples…
cout and endl
• cout (console output) and the operator
• << referred to as the stream insertion operator
• << “Inserts values of data-items or string to
screen.”
• >> referred as stream extraction operator, extracts
value from stream and puts into “Variables”
• A string must be enclosed in quotation marks.
• endl stands for end line, sending ‘endl’ to the
console outputs a new line
Input and type
–cin>>name; reads characters until a
whitespace character is seen
–Whitespace characters:
• space,
• tab,
• newline {enter key}
Variables
- Variables are identifiers which represent
some unknown, or variable-value.
- A variable is named storage (some
memory address’s contents)
x = a + b;
Speed_Limit = 90;
Variable declaration
TYPE <Variable Name> ;
Examples:
int marks;
double Pi;
int suM;
char grade;
- NOTE: Variable names are case sensitive in C++ ??
Variable declaration
• C++ is case sensitive
–Example:
area
Area
AREA
ArEa
are all seen as different variables
Names
Valid Names
• Start with a letter
• Contains letters
• Contains digits
• Contains underscores
• Do not start names with underscores: _age
• Don’t use C++ Reserve Words
C++ Reserve Words
• auto break
• case char
• const continue
• default do
• double else
• enum extern
• float for
• goto if
int long
register return
short
signed
sizeof static
struct switch
typedef union
unsigned void
volatile while
Names
• Choose meaningful names
– Don’t use abbreviations and acronyms: mtbf, TLA,
myw, nbv
• Don't use overly long names
• Ok:
partial_sum
element_count
staple_partition
• Too long (valid but not good practice):
remaining_free_slots_in_the_symbol_table
Which are Legal Identifiers?
AREA
2D
Last Chance
x_yt3
Num-2
Grade***
area_under_the_curve
_Marks
#values
areaoFCirCLe
%done
return
Ifstatement
String input (Variables)
// Read first and second name
#include<iostream>
#include<string>
int main() {
string first;
string second;
cout << “Enter your first and second names:";
cin >> first >> second;
cout << "Hello “ << first << “ “ << second;
return 0;
}
Declaring Variables
• Before using you must declare the variables
Declaring Variables…
• When we declare a variable, what happens ?
– Memory allocation
• How much memory (data type)
– Memory associated with a name (variable name)
– The allocated space has a unique address
Marks
FE07
%$^%$%$*^%int Marks;
Using Variables: Initialization
• Variables may be given initial values, or
initialized, when declared. Examples:
int length = 7 ;
float diameter = 5.9 ;
char initial = ‘A’ ;
7
5.9
‘A’
length
diameter
initial
• Are the two occurrences of “a” in this expression the
same?
a = a + 1;
One on the left of the assignment refers to the location
of the variable (whose name is a, or address);
One on the right of the assignment refers to the value of
the variable (whose name is a);
• Two attributes of variables lvalue and rvalue
• The lvalue of a variable is its address
• The rvalue of a variable is its value
rvalue and lvalue
• Rule: On the left side of an assignment there
must be a lvalue or a variable (address of
memory location)
int i, j;
i = 7;
7 = i;
j * 4 = 7;
rvalue and lvalue
Data Types
Three basic PRE-DEFINED data types:
1. To store whole numbers
– int, long int, short int, unsigned int
2. To store real numbers
– float, double
3. Characters
– char
Types and literals
• Built-in types
– Boolean type
• bool
– Character types
• char
– Integer types
• int
–and short and
long
– Floating-point types
• double
–and float
• Standard-library types
– string
Literals
• Boolean: true, false
• Character literals
– 'a', 'x', '4', 'n', '$'
• Integer literals
– 0, 1, 123, -6,
• Floating point literals
– 1.2, 13.345, 0.3, -0.54,
• String literals
– "asdf”, “Helllo”, Pakistan”
Types
• C++ provides a set of types
– E.g. bool, char, int, double called “built-in types”
• C++ programmers can define new types
– Called “user-defined types”
• The C++ standard library provides a set of types
– E.g. string, vector, ..
– (for vector type  #include<vector> )
Declaration and initialization
int a = 7;
int b = 9;
char c = 'a';
double x = 1.2;
string s1 = "Hello, world";
string s2 = "1.2";
7
9
a
1.2
Hello, world
1.2
char type
• Reserves 8 bits or 1 byte of memory
• A char variable may represent:
– ASCII character 'A‘, 'a‘, '1‘, '4‘, '*‘
– signed integers 127 to -128 (Default)
– unsigned integer in range 255 to 0
Examples:
–char grade;
–unsigned char WeekNumber= 200;
–char cGradeA = 65;
–char cGradeAA = ‘A';
char type
• Example program…
Special characters
• Text string special characters (Escape Sequences)
– n = newline
– r = carriage return
– t = tab
– " = double quote
– ? = question
–  = backslash
– ' = single quote
• Examples:
cout << "Hellot" << "I’m Bobn";
cout << "123nabc “;
Escape Sequence
• Example Program:
int type
• 16 bits (2 bytes) on Windows 16-bits
– int -32,768 to 32,767
– unsigned int 0 to 65,535
– Also on Turbo C++, 2 bytes for int
• 32 bits (4 bytes) on Win32 (Visual C++)
– int -2,147,483,648 to 2,147,483,647
– unsigned int 0 to 4,294,967,295
int type
• Examples:
int earth_diameter;
int seconds_in_week= 604800;
unsigned int Height = 100;
unsigned int Width = 50000;
int type (long and short)
• long int
–reserves 32 bits (4 bytes) of memory
–signed long -2,147,483,648 to 2,147,483,647
–unsigned long int 0 to 4,294,967,295
• short int
–reserves 16 bits (2 bytes) of memory
–signed short int -32,768 to 32,767
–unsigned short int 0 to 65,535
int (long and short)
• Examples:
long int light_speed=186000;
unsigned long int seconds= 604800;
short int Height = 30432;
unsigned short int Width = 50000;
Home Work-1
• Use Visual C++ on Windows and get information
for following data types:
– int
– short
– long int
– short int
– char
• Use ( cout << sizeof( intVar ); ) operator to get
this information, Example:…
Real Values
• float
– Reserves 32 bits (4 bytes) of memory
– + 1.180000x10
+38
, 7-digit precision
– Example: float radius= 33.4221;
• double
– Reserves 64 bits (8 bytes) of memory
– + 1.79000000000000x10
+308
, 15-digit precision
– Example: double Distance = 257.5434342;
• long double
– Reserves 80 bits (10 bytes) of memory , 18-digit precision
– Example: long double EarthMass = 25343427.53434233;
Home Work-2
• Use Visual C++ on Windows and get information
for following data types:
– float
– double
– long double
• Use ( cout << sizeof(floatVar); ) operator to get
this information, Example:…
bool Type
• Only 1 bit of memory required
– Generally, 1 byte because is reserved
• Literal values:
– true
– false
• Can be used in logical conditions:
– Examples:
bool RainToday=false;
bool passed;
passed = GetResult(80);
string type
• Special data type supports working with “strings”
#include <string>
string <variable_name> = “string literal”;
• string type variables in programs:
string firstName, lastName;
• Using with assignment operator:
firstName = “Mohammad";
lastName = “Ali";
• Display using cout
cout << firstName << " " << lastName;
Getting input in string with Spaces
string s1;
cin>> s1; //Spaces will not be input in s1
//Following statements read spaces in “string”
string s1;
getline(cin, s1); //Spaces will be input in s1
string type
• Find Errors in the example program…
Ad

More Related Content

What's hot (20)

Extracting text from PDF (iOS)
Extracting text from PDF (iOS)Extracting text from PDF (iOS)
Extracting text from PDF (iOS)
Kaz Yoshikawa
 
Python programming
Python programmingPython programming
Python programming
Prof. Dr. K. Adisesha
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
AkramWaseem
 
Acm aleppo cpc training eighth session
Acm aleppo cpc training eighth sessionAcm aleppo cpc training eighth session
Acm aleppo cpc training eighth session
Ahmad Bashar Eter
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
Srinivas Narasegouda
 
Shared Memory Parallelism with Python by Dr.-Ing Mike Muller
Shared Memory Parallelism with Python by Dr.-Ing Mike MullerShared Memory Parallelism with Python by Dr.-Ing Mike Muller
Shared Memory Parallelism with Python by Dr.-Ing Mike Muller
PyData
 
Programming using c++ tool
Programming using c++ toolProgramming using c++ tool
Programming using c++ tool
Abdullah Jan
 
Cython - close to metal Python
Cython - close to metal PythonCython - close to metal Python
Cython - close to metal Python
Taras Lyapun
 
Python programming introduction
Python programming introductionPython programming introduction
Python programming introduction
Siddique Ibrahim
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
NishantKumar1179
 
Format String Vulnerability
Format String VulnerabilityFormat String Vulnerability
Format String Vulnerability
Jian-Yu Li
 
Python basics
Python basicsPython basics
Python basics
RANAALIMAJEEDRAJPUT
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data science
deepak teja
 
SunPy: Python for solar physics
SunPy: Python for solar physicsSunPy: Python for solar physics
SunPy: Python for solar physics
segfaulthunter
 
C tutorials
C tutorialsC tutorials
C tutorials
Amit Kapoor
 
streams and files
 streams and files streams and files
streams and files
Mariam Butt
 
What is Python?
What is Python?What is Python?
What is Python?
wesley chun
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
Data file handling
Data file handlingData file handling
Data file handling
Prof. Dr. K. Adisesha
 
Extracting text from PDF (iOS)
Extracting text from PDF (iOS)Extracting text from PDF (iOS)
Extracting text from PDF (iOS)
Kaz Yoshikawa
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
Acm aleppo cpc training eighth session
Acm aleppo cpc training eighth sessionAcm aleppo cpc training eighth session
Acm aleppo cpc training eighth session
Ahmad Bashar Eter
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
Srinivas Narasegouda
 
Shared Memory Parallelism with Python by Dr.-Ing Mike Muller
Shared Memory Parallelism with Python by Dr.-Ing Mike MullerShared Memory Parallelism with Python by Dr.-Ing Mike Muller
Shared Memory Parallelism with Python by Dr.-Ing Mike Muller
PyData
 
Programming using c++ tool
Programming using c++ toolProgramming using c++ tool
Programming using c++ tool
Abdullah Jan
 
Cython - close to metal Python
Cython - close to metal PythonCython - close to metal Python
Cython - close to metal Python
Taras Lyapun
 
Python programming introduction
Python programming introductionPython programming introduction
Python programming introduction
Siddique Ibrahim
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
NishantKumar1179
 
Format String Vulnerability
Format String VulnerabilityFormat String Vulnerability
Format String Vulnerability
Jian-Yu Li
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data science
deepak teja
 
SunPy: Python for solar physics
SunPy: Python for solar physicsSunPy: Python for solar physics
SunPy: Python for solar physics
segfaulthunter
 
streams and files
 streams and files streams and files
streams and files
Mariam Butt
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
Amira ElSharkawy
 

Viewers also liked (15)

Cs1123 1 intro
Cs1123 1 introCs1123 1 intro
Cs1123 1 intro
TAlha MAlik
 
Cs1123 9 strings
Cs1123 9 stringsCs1123 9 strings
Cs1123 9 strings
TAlha MAlik
 
Anness publishing interview questions and answers
Anness publishing interview questions and answersAnness publishing interview questions and answers
Anness publishing interview questions and answers
jakebrown118
 
Anglian home improvements group interview questions and answers
Anglian home improvements group interview questions and answersAnglian home improvements group interview questions and answers
Anglian home improvements group interview questions and answers
jakebrown118
 
Pdhpe presentation
Pdhpe presentationPdhpe presentation
Pdhpe presentation
deana1994
 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
TAlha MAlik
 
Analox interview questions and answers
Analox interview questions and answersAnalox interview questions and answers
Analox interview questions and answers
jakebrown118
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
TAlha MAlik
 
Data file handling
Data file handlingData file handling
Data file handling
TAlha MAlik
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
TAlha MAlik
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
TAlha MAlik
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
TAlha MAlik
 
Cs1123 4 variables_constants
Cs1123 4 variables_constantsCs1123 4 variables_constants
Cs1123 4 variables_constants
TAlha MAlik
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
TAlha MAlik
 
Cs1123 2 comp_prog
Cs1123 2 comp_progCs1123 2 comp_prog
Cs1123 2 comp_prog
TAlha MAlik
 
Cs1123 9 strings
Cs1123 9 stringsCs1123 9 strings
Cs1123 9 strings
TAlha MAlik
 
Anness publishing interview questions and answers
Anness publishing interview questions and answersAnness publishing interview questions and answers
Anness publishing interview questions and answers
jakebrown118
 
Anglian home improvements group interview questions and answers
Anglian home improvements group interview questions and answersAnglian home improvements group interview questions and answers
Anglian home improvements group interview questions and answers
jakebrown118
 
Pdhpe presentation
Pdhpe presentationPdhpe presentation
Pdhpe presentation
deana1994
 
Analox interview questions and answers
Analox interview questions and answersAnalox interview questions and answers
Analox interview questions and answers
jakebrown118
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
TAlha MAlik
 
Data file handling
Data file handlingData file handling
Data file handling
TAlha MAlik
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
TAlha MAlik
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
TAlha MAlik
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
TAlha MAlik
 
Cs1123 4 variables_constants
Cs1123 4 variables_constantsCs1123 4 variables_constants
Cs1123 4 variables_constants
TAlha MAlik
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
TAlha MAlik
 
Cs1123 2 comp_prog
Cs1123 2 comp_progCs1123 2 comp_prog
Cs1123 2 comp_prog
TAlha MAlik
 
Ad

Similar to Cs1123 11 pointers (20)

C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
Mohammad Shaker
 
Modern C++
Modern C++Modern C++
Modern C++
Michael Clark
 
Abhishek lingineni
Abhishek lingineniAbhishek lingineni
Abhishek lingineni
abhishekl404
 
CPlusPus
CPlusPusCPlusPus
CPlusPus
rasen58
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
Dikshithkumar10
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
JayarAlejo
 
C++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.pptC++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.ppt
yp02
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
JayarAlejo
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
Infotech27
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
EPORI
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
TeacherOnat
 
C cpluplus 2
C cpluplus 2C cpluplus 2
C cpluplus 2
sanya6900
 
Return of c++
Return of c++Return of c++
Return of c++
Yongwei Wu
 
Programming in C [Module One]
Programming in C [Module One]Programming in C [Module One]
Programming in C [Module One]
Abhishek Sinha
 
cs8251 unit 1 ppt
cs8251 unit 1 pptcs8251 unit 1 ppt
cs8251 unit 1 ppt
praveenaprakasam
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
Sikder Tahsin Al-Amin
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++
kinan keshkeh
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
ssuser0c24d5
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
nilesh405711
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
YashpalYadav46
 
Abhishek lingineni
Abhishek lingineniAbhishek lingineni
Abhishek lingineni
abhishekl404
 
CPlusPus
CPlusPusCPlusPus
CPlusPus
rasen58
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
JayarAlejo
 
C++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.pptC++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.ppt
yp02
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
JayarAlejo
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
Infotech27
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
EPORI
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
TeacherOnat
 
C cpluplus 2
C cpluplus 2C cpluplus 2
C cpluplus 2
sanya6900
 
Programming in C [Module One]
Programming in C [Module One]Programming in C [Module One]
Programming in C [Module One]
Abhishek Sinha
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++
kinan keshkeh
 
Ad

Recently uploaded (20)

Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Artificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptxArtificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptx
03ANMOLCHAURASIYA
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Artificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptxArtificial_Intelligence_in_Everyday_Life.pptx
Artificial_Intelligence_in_Everyday_Life.pptx
03ANMOLCHAURASIYA
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 

Cs1123 11 pointers

  • 1. Introduction to C++ (CS1123) By Dr. Muhammad Aleem, Department of Computer Science, Mohammad Ali Jinnah University, Islamabad Fall 2013
  • 2. History • C evolved from two languages (BCPL and B) • 1980: “C with Classes” • 1985: C++ 1.0 • 1995: Draft standard • Developed by Bjarne Stroustrup at Bell Labs • Based on C, added Object-Oriented Programming concepts (OOP) in C • Similar program performance (compared to C)
  • 3. C vs C++ • Advantages: – 1. Faster development time (code reuse) – 2. Creating / using new data types is easier – 3. Easier memory management – 4. Stricter syntax & type checking => less bugs – 5. Easier to implement Data hiding – 6. Object Oriented concepts
  • 4. C++ Program Structure //My first program in C++ First.cpp #include <iostream> using namespace std; int main () { cout << "Hello World!"; return 0; } Preprocessor Directive (no ;)
  • 5. IDE and Compilation Steps C++ Preprocessor First.cpp C++ Compiler Linker First.exe C++ Header Files Object code for library function
  • 6. Integrated Development Environments (IDEs) • An IDE is a software application which provides a comprehensive facility to programmers to Write /Edit /Debug /Compile the code • Main components: –Source code editor –Debugger –Complier –…
  • 7. IDEs on Windows platform • Turbo C++ • Microsoft Visual C++ • Dev C++
  • 8. Input / Output Example #include <iostream> #include <string> using namespace std; int main ( ) { string name; //Name of student cout<< “Enter you name"; cin>>name; /* Now print hello , and students name */ cout<< “Hello “ << name; return 0; }
  • 9. Comments • Two types of comments 1. Single line comment //…. 2. Multi-line (paragraph) comment /* */ • When compiler sees // it ignores all text after this on same line • When it sees /*, it scans for the text */ and ignores any text between /* and */
  • 10. Preprocessor Directives • #include<iostream> • # is a preprocessor directive. • The preprocessor runs before the actual compiler and prepares your program for compilation. • Lines starting with # are directives to preprocessor to perform certain tasks, e.g., “include” command instructs the preprocessor to add the iostream library in this program
  • 11. Header files (functionality declarations) (Turbo C++) (Visual C++) • #include<iostream.h> or #include <iostream> • #include<stdlib.h> or #include<stdlib> • …
  • 12. std:: Prefix • std::cout<“Hello World”; • std::cout<<Marks; • std::cout<<“Hello ”<<name; • Scope Resolution Operator :: • std is a namespace, Namespaces ? using namespace std; cout<<“Hello World”; cout<<Marks; cout<<“Hello ”<<name;
  • 13. Namespaces • Namespace pollution – Occurs when building large systems from pieces – Identical globally-visible names clash – How many programs have a “print” function? – Very difficult to fix
  • 14. Namespaces namespace Mine { const float pi = 3.1415925635; } void main(){ float x = 6 + Mine::pi; cout<<x; }
  • 16. Omitting std:: prefix - using directive brings namespaces or its sub-items into current scope #include<iostream> using namespace std; int main() { cout<<“Hello World!”<<endl; cout<<“Bye!”; return 0; }
  • 17. main( ) function • Every C++ program start executing from main ( ) • A function is a construct that contains/encapsulates statements in a block. • Block starts from “{“ and ends with “}” brace • Every statement in the block must end with a semicolon ( ; ) • Examples…
  • 18. cout and endl • cout (console output) and the operator • << referred to as the stream insertion operator • << “Inserts values of data-items or string to screen.” • >> referred as stream extraction operator, extracts value from stream and puts into “Variables” • A string must be enclosed in quotation marks. • endl stands for end line, sending ‘endl’ to the console outputs a new line
  • 19. Input and type –cin>>name; reads characters until a whitespace character is seen –Whitespace characters: • space, • tab, • newline {enter key}
  • 20. Variables - Variables are identifiers which represent some unknown, or variable-value. - A variable is named storage (some memory address’s contents) x = a + b; Speed_Limit = 90;
  • 21. Variable declaration TYPE <Variable Name> ; Examples: int marks; double Pi; int suM; char grade; - NOTE: Variable names are case sensitive in C++ ??
  • 22. Variable declaration • C++ is case sensitive –Example: area Area AREA ArEa are all seen as different variables
  • 23. Names Valid Names • Start with a letter • Contains letters • Contains digits • Contains underscores • Do not start names with underscores: _age • Don’t use C++ Reserve Words
  • 24. C++ Reserve Words • auto break • case char • const continue • default do • double else • enum extern • float for • goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while
  • 25. Names • Choose meaningful names – Don’t use abbreviations and acronyms: mtbf, TLA, myw, nbv • Don't use overly long names • Ok: partial_sum element_count staple_partition • Too long (valid but not good practice): remaining_free_slots_in_the_symbol_table
  • 26. Which are Legal Identifiers? AREA 2D Last Chance x_yt3 Num-2 Grade*** area_under_the_curve _Marks #values areaoFCirCLe %done return Ifstatement
  • 27. String input (Variables) // Read first and second name #include<iostream> #include<string> int main() { string first; string second; cout << “Enter your first and second names:"; cin >> first >> second; cout << "Hello “ << first << “ “ << second; return 0; }
  • 28. Declaring Variables • Before using you must declare the variables
  • 29. Declaring Variables… • When we declare a variable, what happens ? – Memory allocation • How much memory (data type) – Memory associated with a name (variable name) – The allocated space has a unique address Marks FE07 %$^%$%$*^%int Marks;
  • 30. Using Variables: Initialization • Variables may be given initial values, or initialized, when declared. Examples: int length = 7 ; float diameter = 5.9 ; char initial = ‘A’ ; 7 5.9 ‘A’ length diameter initial
  • 31. • Are the two occurrences of “a” in this expression the same? a = a + 1; One on the left of the assignment refers to the location of the variable (whose name is a, or address); One on the right of the assignment refers to the value of the variable (whose name is a); • Two attributes of variables lvalue and rvalue • The lvalue of a variable is its address • The rvalue of a variable is its value rvalue and lvalue
  • 32. • Rule: On the left side of an assignment there must be a lvalue or a variable (address of memory location) int i, j; i = 7; 7 = i; j * 4 = 7; rvalue and lvalue
  • 33. Data Types Three basic PRE-DEFINED data types: 1. To store whole numbers – int, long int, short int, unsigned int 2. To store real numbers – float, double 3. Characters – char
  • 34. Types and literals • Built-in types – Boolean type • bool – Character types • char – Integer types • int –and short and long – Floating-point types • double –and float • Standard-library types – string Literals • Boolean: true, false • Character literals – 'a', 'x', '4', 'n', '$' • Integer literals – 0, 1, 123, -6, • Floating point literals – 1.2, 13.345, 0.3, -0.54, • String literals – "asdf”, “Helllo”, Pakistan”
  • 35. Types • C++ provides a set of types – E.g. bool, char, int, double called “built-in types” • C++ programmers can define new types – Called “user-defined types” • The C++ standard library provides a set of types – E.g. string, vector, .. – (for vector type  #include<vector> )
  • 36. Declaration and initialization int a = 7; int b = 9; char c = 'a'; double x = 1.2; string s1 = "Hello, world"; string s2 = "1.2"; 7 9 a 1.2 Hello, world 1.2
  • 37. char type • Reserves 8 bits or 1 byte of memory • A char variable may represent: – ASCII character 'A‘, 'a‘, '1‘, '4‘, '*‘ – signed integers 127 to -128 (Default) – unsigned integer in range 255 to 0 Examples: –char grade; –unsigned char WeekNumber= 200; –char cGradeA = 65; –char cGradeAA = ‘A';
  • 38. char type • Example program…
  • 39. Special characters • Text string special characters (Escape Sequences) – n = newline – r = carriage return – t = tab – " = double quote – ? = question – = backslash – ' = single quote • Examples: cout << "Hellot" << "I’m Bobn"; cout << "123nabc “;
  • 41. int type • 16 bits (2 bytes) on Windows 16-bits – int -32,768 to 32,767 – unsigned int 0 to 65,535 – Also on Turbo C++, 2 bytes for int • 32 bits (4 bytes) on Win32 (Visual C++) – int -2,147,483,648 to 2,147,483,647 – unsigned int 0 to 4,294,967,295
  • 42. int type • Examples: int earth_diameter; int seconds_in_week= 604800; unsigned int Height = 100; unsigned int Width = 50000;
  • 43. int type (long and short) • long int –reserves 32 bits (4 bytes) of memory –signed long -2,147,483,648 to 2,147,483,647 –unsigned long int 0 to 4,294,967,295 • short int –reserves 16 bits (2 bytes) of memory –signed short int -32,768 to 32,767 –unsigned short int 0 to 65,535
  • 44. int (long and short) • Examples: long int light_speed=186000; unsigned long int seconds= 604800; short int Height = 30432; unsigned short int Width = 50000;
  • 45. Home Work-1 • Use Visual C++ on Windows and get information for following data types: – int – short – long int – short int – char • Use ( cout << sizeof( intVar ); ) operator to get this information, Example:…
  • 46. Real Values • float – Reserves 32 bits (4 bytes) of memory – + 1.180000x10 +38 , 7-digit precision – Example: float radius= 33.4221; • double – Reserves 64 bits (8 bytes) of memory – + 1.79000000000000x10 +308 , 15-digit precision – Example: double Distance = 257.5434342; • long double – Reserves 80 bits (10 bytes) of memory , 18-digit precision – Example: long double EarthMass = 25343427.53434233;
  • 47. Home Work-2 • Use Visual C++ on Windows and get information for following data types: – float – double – long double • Use ( cout << sizeof(floatVar); ) operator to get this information, Example:…
  • 48. bool Type • Only 1 bit of memory required – Generally, 1 byte because is reserved • Literal values: – true – false • Can be used in logical conditions: – Examples: bool RainToday=false; bool passed; passed = GetResult(80);
  • 49. string type • Special data type supports working with “strings” #include <string> string <variable_name> = “string literal”; • string type variables in programs: string firstName, lastName; • Using with assignment operator: firstName = “Mohammad"; lastName = “Ali"; • Display using cout cout << firstName << " " << lastName;
  • 50. Getting input in string with Spaces string s1; cin>> s1; //Spaces will not be input in s1 //Following statements read spaces in “string” string s1; getline(cin, s1); //Spaces will be input in s1
  • 51. string type • Find Errors in the example program…
  翻译: