0% found this document useful (0 votes)
73 views41 pages

G52CPP C++ Programming: DR Jason Atkin E-Mail: Jaa@cs - Nott.ac - Uk

This lecture provided an introduction and overview of the G52CPP C++ Programming course. It discussed the course web page, suggested texts, and assessment methods. It explained why C and C++ are still widely used in industry and provided a brief history of the development of C and C++. The lecture emphasized learning procedural C++ first before object-oriented concepts and focusing on understanding what the code is actually doing rather than just syntax.

Uploaded by

krishna sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views41 pages

G52CPP C++ Programming: DR Jason Atkin E-Mail: Jaa@cs - Nott.ac - Uk

This lecture provided an introduction and overview of the G52CPP C++ Programming course. It discussed the course web page, suggested texts, and assessment methods. It explained why C and C++ are still widely used in industry and provided a brief history of the development of C and C++. The lecture emphasized learning procedural C++ first before object-oriented concepts and focusing on understanding what the code is actually doing rather than just syntax.

Uploaded by

krishna sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

G52CPP

C++ Programming
Lecture 1

Dr Jason Atkin
E-Mail: [email protected]

1
This Lecture
• Course web page
• Why learn C/C++ ?
• History of C and C++
• What this module covers
• Suggested course texts
• Assessment methods
• My view of C/C++ vs Java
• ‘C++ Hello World’ (vs ‘C Hello World’)
2
Information
• Lecture slides, sample code and lab notes
will be on the module web page:
– http://www.cs.nott.ac.uk/~jaa/cpp/g52cpp.html
• Or google me
(Jason Atkin)
• And scroll
down to the
buttons on
my homepage
3
Information
• Web page contents:
– Lecture slides
– Labs information (close to the lab time)
– Coursework information (later on)
– Exam revision information (nearer the exams)
• WARNING: If lecture attendance is low I
will start to remove parts of the slides on
the web – so that you have to fill them in
• Any questions?
– Easiest way is to ask at the lab session
– Or email me: [email protected] 4
Five reasons to learn C & C++?
1. (Still) utilised in industry
– Why so popular? (after so long)
2. Choose the appropriate tool for the task
– Understand the Java/C# vs C/C++ differences
3. More programming practice
– Much is common across languages
4. Much is VERY similar to Java and C#
– Easier to learn: much will be familiar to you
5. Useful for other modules
– And for 3rd year projects 5
www.langpop.com, normalised, 2011

6
The aims of C and C++
• C still even more popular than C++ and Java
– Especially for operating systems and device drivers
– Where layout in memory matters – control needed
• C came first : with specific design aims
– Ability to write low-level code (e.g. O/S)
– Speed and efficiency
– Ease for programmers, rather than non-programmers
• Cross-platform compilation
– Compared with Assembly code
– Not as much as Java
• Why is C still so popular (over C++ and Java)?
– Control
– Anything you can do in C++ can be done in C
– But may need more code
7
The history of C/C++
In Bell Labs, ‘B’ language created, based on BCPL
1971-1973 : Dennis Ritchie extended ‘B’ to create ‘C’
Main features of C developed over this time
1973-1980 : New features were added
C needed to be standardised!
1979 : Bjarne Stroustrup (Bell labs) extended C to make ‘C with classes’
1982 : ‘K&R’ (Kernighan and Ritchie) unofficial C ‘standard’
1983 : ‘C with classes’ renamed C++, features still being added
1989 : ANSI standard C (started in 1983!) (=> ISO standard in 1990)
Differs in some ways from K&R ‘C’ and is often named ‘C89’
Together with Amendment 1, forms ‘C’ element of ‘C++’
1990s : C++ took centre stage (Standardisation progressing)
1994 : Standard Template Library makes it into the ISO standard C++
(Some template implementation arguments ongoing as late as 2003)
1995 : Java released by Sun
1998 : ISO standard C++ ratified (C++98)
1999 : New version of C standard (C99) (Differs from C++)
8
We will consider C++, G51PRG covered parts of C89
So why C++ rather than C?
• Since everything in C++ could be done in C, why learn
C++?

• C++ giving you higher level concepts


