Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.

Similar presentations


Presentation on theme: "Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March."— Presentation transcript:

1 Recap, Test 1 prep, Composition and Inheritance

2 Dates Test 1 – 12 th of March Assignment 1 – 20 th of March

3 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

4 Assignment 1 requirements GUI Software Design  At least ONE c++ GUI referenced  Design pattern(s) mentioned  UML diagram of your proposed system  Paragraph on event programming (ideally reference sources)

5 Assignment 1 requirements GUI Design  At least ONE cue game referenced (game and possibly real life)  Diagram of interface placement  Brief discussion User interface controls

6 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

7 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()

8 Header example

9 CPP example

10 Scope Local – In a function Global – Outside a function and class/namespace

11 Macros #include #define #ifndef  #ifndef _INCL_GUARD #define _INCL_GUARD class myclass { … }; #endif

12 System specific code #ifdef __linux__ //linux code #elif _WIN32 // windows code #elif __ANDROID__ //android code #else http://sourceforge.net/p/predef/wiki/OperatingSystems/

13 #define Define constant functions #define MIN(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;

14 Typedef typedef int i32; typedef long long l64; e.g. l64 mylong = 123323;

15 Common errors Compiler errors Linker errors Execution error Logical error

16 Compiler errors Typographical errors  > instead of >>  Forgetting ; Other errors  Not declaring a variable  Not defining a function before main  Trying to access private variables  Including the wrong header file  Mismatched braces {  Ambiguous function calls

17 Linker errors A function with no declaration A library not linked too Execution errors Out of array bounds Assigning a NULL pointer, a value

18 Logical errors while (x=y) Iterating an address of an array out of bounds

19 library http://www.cplusplus.com/reference/cmath / http://www.cplusplus.com/reference/cmath / trunc, ceil, pow, cos, tan, log

20 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;

21 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)

22 Passing by value Makes a copy of the variable passed into the function Does not change the original variable

23 Passing by reference Passes ONLY the variable we want to change Does not make a copy

24 Passing by address Passes the memory address Treats the variables passed as addresses not values  Requires dereferencing to assign values

25 Pointers

26 1729 Address Value NULL Computer c = new Computer(); int x =1729; int* z = &x;

27 Arrays 0x6339392C1 (0) 0x6339392D7 (1) 0x6339392E2 (2) 0x6339392F9 (3) Array name

28 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} };

29 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

30 Clean up For each array delete[] the array for(int i=0; i < 2; ++i) { delete[] twoD[i]; } delete[] twoD;

31 Arrays can be passed in functions as func(int[] myarr) However func(int[][] my2darray) is invalid func(int** my2darray) is valid syntax

32 Various examples of Prac 2 Q1+Q2

33 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

34 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

35 Sub classes Do not inherit  Constructors  Destructors  Copy constructors  Friends

36 Instantiation of a subclass

37

38


Download ppt "Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March."

Similar presentations


Ads by Google