SlideShare a Scribd company logo
1 of 30
Computer Programming
Introduction to the course and C++
Prof. Dr. Mohammad Haseeb Zafar
haseeb@uetpeshawar.edu.pk
Course Overview
• Aims:
– to provide a good introduction to programming in C++
– to give increased familiarity with programming, use of an
‘integrated development environment’ (IDE) and software
development in general
– to deepen appreciation of object orientation
• Personnel
– lectures: Prof. Dr. Mohammad Haseeb Zafar
– labs: Engr. Maha Gul
Course format
• A weekly lecture introducing programming concepts and
examples
• From week 2, a weekly lab to practise the new ideas
– Practice of the ideas will be essential to learning enough to
pass the course
– You should complete the exercise in the lab
• People who fail to complete the labs tend to fail the course
• Don’t fall behind!
– You will need at least 2 hours
• Do not assume the lab is effectively free time
• You will not be able to simply copy and paste from the
lectures!
– You need to use the idea from the lecture
Recommended resources
• Books
– “C++ How to Program” by Deitel & Deitel
– “C++ programming in easy steps” by Mike McGrath
– “Thinking in C++” by Bruce Eckel available at
http://mindview.net/Books/TICPP/ThinkingInCPP2e.html#Con
– For the advanced programmer: “The C++ Programming
Language” by Bjarne Stroustrup, published by Addison
Wesley
• Web resources
– http://www.cplusplus.com/
– Stroustrup’s website:
http://www.research.att.com/~bs/C++.html
Assessment
• Sessionals: 25%
– Assignments: 6%
– Quizzes: 9%
– Project: 10%
• done in pairs in last 8 weeks of course
• Mid Term Exam: 25%
• Final Term Exam: 50%
Course content
• Introduction to the course, C++ and the IDE
• Data types and operators
• Functions
• Conditions (if…)
• Iteration (for loops, etc.)
• Arrays, Strings and Pointers
• Classes and objects
Why learn to program?
• You are likely to have to write some code some time
– to do accurate calculations quickly and repeatably
– to modify existing programs to reflect changed needs
– for processing of large amounts of data
• format changes, extraction of key information, …
– for actions on a programmable device
– to do your FYP
– to learn about algorithms and how to solve problems
Why learn C++?
• It is sort of an extension of C
– Lots of old code is in C – don’t want to throw it away!
• Lots of professional software is written in C++
– games
– engineering and statistical software
– database managers
– operating systems
– word processors
– spreadsheets
– embedded systems
• It affords use of object orientation
Object orientation and C++
• C++ is an extension of C invented in the 1980s by Bjarne
Stroustrup of AT&T Bell Labs (no relation)
– It was originally ‘C with classes’, i.e. an attempt to enable
decent object orientation in C
– C was used as the ‘base language’ as (according to
Stroustrup)
• it is versatile, terse and relatively low level
• is adequate for most tasks
• is widely available and use
– C is retained as a subset of C++ to avoid the need for
millions of lines of code to be re-written
• Stroustrup and his friends took the opportunity to add a few
useful extra features (see later)
The inventor of C++
Not a typical
computer
nerd?
http://www.research.att.com/~bs/
Gratuitous picture of Bjarne Stroustrup
Why use C++ rather than Java?
• C++ can re-use old C code
• Use of executables created using C++ do not depend on
‘virtual machines’ to run
• C++ is stable
– There is an international standard for C++
– Java is changing all the time so code might not work
everywhere
• Because C and C++ get quite close to the operating system,
code tends to be efficient and to run fast
• Lots of professional software is written using C++
– games, embedded systems, etc.
What is software?
• Generally means anything relating to a computer that is not
hardware
– Term first used by John Tukey – a Princeton statistician –
in 1958
– Separation – as articulated by Turing – of a single
physical machine from the different programmable tasks
that it can perform
• If the single machine to do what you want doesn’t exist, use a
computer and program it
– To be able to write software is useful even when using
software!
• A program doesn’t do quite what you need: create your own
• A program that you need to use laboriously and repeatedly: write
your own to perform the repeated operations
Algorithms and problem solving
• In essence, computer programs are solutions to problems
– What is the critical clearing time for a three-phase short-
circuit fault?
– What is average delay of a message on a given comms
network?
– What is the way of simulating a game of tennis?
• Problem solving is a key activity
– the process of taking a statement of a problem and
developing a computer program that solves it
• A solution consists of
1. an algorithm
2. a way to store data
What is a program?
• A program might be thought of simply as a solution to a
computation problem
– The solution will involve a sequence of instructions
• Some of these may be conditional on values of data
• In the early days of programming, software engineers
focused on the algorithm and implemented it in a procedural
way
– An example: instructions to make a cup of tea
– The means of storing data was often an afterthought
An algorithm to make a hot drink
Start
Kettle water
level == 0?
Want milk?
Put teabag in mug
Tea
Drink!
NoExtract teabag
Yes
Add milk
Yes
Heat kettle
Yes
Fill kettle
Yes
Put instant coffee in mug
Coffee
Teabag in mug?
No
Want tea
or coffee?
No
Water
temp < 100?
No
A conditional branching
A loop
Test condition for the loop
(whether or not to do an
iteration)
A conditional branching
A conditional branching
A conditional branching
A procedural program
kettle_water_level = 0
water_temperature = 0
If (kettle_water_level == 0) fill (kettle)
If (water_temperature < 100) {
Put_on_stove(kettle)
Stove(on)
until (water_temperature == 100) { /* don’t watch kettle */}
Stove(off)
}
Add_teabag (mug)
mug_water_level = 0
mug_water_colour = 0
Until (mug_water_level == 90) poor (kettle, mug)
Until (mug_water_colour == 70) { }
Add_milk (mug)
A conditional branching
A conditional branching
A conditional loop
Data storage
A function
A function is like a group of instructions
carried for one or more given inputs
• May or may not return some output
What is an object oriented program?
Kettle
{
water_level = 0
water_temperature = 0
Fill ()
PlaceOnStove()
Boil ()
}
Kettle::Boil()
{
if (water_level < 50) this->Fill()
if (water_temperature < 100)
this->PlaceOnStove()
stove.on()
until (water_temperature == 100)
{}
stove.off()
}
Mug
{
MakeTea(source)
MakeCoffee(source)
}
Mug::MakeTea(source)
{
source.Boil()
Add_TeaBag()
Poor_water()
Add_Milk()
}
Mug::MakeCoffee(source)
{
source.Boil()
Add_Coffee()
Poor_water()
}
main ()
{ mug.MakeTea() }
The program is organised
around the data structures:
• what are the objects?
• what states do they have?
• what do they do?
Features of a program - 1
• Variables (‘data’)
– storage or representation of the present state of the
program
– these may or may not be ‘typed’
• e.g. integer, floating point, string, …
• Operators
– the program can change the values of variables
– there are standard operations on variables
• e.g. increase or decrease the values, add two together, take one
from another, mulitply two together, …
• Functions
– standard or defined sets of operations on variables
Features of a program - 2
• Branches
– Testing of conditions (based on values of variables) and calling of
different functions depending on the test result
• Output to the user
– Something to tell the user the result(s) of the program
• Message to the screen (‘console’) or a file
• Input from the user
– If execution depends on values given by the user ‘at run time’, i.e. not
‘hard coded’ into the program, there should be input
• From the keyboard (‘console’), mouse or a file
• An entry point
– Where program execution starts
• One or more exit points
– Where program execution finishes
– Might depend on the flow of the program
Anatomy of a C++ program
• As in any high level language, we find
– A mechanism for storing data in the computer’s memory
• Known as variables
– Processes that can be used to perform calculations on
this data
• Operations and Functions
– Control Structures
• Allow control of how the operations are performed
– Optional execution (conditions and branches)
– Repeated execution (‘loops’)
– Mechanisms for inputting and outputting data
What C++ looks like
#include <iostream.h>
#include <stdio.h>
using namespace std;
int main()
{
int n;
// Prints “Hello!”
cout << “Hello!n”;
return 0;
}
• Basic Structure for any program in C++
Headers: Give access to
intrinsic functions
All programs must have
exactly one entry point.
In C and C++, this is the
‘main’ function
Don’t forget to terminate instructions with ;
Lets the compiler know which
version of standard functions to use
// Comments behind ‘//’
/* Old C style */
Variables and identifiers
• ‘Identifier’
– Name of a variable, structure, class, function, …
– Can be quite long and is case sensitive
• Count is not the same as count!!
• Variables
– Named memory locations to hold values that can be
modified
– In C++, each variable has a specific data ‘type’
– ‘Strong’ typing - why?
• Operations can be designed for specific types (for speed)
• The user is forced to think about what kind of data they are
interested in
• Inconsistent treatment of variables is minimised
Variable types
Size Type Description Range
4 int Integer -big no. to
+big no.
1 char Character – ASCII
codes
0 – 255
4 float Floating point number
– fractions possible
~1.2 e-38 to
3.4 e38
8 double Double precision float 1.2e-308 to
3.4e308
1 bool Boolean TRUE
FALSE
7 or 8 significant figures
15 or 16 significant figures
Operations
• Two types of basic operation we can perform on numbers.
– Logical tests
• Equal to (==), Not equal to (!=), Greater than (>), Less than (<),
Greater than or equal to (>=), Less than or equal to (<=).
• Logical NOT (!), Logical AND (&&), Logical OR (||)
• Be careful when testing equality of two floats or doubles
– check absolute value of difference is less than a very small number
– Arithmetic operations
• Assignment, Addition, Subtraction, Multiplication, Division
• =, +, -, *, /
• Maths functions achieved using library functions available to the
language (‘intrinsic’)
– Accessed via calls to code made available with compiler
– Let compiler know about them by using a #include statement
– #include <math.h>
Careful not to confuse assignment
with test of equality!
Turning source code into an executable
• ‘Interpreted’ code depends on some interpretation software
that acts as an interface between the humanly readable
source code and the operating system, .e.g. BASIC
– Java is a bit like that but depends on a ‘virtual machine’
• Most ‘high level’ languages use a ‘compiler’ to turn source
code into binary ‘executable’ code that can be run directly on
the host computer
– The source code can be transported between operating
systems but must be recompiled
– Binary executables run faster…
– … but you do need to use a compiler
Using a compiler
source1.cpp
source3.cpp
source2.cpp
header1.hpp
header2.hpp
header3.hpp
object1.obj
object2.obj
object3.obj
prog.exe
Pre-processor
Compile
Link
Using a C++ compiler
• C and C++ source code is
– ‘compiled’ into ‘objects’
– ‘objects’ are ‘linked’ into a binary ‘executable’ particular to the host
operating system
– You run the ‘executable’
• Naming convention
– stem.extension
• Source code: *.cpp
• Object code: *.obj or *.o
• Executable: can be with or without .exe extension
• * is a ‘wildcard’ character
• Use of compilers allows the code to be optimised for execution speed
– Very important for large, complex programs!
– (For simple stuff, some sort of ‘interpreted’ language such as Java,
VBA, awk or Perl is often fine even though these run slower)
More on compilers
• Compilers are themselves programs
• There are many different C and C++ compilers
– Different compilers sometimes assume source code with
slightly different syntax or with different intrinsic functions
• Source code may not be fully ‘portable’ to different
compilers/platforms
• If in doubt, write in ‘ANSI standard’ C or C++
• Switch on compiler option for ANSI standard source code
– Different compilers are better or worse at
• optimising code
• indicating problems with source code
– Some compilers are more ‘buggy’ than others!
Messages from compilers
• Compilers will flag up:
– ‘Errors’ – code that the complier cannot interpret or that
is inconsistent with code already processed
– ‘Warnings’ – code that, according to rules coded in the
compiler, might give errors upon execution
• You will not get object files or executables without fixing the
errors
• You should also resolve the warnings
Development environments
• An ‘integrated development environment’ (IDE) comprises
– a compiler
– a ‘debugger’
– some nice visual aids for writing and managing code
• otherwise, you would need just a text editor for creating the source code
• On this course we will use ‘Visual C++’ (see later)
– It is built around Microsoft’s C++ compiler
– Visual C++ Express is a free version that you can download at home
• Debuggers allow you to
– stop program execution at different points and look at the values of
variables
– you can check the program flow is as you expect
– need to compile the code specifically for debugging
– when satisfied it works, compile again for fast execution

