Download presentation
Presentation is loading. Please wait.
Published byPatricia Bradley Modified over 6 years ago
1
CS 13012 Computer Science IB: Object Oriented Programming
Final Exam 2
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
3
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
4
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)
5
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; };
6
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) {}
7
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;
8
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;
9
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
10
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();
11
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]; }
12
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];
13
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];
14
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);
15
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
16
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);
17
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
18
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
19
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.