Upload
margery-bradley
View
219
Download
1
Embed Size (px)
Citation preview
C++ Programming Concepts
Lecture 1
Introduction
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
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
Some ‘C’ Revision
• Variable declarationsint nCount, iLoopCounter;
double dValue;
float fAmount;
long lBigValue;
int *ptrMyInt;
“C” – Hello World
#include <stdio.h>
void main(void)
{
printf(“Hello World.\n”);
}
“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
“C” – Hello World : Functions#include <stdio.h>
void fnSayHello(void);
void main(void)
{
fnSayHello();
}
void fnSayHello(void)
{
printf(“Hello World. \n”);
}
“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
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*/
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
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
Naming
• Hungarian Notation– Microsoft Web Site
• Simplified– Chuck Sphar– Cut down version
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….
Objects and Classes
• What is an Object?
• What is a Class?
• Some examples …..
Building Plans
House
Houses
Doing a “Delia”
IngredientsButter 110grms Sugar 110grmsEggs 2NoFlour 110gmsVanilla 3drops
MethodMix the ingredients together and bake.
Cake & Cakes
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)
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
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
Attributes
IngredientsButter 110grms Sugar 110grmsEggs 2NoFlour 110gmsFlavouring 3drops
MethodMix the ingredients together and bake.
Generic Cake
Define flavour at time of
instantiation
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.
Inheritance
Sponge Cake
Nutted
Take “Sponge Cake”Add Nuts
Iced
Take “Sponge Cake”Add Icing
Implementation Hiding
• Interact with objects via “methods”
• Consider a watch– Q – How interact with a watch?
• Example on next slide …..
A typical watch
Methods ?
Encapsulation
• Dictionary definition.– Encapsulate
“to express or show the most important facts about something”
Cambridge University Press Dictionary
http://dictionary.cambridge.org/
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);
}
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);}
Summary
• Overview of module– Concepts of OO – classes, objects etc.– Two pieces of code to try – and repair!