More Related Content

What's hot (13)

Lecture 21
Lecture 21Lecture 21
Lecture 21
 
Program logic formulation
Program logic formulationProgram logic formulation
Program logic formulation
 
Embedded systems course - student resume template
Embedded systems course - student resume templateEmbedded systems course - student resume template
Embedded systems course - student resume template
 
(Prog213) (introduction to programming)v1
(Prog213) (introduction to programming)v1(Prog213) (introduction to programming)v1
(Prog213) (introduction to programming)v1
 
DMA113 Chap1
DMA113 Chap1DMA113 Chap1
DMA113 Chap1
 
Introduction to course
Introduction to courseIntroduction to course
Introduction to course
 
Test first!
Test first!Test first!
Test first!
 
Lecture 23 p1
Lecture 23 p1Lecture 23 p1
Lecture 23 p1
 
Beekman5 std ppt_13
Beekman5 std ppt_13Beekman5 std ppt_13
Beekman5 std ppt_13
 
Software engineering
Software engineeringSoftware engineering
Software engineering
 
Parallel Computing - Lec 6
Parallel Computing - Lec 6Parallel Computing - Lec 6
Parallel Computing - Lec 6
 
STRUCTURED PROGRAMMING Chap2
STRUCTURED PROGRAMMING Chap2STRUCTURED PROGRAMMING Chap2
STRUCTURED PROGRAMMING Chap2
 
