c++ programming concepts lecture 1 introduction. module introduction – text deitel, h.m. &...
TRANSCRIPT
![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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/1.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/2.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/3.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/4.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/5.jpg)
“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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/6.jpg)
“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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/7.jpg)
“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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/8.jpg)
“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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/9.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/10.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/11.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/12.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/13.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/14.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/15.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/16.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/17.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/18.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/19.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/20.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/21.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/22.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/23.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/24.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/25.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/26.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/27.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/28.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/29.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/30.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062320/56649cf75503460f949c6e3a/html5/thumbnails/31.jpg)
Summary
• Overview of module– Concepts of OO – classes, objects etc.– Two pieces of code to try – and repair!