introduction to c programming sessions 1 3 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to C++ Programming Sessions 1-3 PowerPoint Presentation
Download Presentation
Introduction to C++ Programming Sessions 1-3

Loading in 2 Seconds...

play fullscreen
1 / 58

Introduction to C++ Programming Sessions 1-3 - PowerPoint PPT Presentation


  • 86 Views
  • Uploaded on

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?

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to C++ Programming Sessions 1-3' - kiril


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
today s arrangements
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
Your safety is important
  • Where is the fire exit?
  • Beware of hazards
    • Tripping over bags and coats
  • Please tell us if anything does not work
  • Let us know if you have any other concerns
your comfort is important

♫♪

Your comfort is important
  • The toilets are along the corridor just outside the teaching rooms
  • The rest area is where you registered;it has vending machines and a water cooler
  • The seats at the computers are adjustable
  • You can adjust the monitors for height, tilt and brightness
what you know already
What you know already…
  • Nothing is assumed
today s topics
C++ and OOP?

Compilers

ISO and ANSI

Standard C++ Library

The Standard Template Library (STL )

Dev-C++

Today's Topics

First Program

Fundamental data types

string class

Functions

what is oop
What is OOP?
  • Object Oriented Programming
  • A way of modelling individual objects in the real world
    • Students
    • Vehicles
    • Buildings
    • ATM’s
    • etc, etc
so what is oop
So, what is OOP?
  • Natural thinking – making our C++ code do what we expect something to do in real life.
  • Class
    • Member functions/methodsrepresent real object - behaviour
    • Member variables/data membersrepresent real object - state
creating a c program

Editor

Disk

Add source Code

Pre-processor

Disk

Directives allow add contents from ext files or constants

Compiler

Disk

Convert the high level language into object code

Linker

Disk

Link object code to library code & create exec code

Memory

Load from disk into memory

Loader

Disk

Execution, CPU executes the program

CPU

Creating a C++ program
why compile
Why Compile?

English talk

“Add 17 and 9 please”

“Johann Strauss”

I’m sorry but I don’t understand English just binary talk, 1’s and 0’s.

001010100111

011011000110

100110110011

111100100000

100000111110

why compile1
Why Compile?

C++ Talk

C

O

M

P

I

L

E

R

cout<<"Enter the first number: ";

cin >>Num1;

cout<<"Enter the second number: ";

cin >>Num2;

cout<<"The two numbers added = "<<Num1 + Num2;

Binary Talk

Enter the first number:

17

111001101111

001010100111

011011000110

100110110011

111101011111

Enter the second number:

Monitor

9

The two numbers added =

26

c compilers
C++ Compilers
  • Many and varied
    • Sun Studio 10 Solaris, Linux
    • VisualAge C++ Linux
    • GCC Multi-platform
    • Microsoft Visual C++ Windows
    • Cygwin Linux
    • Dev-C++ Windows
    • Xcode Apple
compiler conformance
Compiler Conformance
  • ISO International Standards Organisation
  • ANSI - American National Standards Institute
  • Standard
    • C++98
    • C++2003
    • 2005 Library Technical Report 1
  • Compilers and standard library implementations should support these standards
compiler conformance1
Compiler Conformance
  • There is no C++ compiler or library today that implements the Standard perfectly
standard c library
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
first program
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
first program1
First Program

int 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

exercises
Exercises
  • Complete Exercises 1-4
  • Complete all the tasks
  • Restart at 3:30pm
functions
Functions
  • Used to encapsulate data and operations
    • Can simplify coding
      • Functions for discrete tasks
      • Not hundreds of lines of code
    • Only need to know
      • Input data
      • Output data
  • Functions can be reused
functions1
Functions
  • Need a prototype
    • Tells the compiler what is coming
      • Return type
      • Function name
      • Parameter list (what is being passed in)

voidreadChar();

intgetNumber();

doublenumDoubled(int);

functions2
Functions
  • Defining a function (no return value)

voidreadChar()

{

char aChar;

cout << "Enter a CHARACTER: " ;

cin >> aChar;

cout << "Character is " << aChar << endl;

return;

}

functions3
Functions
  • Calling a function
  • With no return type
    • readChar();
  • With return type int
  • int myNum = 0;
    • myNum = readNumber();
    • cout << “The integer returned is “<< myNum;

Func.dev

functions4
Functions
  • Pass by Reference
  • Previously, pass by Value
    • Copy of value passed to function
  • Pass by Reference
    • Address of value passed to function

FuncRef.dev

exercises1
Exercises
  • Complete Exercises 5-7
  • Complete all the tasks
todays topics
Creating classes

Member functions

Data members

Access specifiers

Todays Topics
  • Flow Control
  • Sequence
  • Selection
    • if..else
    • switch
  • Repetition
    • while
    • for
    • do…while
classes and objects
Classes and Objects
  • #include <string>
  • stringStudent1;
  • intnumChar = 0;
  • cout << "Enter your name ";
  • getline(cin, Student1);
  • numChar = Student1.length();
  • cout<<"The number of characters is: "<<numChar;
