CS Computer Science IB: Object Oriented Programming

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Pointer Data Type and Pointer Variables
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
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+
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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?
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
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.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact) l they serve the same purpose as arrays but using.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Yan Shi CS/SE 2630 Lecture Notes
Learning Objectives Pointers as dada members
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?
CS 215 Final Review Ismail abumuhfouz Fall 2014.
ECE 264 Object-Oriented Software Development
Motivation and Overview
CISC181 Introduction to Computer Science Dr
classes and objects review
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?
Class: Special Topics Copy Constructors Static members Friends this
Pointers Revisited What is variable address, name, value?
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
LinkedList Class.
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
Multidimensional Arrays Vectors of Vectors
Vectors the better arrays.
Copy Constructor CSCE 121 J. Michael Moore.
Structures putting data together.
Classes, Constructors, etc., in C++
Jordi Cortadella and Jordi Petit Department of Computer Science
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
Lists - I The List ADT.
Lists - I The List ADT.
CS148 Introduction to Programming II
Arrays Arrays A few types Structures of related data items
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 9: Pointers and String
COP 3330 Object-oriented Programming in C++
Pointers and dynamic objects
CS 144 Advanced C++ Programming February 12 Class Meeting
Lesson 25 Miscellaneous Topics
CS31 Discussion 1D Winter18: week 6
Pointers and References
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
How Dynamic Memory Works with Memory Diagram
Copy Constructor CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
CS 144 Advanced C++ Programming April 30 Class Meeting
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Vectors the better arrays.
Presentation transcript:

CS 13012 Computer Science IB: Object Oriented Programming Final Exam 2

Structures Structure declaration Structure variable declaration struct CDAacct{ int balance; int rate; int term; }; Structure variable declaration CDAcct myacct, youracct; How to access members (or member variables) of the structure variable? myacct.balance

Structures (cont'd) Initialization of structure variable CDAcct myacct={1000, 9, 12}; Assignment of structure variables CDAcct youracct; youracct=myacct; Structure variables cannot be directly compared if (youracct == myacct) // error Structure can be used as parameters of a function (passing by reference or by value) and can be also returned from the function Complex structures Members of a structure can be other structures or arrays

Classes Class definition Object class Date { // class name public: // ignore this for now void set(int, int, int); int getDay(); int month; int day; int year; }; // don’t forget the semicolon Member variables Member functions Access control: public, private Object Date mybday; Each object belongs to a class; each class can have multiple objects (instances)

Classes (cont'd) Access member variables/functions in the object mybday.set(10, 26, 68); cout << mybday.day; Member function declaration In-line definition Out-of-line definition void Date::set(int m, int d, int y){...} Mutator vs. accessor class Date { public: // mutator void set(int, int, int); // mutator int getMonth() const; // accessor int getDay() const {return day;} // accessor in-line private: int month; int day; int year; };

Classes (cont'd) Classes with objects Constructors Destructors Member variables can be objects of other classes Constructors void or default constructor Date(); // constructor in class Date Normal constructor Date(int, int, int); // constructor in class Date Copy constructor Date(const Date &); Destructors ~Date(); Initializer list Date(int m, int d, int y): month(m), day(d), year(y) {}

Pointers Pointer declaration Pointer initialization Use the pointer int *p; Pointer initialization int i; int *p = & i; Assign an array to a pointer: int arr[3]; p=arr; Use the pointer *p= 1; cout << *p<<endl;

Pointers (cont'd) Pointer arithmetic int a[5], *p = a; p = p + 1; p = p – 1; p++; p+=2; Null pointer or loose pointer problem int *ptr; *ptr = 5; // ERROR - loose pointer! Memory leak problem int *ptr = new int[3]; int *ptr2 = new int[10]; ptr = ptr2;

Dynamic Memory Allocation new – allocate memory space int *ip; // declares pointer ip = new int; // ip points to integer variable int *ip2 = new int(5); // 5 assigned to the var int *ip3 = new int[10]; // allocate an array of size 5 Myclass * obj = new Myclass; // dynamically create an object delete – deallocate memory space delete ip; delete ip2; delete [] ip3; Memory leak problem (see previous slide) Pointers can be passed into the function by reference or by value, and be returned from a function

Dynamic Memory Allocation (cont'd) Dynamic objects myclass *mp1; mp1 = new myclass(5); Access member variables/functions for an object pointer cout << mp1->getdata(); (*mp1).getdata();

Objects Containing Dynamically Allocated Members Dynamically allocate an array within an object class MyClass{ public: MyClass(int); // constructor ~MyClass(); // destructor MyClass(const MyClass&); // copy constructor private: int *d; int size; }; // constructor MyClass::MyClass(int n){ size=n; d = new int[size]; }

Objects Containing Dynamically Allocated Members (cont'd) Destructor MyClass::~MyClass(){ delete [] d; } Copy constructor Pass an object into a function by value Return an object by a function MyClass::MyClass(const MyClass& org){ size=org.size; d = new int[size]; for(int i=0; i< size; ++i) d[i]=org.d[i];

Vectors Vector vs array Vector declaration Vector operations Why vector? Vector declaration vector<int> items; // declares vector with no elements vector<double> items(5); // declares vector with 5 elements vector<myclass> items(5); // declares a vector of 5 objects of myclass Vector operations items [3];

Vectors (cont'd) Vector functions return the vector size: items.size(); adding element: items.push_back(55); removing element: items.pop_back(); emptying: items.erase(); Deleting specific location: items.erase(item_iterator);

Vectors (cont'd) Iterators The usage of vector iterators vector<typeParameter>::iterator iteratorName; e.g., vector<int>::iterator ip; v.begin() returns an iterator pointing to the first element of container v v.end() returns an iterator pointing past the last element of container v useful to compare for end of iteration Insert: v.insert(ip, 22); // inserts one element at // iterator ip position v.insert(v.end()-2, 55); // ? Delete: v.erase(ip); // erases one element at // iterator ip position The usage of vector iterators

Multidimensional Arrays, Vectors of Vectors Multidimensional array declaration int a[3][4]; How to access or iterate over elements in the multidimensional array? Nested for loop Vector of vectors How to create and initialize a vector of vectors? vector<vector<int>> a; vector<int> row(width); for(int i=0; i < length; ++i) a.push_back(row);

Multidimensional Arrays, Vectors of Vectors (cont'd) Ragged array Rows can be of different sizes vector<int> row; vector<vector<int>> a; for(int i=0; i < 4; ++i){ row.push_back(i); a.push_back(row); } See the code on vector of vectors, iterators, and ragged array in lecture slides

Namespaces Name collision problem Three different styles of using namespaces std is one of examples std::cout<<"hello"<<std::endl; using std::cout; using std::endl; // at the beginning of the program using namespace std; // at the beginning of the program

Recursion A function calls itself Examples Attention: base case Function write_vertical() in the lecture slides Function of computing factorial of n (i.e., n!) Attention: base case