38
Recap, Test 1 prep, Composition and Inheritance

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

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

Header example

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

CPP example

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

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

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

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

#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

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

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

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

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

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

<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

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

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

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

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

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

Pointers

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

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

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

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

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

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

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

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

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

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

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

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
Page 38: Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March