introduction to c++ programming sessions 1-3
DESCRIPTION
Introduction to C++ Programming Sessions 1-3. IT Learning Group. Today’s arrangements. Your teacher is:Ian Miller Your demonstrators are:Chris, Hasan, Ronald We finish at:5:00pm You should have:Class notes Copies of slides. Your safety is important. Where is the fire exit? - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/1.jpg)
IT Learning Programme
Introduction toC++ ProgrammingSessions 1-3
IT Learning Group
![Page 2: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/2.jpg)
Today’s arrangements
Your teacher is: Ian MillerYour demonstrators are: Chris, Hasan, Ronald
We finish at: 5:00pmYou should have: Class notes
Copies of slides
![Page 3: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/3.jpg)
Your safety is important
Where is the fire exit?Beware of hazards
Tripping over bags and coatsPlease tell us if anything does not workLet us know if you have any other concerns
![Page 4: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/4.jpg)
Your comfort is important
The toilets are along the corridor just outside the teaching roomsThe rest area is where you registered;it has vending machines and a water coolerThe seats at the computers are adjustableYou can adjust the monitors for height, tilt and brightness
♫♪
![Page 5: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/5.jpg)
What you know already…
Nothing is assumed
![Page 6: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/6.jpg)
Today's TopicsC++ and OOP?CompilersISO and ANSIStandard C++ LibraryThe Standard Template Library (STL )Dev-C++
First ProgramFundamental data types
string classFunctions
![Page 7: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/7.jpg)
What is OOP?
Object Oriented ProgrammingA way of modelling individual objects in the real world
StudentsVehiclesBuildingsATM’setc, etc
![Page 8: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/8.jpg)
So, what is OOP?
Natural thinking – making our C++ code do what we expect something to do in real life.Class
Member functions/methods represent real object - behaviourMember variables/data membersrepresent real object - state
![Page 9: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/9.jpg)
Creating a C++ programEditor
Pre-processor
Compiler
Linker
Loader
CPU
Disk
Disk
Disk
Disk
Memory
Add source Code
Directives allow add contents from ext files or constants
Convert the high level language into object code
Link object code to library code & create exec code
Load from disk into memory
Execution, CPU executes the program
Disk
![Page 10: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/10.jpg)
Why Compile?English talk
“Add 17 and 9 please”
I’m sorry but I don’t understand English just binary talk, 1’s and 0’s.
001010100111011011000110100110110011111100100000100000111110
“Johann Strauss”
![Page 11: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/11.jpg)
Why Compile?
cout<<"Enter the first number: ";cin >>Num1;cout<<"Enter the second number: ";cin >>Num2;cout<<"The two numbers added = "<<Num1 + Num2;
The two numbers added =
111001101111 001010100111011011000110100110110011111101011111
COMPILER
C++ Talk
Binary TalkEnter the first number:
Enter the second number:
17
9Monitor
26
![Page 12: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/12.jpg)
C++ Compilers
Many and variedSun Studio 10 Solaris, LinuxVisualAge C++ LinuxGCC Multi-platformMicrosoft Visual C++ WindowsCygwin LinuxDev-C++ WindowsXcode Apple
![Page 13: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/13.jpg)
Compiler ConformanceISO International Standards Organisation
ANSI - American National Standards Institute Standard
C++98C++20032005 Library Technical Report 1
Compilers and standard library implementations should support these standards
![Page 14: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/14.jpg)
Compiler Conformance
There is no C++ compiler or library today that implements the Standard perfectly
![Page 15: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/15.jpg)
Standard C++ Library• Collection of classes and
functions• Result of conformance to ISO
standard• Incorporates what was STL
• Class definitions for standard data structures
• Collection of algorithms used to manipulate these and other structures
![Page 16: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/16.jpg)
First Program
#include <iostream> Include contents of the header file in the program
using namespace std;cin is standard input stream object of istream class that allows input from keyboard cout is standard output stream object, output to screen
int main()In every C++ program, function
![Page 17: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/17.jpg)
First Programint aNum = 0;
variable (memory location called aNum) to hold number of type integer (int)
<< stream insertion operator cout <<“Enter a number:- ”;
>> stream extraction operator cin >>aNum;
First program
![Page 18: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/18.jpg)
Exercises
Complete Exercises 1-4
Complete all the tasks
Restart at 3:30pm
![Page 19: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/19.jpg)
FunctionsUsed to encapsulate data and operations
Can simplify codingFunctions for discrete tasksNot hundreds of lines of code
Only need to knowInput dataOutput data
Functions can be reused
![Page 20: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/20.jpg)
FunctionsNeed a prototype
Tells the compiler what is comingReturn typeFunction nameParameter list (what is being passed in)
void readChar();int getNumber();double numDoubled(int);
![Page 21: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/21.jpg)
FunctionsDefining a function (no return value)
void readChar(){char aChar; cout << "Enter a CHARACTER: " ;cin >> aChar; cout << "Character is " << aChar << endl;return;}
![Page 22: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/22.jpg)
FunctionsCalling a function
With no return type readChar();
With return type int int myNum = 0;
myNum = readNumber();cout << “The integer returned is “<< myNum; Func.dev
![Page 23: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/23.jpg)
FunctionsPass by Reference
Previously, pass by Value Copy of value passed to function
Pass by ReferenceAddress of value passed to function
FuncRef.dev
![Page 24: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/24.jpg)
Exercises
Complete Exercises 5-7
Complete all the tasks
![Page 25: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/25.jpg)
Todays TopicsCreating classesMember functionsData membersAccess specifiers
Flow ControlSequenceSelection
if..elseswitch
Repetitionwhilefordo…while
![Page 26: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/26.jpg)
Classes and Objects#include <string> string Student1;int numChar = 0;
cout << "Enter your name ";getline(cin, Student1);
numChar = Student1.length(); cout<<"The number of characters is: "<<numChar;
![Page 27: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/27.jpg)
Classes and ObjectsA class is a definition of a compound variable typeAn object is an instance of that classFrom a student class
Create many objects of type student James Sarah Thomas Jane
Create an instance (object) of class studentstudent James;student Sarah;
![Page 28: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/28.jpg)
Classes and ObjectsMember functions
Called by objectName.functionName()
James.displayName(); James.setCourseName(); James.setYears();
Data Members (variables of object)Member functions used to access the data members
![Page 29: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/29.jpg)
Classes and Objects
Data Members
Access specifiers
private: only accessible via member functions of the classprotected: only accessed via member functions of the class and member functions of a derived classpublic: can be accessed from any (non-member) function, anywhere the object is visible
![Page 30: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/30.jpg)
Classes and Objectsclass StudentCourse { private: string courseName; public:
void setName(string name) { courseName = name;
} }; OxStudents.dev
![Page 31: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/31.jpg)
Classes and ObjectsConstructors
Default constructors, provided by compilerCreate your own & initialise data membersStudentCourse (string cName) {
CourseName = cName;}
DestructorsClass name preceded by tilde ~~ StudentCourse ();
![Page 32: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/32.jpg)
Classes and Objects
Separate class files for reusabilityClass files with main() means the class cannot be reused.Only have one main() functionSeparate class into own file with .h suffixUse pre-processor directive to add the file when compiled#include “myClass.h”
Constructor.dev
![Page 33: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/33.jpg)
Exercises
Complete Exercises 8-13
Complete all the tasks
Re-start 3:45pm
![Page 34: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/34.jpg)
Basic Control FlowSequence
What we have been doing already
Selectionif…else statementsTwo possible marks, 49 or 50 stored in score
if(condition is true) if(score >= 50)cout<<“Passed”; cout<<“Passed”;
else elsecout<<“Failed”; cout<<“Failed”;
![Page 35: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/35.jpg)
Basic Control FlowSelection
switch multiple selection statementsTest must be constant integer value, 1, 10, ‘A’, ‘x’. Not 10.56, 5.2.
switch (Test) { case 1: cout<<“Number1”; break;
default: cout<<“NOT Number1”;} IfSwitch.dev
![Page 36: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/36.jpg)
Basic Control FlowRepetition
The ‘for’ loopfor (i = 1; i<= 5; i++)
{do this statement;now do this statement;}
Note: = is an assignment <= is a relational operator == is an equality operator
![Page 37: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/37.jpg)
Basic Control FlowRepetition
while statementwhile(some condition is true)
do the statements;while (counter < 4) { cout<<"Enter mark "; cin >> mark; total = total + mark; counter ++; }
![Page 38: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/38.jpg)
Basic Control FlowRepetition
do…while loopdo{statements}while(the condition is true) do { cout<<“Mark number " <<mark <<endl;
mark ++; } while (mark <=10);
DoWhile.dev
![Page 39: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/39.jpg)
Exercises
Complete Exercises 14-18
Complete all the tasks
![Page 40: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/40.jpg)
Today's Topics
ArraysVectors
Function TemplatesPointersInterfaces
![Page 41: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/41.jpg)
ArraysData structure containing same type of data (int, double, string, char, object)
Series of elements each containing one item of data (contiguous memory locations)
Cannot change sizeElement 0 1 2 3 4 5
Experiment Result 34.67 31.78 31.89 34.67 36.23 32.78
![Page 42: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/42.jpg)
Arrays
int inNumbers[20];an array of 20 integers
char inName[5];an array of 5 characters
double examMarks[] = {1.2, 3.9, 9.5}initialise and set size to 3 elements of type double
![Page 43: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/43.jpg)
ArraysAdding data to arrays.
double examMarks[5];
for(int i = 0; i <5; i++) { cout<<"Enter Exam Mark "<<i + 1<<" "; cin >> examMarks[i];}
![Page 44: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/44.jpg)
Arrays
Outputting data from an array
for(int i = 0; i <5; i++) {cout<<"Exam Mark "<<i + 1<<"is "<< examMarks[i] <<endl;}
Array.dev
![Page 45: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/45.jpg)
Vectors
Container class, part of Standard Template Library, similar to arraysCan hold objects of same type of data (int, double, string, char, object)Can resize, grow, shrink as elements are added or removed from the endAlgorithms to manipulate dataIterators (like pointers) to cycle through all elements in vector
![Page 46: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/46.jpg)
Vectorsvector<int> vec(20);
for(int i = 0; i<vec.size(); i++) vec[i] = (i);
for(int i = 0; i<vec.size(); i++) { cout<< vec[i]<<" "; }
![Page 47: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/47.jpg)
VectorsAdding an extra element
cout<<"Enter an Extra Value ";cin >> aNum;vec.push_back(aNum);
Print vector of 21 numbers
for(int i = 0; i<vec.size(); i++) { cout<< vec[i]<<" "; }
![Page 48: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/48.jpg)
VectorsVector member Function Effect
at(element number) Gets the value contained in the specified element
back() Gets the value in the final elementbegin() Points to the first element in vectorclear() Erases the vectorempty() Returns true (1) if the vector is empty, or false (0) otherwise
end() Points to the last element in vectorfront() Gets the value in the first elementpop_back() Removes the final elementpush_back(value) Adds an element to the end of the vector, containing the
specified valuesize() Gets the number of elements
reverse();reverse(v.begin() v.end())
An algorithm (global function) that reverses the values in a vector (v)
v.begin() and v.end() are iteratorssort(); sort(v.begin(), v.end())
An algorithm (global function) that sorts the vector (v)
v.begin() and v.end() are iterators
VectorPushBack.dev
![Page 49: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/49.jpg)
Function Templates
One function definitionSeparate object code functions created,
Determined by argumentEffectively many overloaded functions
template < typename T >Value T is a type, not a value - we don’t know that yet
T maximum( T value1, T value2, T value3 )
![Page 50: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/50.jpg)
Function Templatestemplate < typename T > T maximum( T value1, T value2, T value3 ){T maximumValue = value1;
if ( value2 > maximumValue ) maximumValue = value2;
if ( value3 > maximumValue ) maximumValue = value3;
return maximumValue;}
FunctionTemplate.dev
ArraySortVectorTEMPLATEClass
![Page 51: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/51.jpg)
Exercises
Complete Exercises 19, 23, 33
Select the tasks to complete
![Page 52: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/52.jpg)
PointersA pointer identifies a memory address
A pointer can be used to point to the location in memory where a variable is storedVariable: int a = 5;Pointer: int *ap; (*is dereference operator)
NOTE: *used in declarations section indicates variable is a pointer, not a value
*used before a pointer elsewhere in program, references the value at the address in memory
![Page 53: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/53.jpg)
PointersInitialising pointers
int a = 5;int *ap;ap = &a; pointer ap now points to
the address of variable a.OR
int a = 5;int *ap = &a;
PointerArithmetic.dev
![Page 54: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/54.jpg)
InterfacesUp to now the class definitions have been kept in a header file .h suffix. They contain definition and implementation
Interfaces define the services a class object can use (it’s member functions)Interfaces should contain no detail of how the class worksInterfaces should separate the class definition from its implementation
![Page 55: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/55.jpg)
InterfacesAn interface defines class member functions as function prototypes but should give no detail of how the member functions are implemented
The member function implementation is held in a source code file with the same base name as the header file with .cpp extension
![Page 56: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/56.jpg)
Interfaces
Student.cpp main function
Student classObject code
Student.h
C++ Library object code
main functionObject code
Student executableapplicationLinker
CompilerCompiler
Interface.dev
Implementation file Class definition/Interface Client Source Code
![Page 57: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/57.jpg)
Exercises
Complete Exercises 34, 35, 36
Select the tasks to complete
![Page 58: Introduction to C++ Programming Sessions 1-3](https://reader036.vdocuments.mx/reader036/viewer/2022062410/5681656f550346895dd802f7/html5/thumbnails/58.jpg)
IT Learning Programme
C++ Sessions 1-3
www.oucs.ox.ac.uk/itlp/courses.xml