Date post: | 11-Jan-2016 |
Category: |
Documents |
Upload: | lesley-ford |
View: | 212 times |
Download: | 0 times |
Recap, Test 1 prep, Composition and Inheritance
Dates
Test 1 – 12th of March Assignment 1 – 20th of March
Test 1 contents
Syntax checking Error identification Code correction Problem solving Code identification Pass by value and Pass by reference Const Pointers will not be tested
Assignment 1 requirements
GUI Software DesignAt least ONE c++ GUI referencedDesign pattern(s) mentionedUML diagram of your proposed systemParagraph on event programming (ideally
reference sources)
Assignment 1 requirements
GUI DesignAt least ONE cue game referenced (game
and possibly real life)Diagram of interface placementBrief discussion
User interface controls
Classes Private default scope Automatically implements a default
constructor and copy constructor Uses a destructor Instantiated using myclass a; or myclass
b(); Header file contains the definitions .cpp (Source) file has the declarations
Classes
Inline functions are declared in the header file A class definition is always ended with a
semicolon ; If an overloaded constructor is implemented the
default will not be available unless explicitly defined and declared
Every member function has access to the THIS pointer this->getVar() or (*this).getVar()
Header example
CPP example
Scope
Local – In a function Global – Outside a function and
class/namespace
Macros
#include #define #ifndef
#ifndef _INCL_GUARD #define _INCL_GUARDclass myclass{…};#endif
System specific code
#ifdef __linux__ //linux code #elif _WIN32 // windows code #elif __ANDROID__ //android code#else
http://sourceforge.net/p/predef/wiki/OperatingSystems/
#define
Define constant functions#define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b))
#define SWAP(a, b) { a ^= b; b ^= a; a ^= b; } Define constant variables
#define GRAVITY 8.9f#define OOPS SWAP;
Typedef
typedef int i32; typedef long long l64;
e.g. l64 mylong = 123323;
Common errors
Compiler errors Linker errors Execution error Logical error
Compiler errors Typographical errors
> instead of >> Forgetting ;
Other errorsNot declaring a variable Not defining a function before mainTrying to access private variables Including the wrong header fileMismatched braces {Ambiguous function calls
Linker errors
A function with no declaration A library not linked too
Execution errors Out of array bounds Assigning a NULL pointer, a value
Logical errors
while (x=y) Iterating an address of an array out of
bounds
<cmath> <math.h> library
http://www.cplusplus.com/reference/cmath/
trunc, ceil, pow, cos, tan, log
Const void myfunc(const int & val); //passes a
const reference void myfunc(const int val); //passes a
constant value int getGravity() const; //a function with a
const return int const * const myvar; //constant pointer int const * myvar; //variable pointer const char * myvar;
Strings Has a member char* (c-strings are char*) Is a class: string mystr(“words”); Is an array uses mystr[0] notation Uses mystr.push_back(char); + concatenates strings and characters .find(“str”) returns position of str in string .substr(position, length)
Passing by value
Makes a copy of the variable passed into the function
Does not change the original variable
Passing by reference
Passes ONLY the variable we want to change
Does not make a copy
Passing by address
Passes the memory address Treats the variables passed as addresses
not valuesRequires dereferencing to assign values
Pointers
1729
Address Value
NULL
Computer c = new Computer();
int x =1729;int* z = &x;
Arrays
0x6339392C 1 (0)
0x6339392D 7 (1)
0x6339392E 2 (2)
0x6339392F 9 (3)
Array name
Multidimensional arrays
Int twoD[2][3]; twoD[0][2] = 5; twoD[1][1] = 3; int twoD [2][3] = { 1, 7, 2, 9, 2, 1, }; int twoD [2][3] = { {1, 7, 2}, {9, 2, 1} };
int ** twoD = new int*[2];for(int i=0; i < 2; ++i){
twoD[i] = new int[3];}
X size
Y size
2D array using dynamic memory
Clean up
For each array delete[] the arrayfor(int i=0; i < 2; ++i){
delete[] twoD[i];}delete[] twoD;
Arrays can be passed in functions as func(int[] myarr)
However func(int[][] my2darray) is invalid func(int** my2darray) is valid syntax
Various examples of Prac 2 Q1+Q2
Inheritance
Inheritance allows code reuse by enabling the creation of sub-classes based on base classes
Syntax of inheritance:class subclass: access_level baseclass1,access_level
baseclass2{};
Class Dog : public Mammal, public Canine Avoid multiple inheritance
Inheritance
When a sub class is instantiated the base class constructor is called by default
The access level describes the maximum access level that members of the sub class will have
The default access level is private for classes and public for structs
Sub classes
Do not inheritConstructorsDestructorsCopy constructorsFriends
Instantiation of a subclass