31
C++ Programming Concepts Lecture 1 Introduction

C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Embed Size (px)

Citation preview

Page 1: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

C++ Programming Concepts

Lecture 1

Introduction

Page 2: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Module Introduction – Text

• Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S. ISBN 0-13-111881-1

• Liberty, J. & Horvath, D.B. (2005) Teach Yourself C++ in 24 Hours, Sams, USA. ISBN 0-672-32681-7

• Overland, B. (2005)C++ Without FearPrenctice Hall, USA. ISBN 0-321-24695-0

Page 3: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Module Introduction - Text

• If available – second hand?Graham, N (1991) Learning C++McGraw Hill, USA. ISBN 0-07-100849-7

• Eckle, B. (2000) Thinking in C++ (Volume 1)Eckle, B. (2003) Thinking in C++ (Volume 2)– Both available as free download

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

– Vol 1 – 867KB; Vol 2 – 991KB

Page 4: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Some ‘C’ Revision

• Variable declarationsint nCount, iLoopCounter;

double dValue;

float fAmount;

long lBigValue;

int *ptrMyInt;

Page 5: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

“C” – Hello World

#include <stdio.h>

void main(void)

{

printf(“Hello World.\n”);

}

Page 6: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

“C” – Hello World

#include <stdio.h>

void main(void)

{

printf(“Hello World.\n”);

}

Pre-processor command

StandardInput / Output

Library

Control Character

Function Call

Function DeclarationType

String literal

Page 7: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

“C” – Hello World : Functions#include <stdio.h>

void fnSayHello(void);

void main(void)

{

fnSayHello();

}

void fnSayHello(void)

{

printf(“Hello World. \n”);

}

Page 8: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

“C” – Hello World : Functions#include <stdio.h>

void fnSayHello(void);

int main(void)

{

fnSayHello();

return 0;

}

void fnSayHello(void)

{

printf(“Hello World. \n”);

}

FunctionCall

Function Prototype

Function Declaration

Page 9: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Comments - C/* Pre-processor instructions */

#include <stdio.h>

int main(void)

{

/* Print output to screen */

printf(“Hello World!/n”);

return 0; /* Signal to system */

} /*end of main*/

Page 10: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Comments – C++// Pre-processor instructions

#include <stdio.h>

int main(void)

{

// Print output to screen

printf(“Hello World!/n”);

return 0; // Signal to system

} // end of main

Page 11: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Comments generally

• Possible to use either style within C++ code

• So…..

• …. make sure you use plenty!

• Together with– good variable names

and– good function / method names

Page 12: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Naming

• Hungarian Notation– Microsoft Web Site

• Simplified– Chuck Sphar– Cut down version

Page 13: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Simplified Hungarian

• Prefix name with lower case to indicate type.

char ‘c’ e.g. char cLetter;

integer ‘i’ or ‘n’ e.g. int nCount, iValue;

float ‘f’ e.g. float fLength;

double ‘d’ e.g. double dSalary;

pointer ‘p’ or ‘ptr’ e.g. char *pName, *ptrNext;

array ‘ar’ e.g. char arCustName[50];

etc….

Page 14: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Objects and Classes

• What is an Object?

• What is a Class?

• Some examples …..

Page 15: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Building Plans

Page 16: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

House

Page 17: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Houses

Page 18: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Doing a “Delia”

IngredientsButter 110grms Sugar 110grmsEggs 2NoFlour 110gmsVanilla 3drops

MethodMix the ingredients together and bake.

Page 19: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Cake & Cakes

Page 20: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Objects and Classes

• Classes – the “instructions”

• Objects – what is created– Use Plans to build House(s)– Use Recipe to bake Cake(s)

• Live in house, eat cake

Use a class to create object(s)Use a class to create object(s)Use a class to instantiate object(s)

Page 21: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Classes → Objects

• Remember …..

• As with house plans and cake recipe

• Use class to instantiate one or more objects

• Interact with the objects

• Not the class

Page 22: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Attributes

IngredientsButter 110grms Sugar 110grmsEggs 2NoFlour 110gmsVanilla Ess. 3drops

MethodMix the ingredients together and bake.

Vanilla Sponge Cake

IngredientsButter 110grms Sugar 110grmsEggs 2NoFlour 110gmsOrange Ess. 3drops

MethodMix the ingredients together and bake.

Orange Sponge Cake

Page 23: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Attributes

IngredientsButter 110grms Sugar 110grmsEggs 2NoFlour 110gmsFlavouring 3drops

MethodMix the ingredients together and bake.

Generic Cake

Define flavour at time of

instantiation

Page 24: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Inheritance

• So far – a plain sponge cake with flavouring.

• ? Iced cake• ? Cake with crushed nuts topping• Rewrite the recipe• No …

– Take the basic recipe and add on the topping part.

Page 25: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Inheritance

Sponge Cake

Nutted

Take “Sponge Cake”Add Nuts

Iced

Take “Sponge Cake”Add Icing

Page 26: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Implementation Hiding

• Interact with objects via “methods”

• Consider a watch– Q – How interact with a watch?

• Example on next slide …..

Page 27: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

A typical watch

Methods ?

Page 28: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Encapsulation

• Dictionary definition.– Encapsulate

“to express or show the most important facts about something”

Cambridge University Press Dictionary

http://dictionary.cambridge.org/

Page 29: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Code exercises – “Hello World”

// BrokenHello.cpp// This code is taken from "How not to programme in C++"// ISBN 1886411956

#include <iostream.h>

int main(void){

cout << "Hello World!/n";return (0);

}

Page 30: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Code exercises – classic error

// SimpleAccounting.cpp// This code is taken from "How not to program in C++"// ISBN 1886411956

#include <iostream.h>

int main(void){

// Amount owed (if any) by the userint amount;

cout << "Enter current balance: ";cin >> amount;

if(amount = 0)cout << "You owe nothing\n";

elsecout << "You owe " << amount << "\n";

return (0);}

Page 31: C++ Programming Concepts Lecture 1 Introduction. Module Introduction – Text Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S

Summary

• Overview of module– Concepts of OO – classes, objects etc.– Two pieces of code to try – and repair!