classes and objects1
Classes and Objects
  • A class is a definition of a compound variable type
  • An object is an instance of that class
  • From a student class
    • Create many objects of type student
      • James
      • Sarah
      • Thomas
      • Jane
    • Create an instance (object) of classstudent

studentJames;

studentSarah;

classes and objects2
Classes and Objects
  • Member functions
    • Called by objectName.functionName()

James.displayName();

James.setCourseName();

James.setYears();

  • Data Members (variables of object)
    • Member functions used to access the data members
classes and objects3
Classes and Objects
  • Data Members
    • Access specifiers
    • private: only accessible via member functions of the class
    • protected: only accessed via member functions of the class and member functions of a derived class
    • public: can be accessed from any (non-member) function, anywhere the object is visible
classes and objects4
Classes and Objects

class StudentCourse

{

private: string courseName;

public:

void setName(string name)

{

courseName = name;

}

};

OxStudents.dev

classes and objects5
Classes and Objects
  • Constructors
    • Default constructors, provided by compiler
    • Create your own & initialise data members

StudentCourse (string cName)

{

CourseName = cName;

}

  • Destructors
    • Class name preceded by tilde ~

~StudentCourse ();

classes and objects6
Classes and Objects
  • Separate class files for reusability
    • Class files with main() means the class cannot be reused.
    • Only have one main() function
    • Separate class into own file with .h suffix
    • Use pre-processor directive to add the file when compiled
    • #include “myClass.h”

Constructor.dev

exercises2
Exercises
  • Complete Exercises 8-13
  • Complete all the tasks
  • Re-start 3:45pm
basic control flow
Basic Control Flow
  • Sequence
    • What we have been doing already
  • Selection
    • if…else statements
    • Two possible marks, 49 or 50 stored in score

if(condition is true)if(score >= 50)

cout<<“Passed”;cout<<“Passed”;

elseelse

cout<<“Failed”;cout<<“Failed”;

basic control flow1
Basic Control Flow
  • Selection
    • switch multiple selection statements
      • Test must be constant integer value, 1, 10, ‘A’, ‘x’. Not 10.56, 5.2.

switch (Test)

{

case1:

cout<<“Number1”;

break;

default:

cout<<“NOT Number1”;

}

IfSwitch.dev

basic control flow2
Basic Control Flow
  • Repetition
    • The ‘for’ loop

for (i = 1; i<= 5; i++)

{

do this statement;

now do this statement;

}

    • Note: = is an assignment

<= is a relational operator

== is an equality operator

basic control flow3
Basic Control Flow
  • Repetition
    • while statement

while(some condition is true)

do the statements;

while (counter < 4)

{

cout<<"Enter mark ";

cin >> mark;

total = total + mark;

counter ++;

}

basic control flow4
Basic Control Flow
  • Repetition
    • do…while loop

do

{

statements

}

while(the condition is true)

    • do

{

cout<<“Mark number " <<mark <<endl;

mark ++;

}

while (mark <=10);

DoWhile.dev

exercises3
Exercises
  • Complete Exercises 14-18
  • Complete all the tasks
today s topics1
Arrays

Vectors

Today's Topics

Function Templates

Pointers

Interfaces

arrays
Arrays
  • Data 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 size
arrays1
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
arrays2
Arrays
  • Adding data to arrays.

double examMarks[5];

for(int i = 0; i <5; i++)

{

cout<<"Enter Exam Mark "<<i + 1<<" ";

cin >> examMarks[i];

}

arrays3
Arrays
  • Outputting data from an array

for(int i = 0; i <5; i++)

{

cout<<"Exam Mark "<<i + 1<<"is "<< examMarks[i] <<endl;

}

Array.dev

vectors
Vectors
  • Container class, part of Standard Template Library, similar to arrays
  • Can hold objects of same type of data (int,double,string, char,object)
  • Can resize, grow, shrink as elements are added or removed from the end
  • Algorithms to manipulate data
  • Iterators (like pointers) to cycle through all elements in vector
vectors1
Vectors
  • vector<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]<<" ";

}

vectors2
Vectors
  • Adding 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]<<" ";

}

vectors3
Vectors

VectorPushBack.dev

function templates
Function Templates
  • One function definition
  • Separate object code functions created,
    • Determined by argument
    • Effectively 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 )
function templates1
Function Templates

template < 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

exercises4
Exercises
  • Complete Exercises 19, 23, 33
  • Select the tasks to complete
pointers
Pointers
  • A pointer identifies a memory address
    • A pointer can be used to point to the location in memory where a variable is stored
    • Variable: 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

pointers1
Pointers
  • Initialising 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

interfaces
Interfaces
  • Up 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 works
  • Interfaces should separate the class definition from its implementation
interfaces1
Interfaces
  • An 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
interfaces2
Interfaces

Implementation file

Class definition/Interface

Client Source Code

Student.h

Student.cpp

main function

Compiler

Compiler

C++ Library

object code

main function

Object code

Student class

Object code

Student executable

application

Interface.dev

Linker

exercises5
Exercises
  • Complete Exercises 34, 35, 36
  • Select the tasks to complete
c sessions 1 3

C++ Sessions 1-3

www.oucs.ox.ac.uk/itlp/courses.xml