Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20...
-
Upload
lesley-ford -
Category
Documents
-
view
213 -
download
0
Transcript of Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20...
![Page 1: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/1.jpg)
Recap, Test 1 prep, Composition and Inheritance
![Page 2: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/2.jpg)
Dates
Test 1 – 12th of March Assignment 1 – 20th of March
![Page 3: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/3.jpg)
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
![Page 4: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/4.jpg)
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)
![Page 5: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/5.jpg)
Assignment 1 requirements
GUI DesignAt least ONE cue game referenced (game
and possibly real life)Diagram of interface placementBrief discussion
User interface controls
![Page 6: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/6.jpg)
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
![Page 7: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/7.jpg)
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()
![Page 8: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/8.jpg)
Header example
![Page 9: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/9.jpg)
CPP example
![Page 10: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/10.jpg)
Scope
Local – In a function Global – Outside a function and
class/namespace
![Page 11: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/11.jpg)
Macros
#include #define #ifndef
#ifndef _INCL_GUARD #define _INCL_GUARDclass myclass{…};#endif
![Page 12: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/12.jpg)
System specific code
#ifdef __linux__ //linux code #elif _WIN32 // windows code #elif __ANDROID__ //android code#else
http://sourceforge.net/p/predef/wiki/OperatingSystems/
![Page 13: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/13.jpg)
#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;
![Page 14: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/14.jpg)
Typedef
typedef int i32; typedef long long l64;
e.g. l64 mylong = 123323;
![Page 15: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/15.jpg)
Common errors
Compiler errors Linker errors Execution error Logical error
![Page 16: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/16.jpg)
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
![Page 17: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/17.jpg)
Linker errors
A function with no declaration A library not linked too
Execution errors Out of array bounds Assigning a NULL pointer, a value
![Page 18: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/18.jpg)
Logical errors
while (x=y) Iterating an address of an array out of
bounds
![Page 19: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/19.jpg)
<cmath> <math.h> library
http://www.cplusplus.com/reference/cmath/
trunc, ceil, pow, cos, tan, log
![Page 20: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/20.jpg)
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;
![Page 21: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/21.jpg)
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)
![Page 22: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/22.jpg)
Passing by value
Makes a copy of the variable passed into the function
Does not change the original variable
![Page 23: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/23.jpg)
Passing by reference
Passes ONLY the variable we want to change
Does not make a copy
![Page 24: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/24.jpg)
Passing by address
Passes the memory address Treats the variables passed as addresses
not valuesRequires dereferencing to assign values
![Page 25: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/25.jpg)
Pointers
![Page 26: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/26.jpg)
1729
Address Value
NULL
Computer c = new Computer();
int x =1729;int* z = &x;
![Page 27: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/27.jpg)
Arrays
0x6339392C 1 (0)
0x6339392D 7 (1)
0x6339392E 2 (2)
0x6339392F 9 (3)
Array name
![Page 28: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/28.jpg)
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} };
![Page 29: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/29.jpg)
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
![Page 30: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/30.jpg)
Clean up
For each array delete[] the arrayfor(int i=0; i < 2; ++i){
delete[] twoD[i];}delete[] twoD;
![Page 31: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/31.jpg)
Arrays can be passed in functions as func(int[] myarr)
However func(int[][] my2darray) is invalid func(int** my2darray) is valid syntax
![Page 32: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/32.jpg)
Various examples of Prac 2 Q1+Q2
![Page 33: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/33.jpg)
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
![Page 34: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/34.jpg)
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
![Page 35: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/35.jpg)
Sub classes
Do not inheritConstructorsDestructorsCopy constructorsFriends
![Page 36: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/36.jpg)
Instantiation of a subclass
![Page 37: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/37.jpg)
![Page 38: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.](https://reader036.fdocuments.net/reader036/viewer/2022070408/56649e535503460f94b49da6/html5/thumbnails/38.jpg)