– Hides complexity
– Java hides even more and gives no choice but ‘do it my way’
– C++ keeps the ability to do things as you wish
• Higher level view is sometimes very useful, when large
amounts of code can be reused
– C++ Class libraries are ideal for a GUI on Windows, OS/X, Linux
(then decide appearance/speed vs portability)
• C++ also adds to C a lot of non-OO features
– e.g. templates, new/delete, operator overloading, references, …
– Useful for procedural programming as well as O.O.
9
Is C++ O.O. or Procedural?
• C is procedural (no classes, hard to do OO)
• C++ will let you do either
– You CAN write procedural C++
• Object oriented C++ ‘purists’ will frown at you
– Or you can write object oriented C++
– Or mix both together (often a bad idea)
• My view: Procedural or O.O. are ways of thinking
• How do you want to think of your program?
– A lot of communicating objects
– Or ‘do this then this then this…’
• Whichever you use: (within a thread)
– Functions are still called one at a time
– And operations are executed one at a time
10
OO – a good or bad thing?
• Object oriented techniques can hide some
of the complexity (a good thing?)
– Make it easier to understand a program (?)
– Make it easier to structure a large program (?)
– Some facilities hide what is actually happening,
to simplify things (bad?)
• We are going to look at C++ as a language
for programming, not at object oriented
design/programming
• Many courses start with object oriented C++
11
Aims: I want you to …
• Be able to understand and write C/C++ source code
– Good for employment prospects
• Know what you can do in C++
– Be able to look up ‘how’ if necessary
• Understand what your code actually does
– Know something of how C++ implements features
• Know the similarities and differences between C, C++
and Java
– Understand why Java changed things
• Practise programming (will help with Java and C too)
• I will not be teaching:
– Object oriented methodology
– Object oriented C++
– How to create C++ programs to satisfy purists
12
We will start with procedural C++
Not hiding what is really
happening

Understand what is
happening
inside/underneath C++

If you understand what it is


doing, you can diagnose
and fix your program when
it goes wrong

Otherwise you need to ask


for help
13
The Iceberg of C++
• We will only look at the top – the common things
• Many other features we will not cover

14
• We will also ignore object oriented theory (‘how’ not ‘why’)
Organisation
• 2 ‘new material’ lectures per week
– 4pm on Monday and Thursday
– Introducing C and C++ concepts
• 1 demo lecture – in a lecture room/slot
– 10am Tuesday
– Demonstrations and more practical
– Usually writing code, no slides
– Introduces the labs and coursework
• 2-hour lab session
– 11am Tuesday – after the demo lecture 15
How to learn C/C++
• Attend the lectures
• Read books & online web sites about C/C++
• But, you also need practice
– Try the samples from lectures
– Try sample programs of your own
• From books or online
– Change existing code
• Observe the effects

16
Exam Questions
• I will assume the following are valid for
exam questions:
– Things covered in the lectures
• Even if not on the lecture slides!
– Things in the samples or lab notes
• Especially the informal coursework
– The basic C/C++ language constructs
• You were introduced to many of these in G51CPP
• Operators, loops, conditionals, etc
– The common C library functions (part of C++)
• e.g. input/output functions, string functions
17
Feedback
• How do you know how well you are doing?

18
Feedback
• How do you know how well you are doing?

• Brilliant automated feedback device:


the compiler
– Check compilation warnings as well as errors!

• Utilise lab session helpers


– 2 hour session each week

• Submit informal coursework 19


Course Text

20
Recommended Course Text
• The Complete Reference: C++, Fourth Edition
by Herbert Schildt

– Similar structure to this module (C then C++)


– A reference, not a tutorial!

http://site.ebrary.com/lib/uon/docDe
tail.action?docID=10176700

21
Library:

The library has


electronic
copies of the
main module
text, available
for viewing
online

22
Other Texts (1)

• Many other C++ books


– But most books now introduce classes from the
start (so you think in an OO way)
– E.g. ‘C++ How to Program’, Deitel and Deitel
– Many books in the Jubilee library

• Other online references


– E.g. http://www.cplusplus.com/doc/tutorial/

23
Other Texts (2)

• ‘The C++ Programming Language’,


by Bjarne Stroustrup
– The definitive book on the language
– but not a tutorial (a technical reference)

• ‘Effective C++’ and ‘More Effective C++’,


by Scott Meyers
– Explain many confusing elements
– Ideal for understanding ‘why’ as well as ‘how’
– NOT an introduction to C++

24
Assessment

25
Course Assessment
• Course assessment is by
coursework (40%)
and exam (60%)

• Formal Coursework : 40%


– C++ programming
– A simple graphical program using the SDL
multimedia library and the supplied framework
• A lot of flexibility for you to choose how to meet the
requirements
26
C++ vs Java?

27
My view of C/C++ vs Java
• C++:
– Power and control: What to do? How to do it?
• Java:
– “Do it my way and I’ll do more of the work for you”
– But it may be less efficient than doing it yourself
– Some things cannot be done in Java alone (JNI)
• Java hides many things from you
– And decides how you will do things
• Java prevents you doing some things and
checks others
– C++ trusts that you know what you are doing
– If you do not, then you can REALLY break things
• Do you want/need the power/control of C++?
28
What is C++?
Procedural C Classes
Global Functions - Grouping of related data together
File-specific functions - With associated methods (functions)
Structs ‘new’ for object creation
Pointers (addresses) ‘delete’ for object destruction
Low-level memory access Constructors, Destructors
C Preprocessor Operator Overloading
Assignment operators
Variables Conversion operators
Arrays Inheritance (sub-classing)
Loops Virtual functions & polymorphism
Conditionals Access control (private/public/protected)

