Download presentation
Presentation is loading. Please wait.
Published byMyrtle Hart Modified over 6 years ago
1
Andy Wang Object Oriented Programming in C++ COP 3330
Test 2 Review Andy Wang Object Oriented Programming in C++ COP 3330
2
Arrays and Classes Arrays of objects
Know how to declare, initialize, and use With default constructor used Fraction num[3]; Explicitly call constructors in the { } Fraction num[3] = { Fractoin(2,3), Fraction(6), Fraction() }; Use like regular objects, but recognize each array element is an object for (int = 0; I < 3; i++) num[i].Show();
3
Arrays as Member Data Arrays can be member data of a class
If the array isn’t always full, use a tracking variable (to track how many slots used) Good for building types that use arrays, but also add in safeties, like boundary checking, in the member functions
4
Card Game Example and Array Techniques
Array of Card objects embedded inside Deck class “has a” relationship Array of Card objects embedded in Player class Arrays of objects (and use of dot-operator) Tracking variable numCards (in Player class) to track how many cards currently in the hand Tracking variable topCard (in Deck class) to index the deal position
5
Dynamic Memory Allocation and Classes
Memory Allocation Categories Static—compile time Sizes and types are known in advance Dynamic—run time Sizes and amounts can be set while the program is running
6
Dynamic Allocation, Deallocation
Create dynamic space with the new operator Always use a type after the word new The new operator also returns the address of the allocated space Use a pointer to store this address Can dynamically allocate basic variables, objects, and arrays Double *dptr = new double; int *list = new int[size]; Fraction *fptr1 = new Fraction; Fraction *fptr2 = new Fraction(1, 2); Date *birthdayList = new Date[20];
7
Dynamic Allocation, Deallocation
Can pass in parameters to constructors on dynamically allocated objects Deallocate dynamically allocated memory with the delete operator Apply delete to the pointer, and it deallocates the target Use delete [] for arrays
8
Notation Arrow operator p->Show() is the same as (*p).Show()
Like the dot operator, but for pointers p->Show() is the same as (*p).Show() Where p is a pointer, *p is the object, the Show() is a member function
9
Dynamically Resizing an Array
Dynamically create a new array of the needed size Another pointer is needed for this Copy the data from the old array to the new one Delete the old dynamic array (via delete) Change the pointer so that the new array has the right name
10
Analogy: Move to a New Apartment
Find a new apartment Move things over (copying in the case of an array) Cancel the lease for the old apartment Update your mailing address
11
Use Dynamic Memory Allocation inside a Class
Pointer(s) as member data Initializing pointers in the constructor(s) Null pointer, dynamic allocation of space Using correct cleanup in destructor Other member functions (memory management tasks) with allocation and deallocation Understand the memory layout of the object and dynamically allocated storage Phonebook database example: dynamic allocation, dynamic resizing of array, pass by address, protecting returned private array pointer, destructor
12
Automatic Functions Every class has these. If not user-provided, a default is built Constructor Destructor Copy constructor Assignment operator= Note Default version of constructor and destructor are empty Not every class has a default constructor Only if no constructor is provided
13
Automatic Functions Default version of copy constructor and assignment operator make a shallow copy Shallow copy Pointers and references copied verbatim End up pointing to the same attached data Deep copy Make copies of attached data as well (stored in heap) When dynamic allocation is done inside a class, deep copy functions are needed if copies of objects are to be allowed
14
Copy Constructor It is a constructor, invoked automatically
Invoked when a new copy of an object is created When an object is declared and initialized to another object’s value When an object is passed into or returned from a function by value Declaration format ClassName(const ClassName &); Parameter must be passed by a const reference Know how to define and to make deep copy
15
Assignment Operator Overload of operator=
Invoked when an assignment statement is made Written as a member function Declaration format ClassName &operator=(const ClassName &); Definition Similar to copy constructor in the making of a deep copy Needs to return *this, enabling cascading of = May have previous attached data to delete first Need to protect from self-assignment
16
Keyword this Pointer to the current calling object
*this is the calling object
17
C-strings vs. String Objects
Understand the implementation of c-style strings Understand the limitations and drawbacks of c-style strings Understand the use of classes in building a string type Internal implementation with character arrays Use of dynamic allocation for variable-length strings Destructor, copy constructor, assignment operator Operator overloads, and likely usage for strings
18
The Basics of Inheritance
The inheritance Relationship “is a” relationship Base classes and derived classes Derived classes inherits from the base class Represents idea of supertypes and subtypes Declaring derived classes class derivedName : public baseName Protection levels Public and private have usual meanings Protected members can be accessed by the class itself and by derived classes
19
The Basics of Inheritance
Constructors When a derived object is created, the base constructors run, too Constructor definitions run in top-to-bottom order Base class constructor first Destructors run in reverse order Derived class first, base class last
20
The Basics of Inheritance
Constructors with parameters For default constructors, the derived class constructor automatically calls the parent constructor Parameters can be passed when declaring a derived object Parameters are distributed up to the base constructors through an initialization list With explicit calls to the parent constructor Constructor bodies still run in normal order Base class first
21
The Basics of Inheritance
Defining derived classes Derived classes inherit all data and functions from the base class Except constructors, destructor, and assignment operator Can still write a new version of an existing base class function for the derived class (function overriding) Both base and derived classes can have their own version of a function with the same prototype Can distinguish between them with class name and scope- resolution operator Multiple inheritance Classes derived from more than one base class
22
Virtual Functions and Abstract Classes
Important pointer property Pointers to base class type can point at derived objects A base class reference variable can attach to a derived object Examples of benefits to the user Simplified storage, putting many base pointers into one container Heterogeneous list—an array of base class pointers, each can point to a different derived object Ability to write more versatile functions, with base pointer (or reference) parameters.
23
Virtual Functions Base class function declared to be virtual
Changes the binding of the call to the function definition from static (compile time) to dynamic (runtime) Compiler can only make decision based on the type of the calling object or pointer Virtual is needed because of the pointer property Base pointer pointing at derived object Virtual function called through base class pointer
24
Abstract Classes =0 on a function declaration means function will not be defined Turn a virtual function into a pure virtual function A class with at least one pure virtual function is an abstract class An abstract class cannot be instantiated
25
Polymorphism With OO programming, refers the use of the base pointer to child object property, along with virtual functions and function overriding to make the appropriate calls See the Employee example
26
Bitwise Operators Built-in operators that allow accessing and manipulating of individual bits Necessary because smallest variables that can be created are at least 1 byte Accessing individual bits can be useful for making more efficient algorithms, or using less storage space
27
The Bitwise Operators Bitwise &. Bitwise | Bitwise ^ (exclusive OR)
Performs the & operation on individual bits (1 is true, 0 is false) Bitwise | Performs the | operation on individual bits Bitwise ^ (exclusive OR) XOR is true if there is exactly one true and one false Complement ~ Reverses the bits of a variable (1 -> 0, 0 -> 1)
28
The Bitwise Operators << (left shift) >> (right shift)
Shifts the bits of a variable to the left >> (right shift) Shifts the bits of a variable to the right
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.