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

Slides:



Advertisements
Similar presentations
Brown Bag #3 Return of the C++. Topics  Common C++ “Gotchas”  Polymorphism  Best Practices  Useful Titbits.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Classes Separating interface from implementation
Computer Science II Exam I Review Monday, February 6, 2006.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
OOP Languages: Java vs C++
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CS 450 MPX P ROJECT A Quick C Review. P OINTERS AND ADDRESSES IN C Check Manual I2 from Dr. Mooney’s website for a complete review of C topics you will.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Dynamic memory allocation and Pointers Lecture 4.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Programming in Java CSCI-2220 Object Oriented Programming.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Chapter 9 Classes: A Deeper Look, Part I Part II.
#include guards Practical session #2 Software Engineering
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Classes, Arrays & Pointers. Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Structs in C Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens struct Properties The C struct mechanism is vaguely similar.
OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun  Aggregation.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Classes C++ representation of an object
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Introduction to Classes
Introduction to Classes
Basic C++ What’s a declaration? What’s a definition?
9-10 Classes: A Deeper Look.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Presentation transcript:

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

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)

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

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_GUARD class myclass { … }; #endif

System specific code #ifdef __linux__ //linux code #elif _WIN32 // windows code #elif __ANDROID__ //android code #else

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

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

Common errors Compiler errors Linker errors Execution error Logical error

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

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

library / / 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 values  Requires dereferencing to assign values

Pointers

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

Arrays 0x C1 (0) 0x D7 (1) 0x E2 (2) 0x F9 (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 array for(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 inherit  Constructors  Destructors  Copy constructors  Friends

Instantiation of a subclass