Function Libraries Templates Class Libraries


Standard functions (Generic classes) (+templated classes)
Custom libraries Standard library
O/S functions Non-C features Custom libraries
e.g. References Platform specific libraries 29
What about Java?
Procedural C Classes
Global Functions - Grouping of related data together
File-specific functions - With associated methods (functions)
Structs ‘new’ for object creation
Pointers (addresses) ‘delete’ for object destruction
Low-level memory access Constructors, Destructors
C Preprocessor Operator Overloading
Assignment operators
Variables Conversion operators (toString()?)
Arrays Inheritance (sub-classing)
Loops (ONLY) Virtual functions & polymorphism
Conditionals Access control (private/public/protected)

Function Libraries Templates Class Libraries


Standard functions ‘Generics’ (weaker) (Standardised)
Custom libraries Collections
O/S functions Non-C features Networking
Java Native Interface (ONLY) references Graphics 30
What Sun changed for Java
• Remember: C++ came first
– The Java changes were deliberate!
• Java is cross-platform
– Interpreted intermediate byte-code (.class files)
– Standard cross-platform class libraries
– Libraries include graphics (AWT, SWING, …), networking, …
– Platform independent type sizes
– Cannot take advantage of platform-specific features
• Java prevents things which are potentially dangerous
– Pointer arithmetic (but it can be fast)
– Writing outside arrays (checks take time)
– Low-level access to memory (dangerous per powerful)
– Uninitialised data (initialisation takes time)
• Java forces you to use objects
– Even when it would be quicker not to
• Java does garbage collection for you
– Safer(?), but may execute slower than freeing memory yourself 31
So which is better: Java or C++?
• What does ‘better’ mean?
• What are you trying to do?
• Do you need the power and control that
C++ gives you?
• With less options, things may seem simpler
– Potentially harder to make mistakes
– But you lose the flexibility to optimise
• If you know both, then you have more
options (and the basics are very similar)
32
“Hello World”

A simple C++ (and C) program

Since you have had 2 semesters to


forget what you did in G51PRG
33
The “Hello World” Program
#include <stdio.h> /* C file */

int main(int argc, char* argv[])


{ C version
printf("Hello world!\n");
return 0;
}
#include <cstdio> /* C++ file */

int main(int argc, char* argv[])


{
printf("Hello world!\n"); C++ version
return 0;
} 34
Compiling as C++
• Name the files .cpp instead of .c
• Can still include header files
• C preprocessor still exists
– Can still use #define
• Templates often make macros unnecessary
– Can still use #include
• But see next slide
• Conditional compilation is still needed in
order to avoid multiple header file inclusion
– i.e. #ifndef #define #endif
35
Using the C library functions
• The standard C library is still available
• Header files have changed names
– Add a c at the beginning and remove the .h
– e.g. #include <cstdio>
• C++ header files MAY differ from the C versions
– But provide the same functionality
– In C++ they may not actually be files
• Functions are in the std namespace
– We will consider namespaces later
• But, also available in global namespace
– So you can use them as global functions
• Need to link to the libstdc++ library in gcc
– e.g. gcc test.cpp –lstdc++ -o test
– or g++ test.cpp –o test 36
#include
#include <cstdio>
int main( int argc, char* argv[] )
{
printf("Hello world!\n");
return 0;
}

• Pre-processor command – processed before compilation


• It means: “Replace the statement “#include <…>” by the
contents of the file(?) called ‘cstdio’ BEFORE compiling”
• stdio.h/cstdio declare many STanDard Input and
Output functions, some other functions and some
constants 37
int main(int argc, char* argv[])
#include <cstdio>
int main( int argc, char* argv[] )
{
printf("Hello world!\n");
return 0;
}

• Define a function called main(), which returns a value of


type ‘int’ and has two parameters called argc and argv
• Your program will start with a call to your ‘main’ function
• argc and argv specify the command line arguments
• argc is of type ‘int’ and is the count of arguments
38
char* and argv
int main( int argc, char* argv[] )

• argv is of type ‘char*[ ]’,


– an array of ‘char*’s, or C-style strings
– The elements of the array are the command line
arguments
• Remember:
– char* is a pointer to a character
– In this case, a pointer to an array of characters
– With a value 0 at the end
• This is the only type of string available in C, but
C++ provides us with more possibilities
39
printf("Hello world!\n");
#include <cstdio>
int main( int argc, char* argv[] )
{
printf("Hello world!\n");
return 0;
}

• Send the string “Hello World” to the output stream


• This will usually be displayed in the output window
• Similar to the Java: System.out.print()
• BUT: The ‘f’ at the end of printf stands for ‘formatted’
• printf is a powerful function for formatting output
40
Next lecture(s)

• Demo lecture: what you should already


know : some reminders

41

You might also like