Comp102 lec 1
Comp102   lec 1Comp102   lec 1
Comp102 lec 1
 

Similar to 01 introduction to cpp

Programming using C++ - slides.pptx
Programming using C++ - slides.pptxProgramming using C++ - slides.pptx
Programming using C++ - slides.pptxHeadoftheDepartment
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing TechniquesAppili Vamsi Krishna
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++KurdGul
 
Introduction to computer programming
Introduction to computer programmingIntroduction to computer programming
Introduction to computer programmingSangheethaa Sukumaran
 
Programming of c++
Programming of c++Programming of c++
Programming of c++Ateeq Sindhu
 
ProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdfProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdflailoesakhan
 
Introducing systems analysis, design & development Concepts
Introducing systems analysis, design & development ConceptsIntroducing systems analysis, design & development Concepts
Introducing systems analysis, design & development ConceptsShafiul Azam Chowdhury
 
Overview of Software Engineering Principles - SCPS311.pptx
Overview of Software Engineering Principles - SCPS311.pptxOverview of Software Engineering Principles - SCPS311.pptx
Overview of Software Engineering Principles - SCPS311.pptxBypassFrp
 
Unit 1 python (2021 r)
Unit 1 python (2021 r)Unit 1 python (2021 r)
Unit 1 python (2021 r)praveena p
 
