ALL YOU NEED TO KNOW-CPP PDF

Title ALL YOU NEED TO KNOW-CPP
Course C++ Programming
Institution University of Nottingham
Pages 43
File Size 5 MB
File Type PDF
Total Downloads 31
Total Views 201

Summary

ALL YOU NEED TO KNOW FOR G52CPP...


Description

ALL YOU NEED TO KNOW FOR C++

Table of Contents Topics to Cover ........................................................................................................................... 3 Example Questions Types ........................................................................................................ 4 Chapter 1 - 7 .................................................................................................................................... 6 Chapter 8 - 14................................................................................................................................ 23 Chapter 15 -20............................................................................................................................... 36

Topics to Cover • Pointers (object references) • C-style strings • Pointer arithmetic • The stack • structs and unions – malloc(), sizeof() • Classes – Constructors – Inline functions – Definitions vs declarations • References ( & ) • new and delete • The this pointer • static members • const • friend • Standard library – Streams – STL classes (lab 3) • Inheritance and virtual functions • Multiple Inheritance • Function pointers • Vtables • Default member functions • Casting • Exceptions • Operator overloading • Template functions and classes • Functors and Lambda functions • The slicing problem • RAII and smart pointers Know basics of Standard Template Library -> a question using this, or to use this – i.e. container classes and algorithms – Ensure that you understand the content of lab 3 (on STL) – Know at least the things that you used in lab 3: • list, array, vector • for_each, count, count_if, sort, copy functors and/or lambda functions somewhere

Example Questions Types • “This code should …., what is wrong with it?” • “Will this code compile? If so, what will the output be? If not then why not and explain how to fix the problem” • “Consider this code …., provide an implementation for the … function, which will …” • “Provide a definition for a function which…” • “Provide a definition for a class which ….” • “Provide a definition for the member function … OUTSIDE of the class definition” • “What does … mean in C++” • “What is the difference between … and …” • “What is wrong with this code…” Many examples could be easily generated: – References or pointers vs pass by value – static v non-static local variables – virtual v non-virtual functions – static v non-static member variables – Exception throwing and catching – Pointer arithmetic (espec. ++) or pointers to pointers • Identify what is used in the code sample – Look for subtleties – These often test knowledge indirectly – Test your ability to apply your knowledge rather than to memorise • What is needed/wrong in a sample? E.g.: – Was something static and needed to be non-static? – Did it need to be pass or return by reference and wasn’t? – Did it need to be virtual and wasn’t? • What is the difference between a class and a struct in C++? • What is the :: operator used for in C++? Give two examples of its use. • What is the difference between protected and private member data in C++? • What is meant by the term ‘overloaded function’ in C++? • Name four methods which may be created implicitly by a C++ compiler for a C++ class if they are needed. • Or “In addition to a default (no-parameter) constructor and a destructor, name two other methods which may be created implicitly by a C++ compiler for a C++ class if they are needed.” • If you add a member function to a class, will the objects grow? Why/why not? • What is the difference between a static and a non-static member function? • What is the difference between a global function and a static member function? • What does ‘friend’ do in C++? • What is meant by “an overridden function” in C++? Give an example. • What is a virtual destructor? When would it be useful? • Why should you not call a virtual function from a destructor? • Why is Java’s ‘finally’ not needed in C++>?



Write a function called min() which takes two long parameters called val1 and val2 and returns the value of the lesser of the two values as a long, using the ternary operator ( ?: ). (i.e. it should return the minimum of the two values.)



3e: Provide the code for a macro called MIN using #define which will perform the same function as your min() function in question 3d.



Provide C/C++ code to define a macro (using #define) called PRODUCT, which takes two parameters and returns the product of the two parameters. e.g. PRODUCT(2,3) is 6, PRODUCT(1,5) is 5 and PRODUCT(-3,4) is -12



Answer: #define PRODUCT(a,b) ((a)*(b))

Chapter 1 - 7 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/quick) – 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 Some guarantees are given: – A minimum size (bits): char 8, short 16, long 32 – Relative sizes: char ≤ short ≤ int ≤ long – – – •

16 bit operating systems 32 bit operating systems 64 bit operating systems sizeof() operator exists

usually usually usually to tell

use 16 use 32 use 64 us the

bit int bit int bit int size

Short s1 = 965; Short* ps1 = &s1; *ps1 = 4;//s1=4

Global variables can be access anywhere, by declaring them and the linker will do the work To access outside the file, use the keyword in a declaration extern changes a definition into a declaration To hide it from other files, use static (encapsulation)

Malloc() will allocate bytes of memory, should include

return a void*

Because in this case the string is an array, so it need to reallocate the memory size (maybe smaller before)

The difference is (ONLY!!!) in encapsulation – struct defaults to public, class to private • Common coding practice in C++: – Data only and no member functions: use a struct • You get the guarantees about size and positions of member data that you get in a C struct

Chapter 8 - 14 REFERENCE(&) A way to give a new name to an item

Act like pointers – To work out what will happen with a reference, think “what would happen if it was a pointer”

IN A CONST function, a value cannot be alter, therefore, setter can't be use. If want to alter the const member function, declare as mutable: Mutable int _lastgot;

Namespaces are used to avoid name conflicts --> only the name is affected

Can avoid needing to keep saying :: specify ‘using namespace ’ – From that point onwards the namespace will be checked when resolving names

Append(),substr(),insert(), replace(), erase(), compare(),find(), c_str()

Chapter 15 -20

Exceptions are ‘thrown’ to report exceptional circumstances

Report errors: 1. Return an error value from function 2. Set a global error code 3. Throw an exception Return an error value from function • Remember to check return value on each call • Must have a valid 'error' return value Set a global error code • Have to remember to check it after each call Throw an exception • Requires an exception handling mechanism

Catching exceptions • Specify that you want to check for exceptions (try) • Then call the code which MAY raise the exceptions • Then specify which exceptions you will CATCH, and WHAT TO DO o Use throw without arguments in a catch clause to RETHROW...


Similar Free PDFs