Software Engineering Lec 1-introduction
Software Engineering Lec 1-introductionSoftware Engineering Lec 1-introduction
Software Engineering Lec 1-introductionTaymoor Nazmy
 

Similar to 01 introduction to cpp (20)

Programming using C++ - slides.pptx
Programming using C++ - slides.pptxProgramming using C++ - slides.pptx
Programming using C++ - slides.pptx
 
C_Programming_Notes_ICE
C_Programming_Notes_ICEC_Programming_Notes_ICE
C_Programming_Notes_ICE
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++
 
Introduction to computer programming
Introduction to computer programmingIntroduction to computer programming
Introduction to computer programming
 
Programming of c++
Programming of c++Programming of c++
Programming of c++
 
73d32 session1 c++
73d32 session1 c++73d32 session1 c++
73d32 session1 c++
 
[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP
 
ProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdfProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdf
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Introducing systems analysis, design & development Concepts
Introducing systems analysis, design & development ConceptsIntroducing systems analysis, design & development Concepts
Introducing systems analysis, design & development Concepts
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
DITEC - Software Engineering
DITEC - Software EngineeringDITEC - Software Engineering
DITEC - Software Engineering
 
SE Unit-1.pptx
SE Unit-1.pptxSE Unit-1.pptx
SE Unit-1.pptx
 
Lecture1
Lecture1Lecture1
Lecture1
 
Overview of Software Engineering Principles - SCPS311.pptx
Overview of Software Engineering Principles - SCPS311.pptxOverview of Software Engineering Principles - SCPS311.pptx
Overview of Software Engineering Principles - SCPS311.pptx
 
OOSE UNIT-1.pdf
OOSE UNIT-1.pdfOOSE UNIT-1.pdf
OOSE UNIT-1.pdf
 
C++ l 1
C++ l 1C++ l 1
C++ l 1
 
Unit 1 python (2021 r)
Unit 1 python (2021 r)Unit 1 python (2021 r)
Unit 1 python (2021 r)
 
Software Engineering Lec 1-introduction
Software Engineering Lec 1-introductionSoftware Engineering Lec 1-introduction
Software Engineering Lec 1-introduction
 

More from Manzoor ALam

8085 microprocessor ramesh gaonkar
8085 microprocessor   ramesh gaonkar8085 microprocessor   ramesh gaonkar
8085 microprocessor ramesh gaonkarManzoor ALam
 
03a control structures
03a   control structures03a   control structures
03a control structuresManzoor ALam
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic Manzoor ALam
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loopsManzoor ALam
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++Manzoor ALam
 

More from Manzoor ALam (6)

8085 microprocessor ramesh gaonkar
8085 microprocessor   ramesh gaonkar8085 microprocessor   ramesh gaonkar
8085 microprocessor ramesh gaonkar
 
03b loops
03b   loops03b   loops
03b loops
 
03a control structures
03a   control structures03a   control structures
03a control structures
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 

Recently uploaded

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Recently uploaded (20)

Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

01 introduction to cpp

  • 1. Computer Programming Introduction to the course and C++ Prof. Dr. Mohammad Haseeb Zafar haseeb@uetpeshawar.edu.pk
  • 2. Course Overview • Aims: – to provide a good introduction to programming in C++ – to give increased familiarity with programming, use of an ‘integrated development environment’ (IDE) and software development in general – to deepen appreciation of object orientation • Personnel – lectures: Prof. Dr. Mohammad Haseeb Zafar – labs: Engr. Maha Gul
  • 3. Course format • A weekly lecture introducing programming concepts and examples • From week 2, a weekly lab to practise the new ideas – Practice of the ideas will be essential to learning enough to pass the course – You should complete the exercise in the lab • People who fail to complete the labs tend to fail the course • Don’t fall behind! – You will need at least 2 hours • Do not assume the lab is effectively free time • You will not be able to simply copy and paste from the lectures! – You need to use the idea from the lecture
  • 4. Recommended resources • Books – “C++ How to Program” by Deitel & Deitel – “C++ programming in easy steps” by Mike McGrath – “Thinking in C++” by Bruce Eckel available at http://mindview.net/Books/TICPP/ThinkingInCPP2e.html#Con – For the advanced programmer: “The C++ Programming Language” by Bjarne Stroustrup, published by Addison Wesley • Web resources – http://www.cplusplus.com/ – Stroustrup’s website: http://www.research.att.com/~bs/C++.html
  • 5. Assessment • Sessionals: 25% – Assignments: 6% – Quizzes: 9% – Project: 10% • done in pairs in last 8 weeks of course • Mid Term Exam: 25% • Final Term Exam: 50%
  • 6. Course content • Introduction to the course, C++ and the IDE • Data types and operators • Functions • Conditions (if…) • Iteration (for loops, etc.) • Arrays, Strings and Pointers • Classes and objects
  • 7. Why learn to program? • You are likely to have to write some code some time – to do accurate calculations quickly and repeatably – to modify existing programs to reflect changed needs – for processing of large amounts of data • format changes, extraction of key information, … – for actions on a programmable device – to do your FYP – to learn about algorithms and how to solve problems
  • 8. Why learn C++? • It is sort of an extension of C – Lots of old code is in C – don’t want to throw it away! • Lots of professional software is written in C++ – games – engineering and statistical software – database managers – operating systems – word processors – spreadsheets – embedded systems • It affords use of object orientation
  • 9. Object orientation and C++ • C++ is an extension of C invented in the 1980s by Bjarne Stroustrup of AT&T Bell Labs (no relation) – It was originally ‘C with classes’, i.e. an attempt to enable decent object orientation in C – C was used as the ‘base language’ as (according to Stroustrup) • it is versatile, terse and relatively low level • is adequate for most tasks • is widely available and use – C is retained as a subset of C++ to avoid the need for millions of lines of code to be re-written • Stroustrup and his friends took the opportunity to add a few useful extra features (see later)
  • 10. The inventor of C++ Not a typical computer nerd? http://www.research.att.com/~bs/ Gratuitous picture of Bjarne Stroustrup
  • 11. Why use C++ rather than Java? • C++ can re-use old C code • Use of executables created using C++ do not depend on ‘virtual machines’ to run • C++ is stable – There is an international standard for C++ – Java is changing all the time so code might not work everywhere • Because C and C++ get quite close to the operating system, code tends to be efficient and to run fast • Lots of professional software is written using C++ – games, embedded systems, etc.
  • 12. What is software? • Generally means anything relating to a computer that is not hardware – Term first used by John Tukey – a Princeton statistician – in 1958 – Separation – as articulated by Turing – of a single physical machine from the different programmable tasks that it can perform • If the single machine to do what you want doesn’t exist, use a computer and program it – To be able to write software is useful even when using software! • A program doesn’t do quite what you need: create your own • A program that you need to use laboriously and repeatedly: write your own to perform the repeated operations
  • 13. Algorithms and problem solving • In essence, computer programs are solutions to problems – What is the critical clearing time for a three-phase short- circuit fault? – What is average delay of a message on a given comms network? – What is the way of simulating a game of tennis? • Problem solving is a key activity – the process of taking a statement of a problem and developing a computer program that solves it • A solution consists of 1. an algorithm 2. a way to store data
  • 14. What is a program? • A program might be thought of simply as a solution to a computation problem – The solution will involve a sequence of instructions • Some of these may be conditional on values of data • In the early days of programming, software engineers focused on the algorithm and implemented it in a procedural way – An example: instructions to make a cup of tea – The means of storing data was often an afterthought
  • 15. An algorithm to make a hot drink Start Kettle water level == 0? Want milk? Put teabag in mug Tea Drink! NoExtract teabag Yes Add milk Yes Heat kettle Yes Fill kettle Yes Put instant coffee in mug Coffee Teabag in mug? No Want tea or coffee? No Water temp < 100? No A conditional branching A loop Test condition for the loop (whether or not to do an iteration) A conditional branching A conditional branching A conditional branching
  • 16. A procedural program kettle_water_level = 0 water_temperature = 0 If (kettle_water_level == 0) fill (kettle) If (water_temperature < 100) { Put_on_stove(kettle) Stove(on) until (water_temperature == 100) { /* don’t watch kettle */} Stove(off) } Add_teabag (mug) mug_water_level = 0 mug_water_colour = 0 Until (mug_water_level == 90) poor (kettle, mug) Until (mug_water_colour == 70) { } Add_milk (mug) A conditional branching A conditional branching A conditional loop Data storage A function A function is like a group of instructions carried for one or more given inputs • May or may not return some output
  • 17. What is an object oriented program? Kettle { water_level = 0 water_temperature = 0 Fill () PlaceOnStove() Boil () } Kettle::Boil() { if (water_level < 50) this->Fill() if (water_temperature < 100) this->PlaceOnStove() stove.on() until (water_temperature == 100) {} stove.off() } Mug { MakeTea(source) MakeCoffee(source) } Mug::MakeTea(source) { source.Boil() Add_TeaBag() Poor_water() Add_Milk() } Mug::MakeCoffee(source) { source.Boil() Add_Coffee() Poor_water() } main () { mug.MakeTea() } The program is organised around the data structures: • what are the objects? • what states do they have? • what do they do?
  • 18. Features of a program - 1 • Variables (‘data’) – storage or representation of the present state of the program – these may or may not be ‘typed’ • e.g. integer, floating point, string, … • Operators – the program can change the values of variables – there are standard operations on variables • e.g. increase or decrease the values, add two together, take one from another, mulitply two together, … • Functions – standard or defined sets of operations on variables
  • 19. Features of a program - 2 • Branches – Testing of conditions (based on values of variables) and calling of different functions depending on the test result • Output to the user – Something to tell the user the result(s) of the program • Message to the screen (‘console’) or a file • Input from the user – If execution depends on values given by the user ‘at run time’, i.e. not ‘hard coded’ into the program, there should be input • From the keyboard (‘console’), mouse or a file • An entry point – Where program execution starts • One or more exit points – Where program execution finishes – Might depend on the flow of the program
  • 20. Anatomy of a C++ program • As in any high level language, we find – A mechanism for storing data in the computer’s memory • Known as variables – Processes that can be used to perform calculations on this data • Operations and Functions – Control Structures • Allow control of how the operations are performed – Optional execution (conditions and branches) – Repeated execution (‘loops’) – Mechanisms for inputting and outputting data
  • 21. What C++ looks like #include <iostream.h> #include <stdio.h> using namespace std; int main() { int n; // Prints “Hello!” cout << “Hello!n”; return 0; } • Basic Structure for any program in C++ Headers: Give access to intrinsic functions All programs must have exactly one entry point. In C and C++, this is the ‘main’ function Don’t forget to terminate instructions with ; Lets the compiler know which version of standard functions to use // Comments behind ‘//’ /* Old C style */
  • 22. Variables and identifiers • ‘Identifier’ – Name of a variable, structure, class, function, … – Can be quite long and is case sensitive • Count is not the same as count!! • Variables – Named memory locations to hold values that can be modified – In C++, each variable has a specific data ‘type’ – ‘Strong’ typing - why? • Operations can be designed for specific types (for speed) • The user is forced to think about what kind of data they are interested in • Inconsistent treatment of variables is minimised
  • 23. Variable types Size Type Description Range 4 int Integer -big no. to +big no. 1 char Character – ASCII codes 0 – 255 4 float Floating point number – fractions possible ~1.2 e-38 to 3.4 e38 8 double Double precision float 1.2e-308 to 3.4e308 1 bool Boolean TRUE FALSE 7 or 8 significant figures 15 or 16 significant figures
  • 24. Operations • Two types of basic operation we can perform on numbers. – Logical tests • Equal to (==), Not equal to (!=), Greater than (>), Less than (<), Greater than or equal to (>=), Less than or equal to (<=). • Logical NOT (!), Logical AND (&&), Logical OR (||) • Be careful when testing equality of two floats or doubles – check absolute value of difference is less than a very small number – Arithmetic operations • Assignment, Addition, Subtraction, Multiplication, Division • =, +, -, *, / • Maths functions achieved using library functions available to the language (‘intrinsic’) – Accessed via calls to code made available with compiler – Let compiler know about them by using a #include statement – #include <math.h> Careful not to confuse assignment with test of equality!
  • 25. Turning source code into an executable • ‘Interpreted’ code depends on some interpretation software that acts as an interface between the humanly readable source code and the operating system, .e.g. BASIC – Java is a bit like that but depends on a ‘virtual machine’ • Most ‘high level’ languages use a ‘compiler’ to turn source code into binary ‘executable’ code that can be run directly on the host computer – The source code can be transported between operating systems but must be recompiled – Binary executables run faster… – … but you do need to use a compiler
  • 27. Using a C++ compiler • C and C++ source code is – ‘compiled’ into ‘objects’ – ‘objects’ are ‘linked’ into a binary ‘executable’ particular to the host operating system – You run the ‘executable’ • Naming convention – stem.extension • Source code: *.cpp • Object code: *.obj or *.o • Executable: can be with or without .exe extension • * is a ‘wildcard’ character • Use of compilers allows the code to be optimised for execution speed – Very important for large, complex programs! – (For simple stuff, some sort of ‘interpreted’ language such as Java, VBA, awk or Perl is often fine even though these run slower)
  • 28. More on compilers • Compilers are themselves programs • There are many different C and C++ compilers – Different compilers sometimes assume source code with slightly different syntax or with different intrinsic functions • Source code may not be fully ‘portable’ to different compilers/platforms • If in doubt, write in ‘ANSI standard’ C or C++ • Switch on compiler option for ANSI standard source code – Different compilers are better or worse at • optimising code • indicating problems with source code – Some compilers are more ‘buggy’ than others!
  • 29. Messages from compilers • Compilers will flag up: – ‘Errors’ – code that the complier cannot interpret or that is inconsistent with code already processed – ‘Warnings’ – code that, according to rules coded in the compiler, might give errors upon execution • You will not get object files or executables without fixing the errors • You should also resolve the warnings
  • 30. Development environments • An ‘integrated development environment’ (IDE) comprises – a compiler – a ‘debugger’ – some nice visual aids for writing and managing code • otherwise, you would need just a text editor for creating the source code • On this course we will use ‘Visual C++’ (see later) – It is built around Microsoft’s C++ compiler – Visual C++ Express is a free version that you can download at home • Debuggers allow you to – stop program execution at different points and look at the values of variables – you can check the program flow is as you expect – need to compile the code specifically for debugging – when satisfied it works, compile again for fast execution