Test 2 Review Andy Wang Object Oriented Programming

  • Slides: 28
Download presentation
Test 2 Review Andy Wang Object Oriented Programming in C++ COP 3330

Test 2 Review Andy Wang Object Oriented Programming in C++ COP 3330

Arrays and Classes Arrays of objects Know how to declare, initialize, and use With

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();

Arrays as Member Data Arrays can be member data of a class If the

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

Card Game Example and Array Techniques Array of Card objects embedded inside Deck class

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 “has a” relationship Arrays of objects (and use of dot-operator) Tracking variable num. Cards (in Player class) to track how many cards currently in the hand Tracking variable top. Card (in Deck class) to index the deal position

Dynamic Memory Allocation and Classes Memory Allocation Categories Static—compile time Sizes and types are

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

Dynamic Allocation, Deallocation Create dynamic space with the new operator The new operator also

Dynamic Allocation, Deallocation Create dynamic space with the new operator The new operator also returns the address of the allocated space Always use a type after the word new 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 *fptr 1 = new Fraction; Fraction *fptr 2 = new Fraction(1, 2); Date *birthday. List = new Date[20];

Dynamic Allocation, Deallocation Can pass in parameters to constructors on dynamically allocated objects Deallocate

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

Notation Arrow operator Like the dot operator, but for pointers p->Show() is the same

Notation Arrow operator 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

Dynamically Resizing an Array Dynamically create a new array of the needed size Another

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

Analogy: Move to a New Apartment Find a new apartment Move things over (copying

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

Use Dynamic Memory Allocation inside a Class Pointer(s) as member data Initializing pointers in

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

Automatic Functions Every class has these. If not user-provided, a default is built Constructor

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

Automatic Functions Default version of copy constructor and assignment operator make a shallow copy

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

Copy Constructor It is a constructor, invoked automatically Invoked when a new copy of

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 Class. Name(const Class. Name &); Parameter must be passed by a const reference Know how to define and to make deep copy

Assignment Operator Overload of operator= Invoked when an assignment statement is made Written as

Assignment Operator Overload of operator= Invoked when an assignment statement is made Written as a member function Declaration format Class. Name &operator=(const Class. Name &); 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

Keyword this Pointer to the current calling object *this is the calling object

Keyword this Pointer to the current calling object *this is the calling object

C-strings vs. String Objects Understand the implementation of c-style strings Understand the limitations and

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

The Basics of Inheritance The inheritance Relationship “is a” relationship Base classes and derived

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 derived. Name : public base. Name Protection levels Public and private have usual meanings Protected members can be accessed by the class itself and by derived classes

The Basics of Inheritance Constructors When a derived object is created, the base constructors

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

The Basics of Inheritance Constructors with parameters For default constructors, the derived class constructor

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

The Basics of Inheritance Defining derived classes Derived classes inherit all data and functions

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

Virtual Functions and Abstract Classes Important pointer property Pointers to base class type can

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.

Virtual Functions Base class function declared to be virtual Changes the binding of the

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

Abstract Classes =0 on a function declaration means function will not be defined Turn

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

Polymorphism With OO programming, refers the use of the base pointer to child object

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

Bitwise Operators Built-in operators that allow accessing and manipulating of individual bits Necessary because

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

The Bitwise Operators Bitwise &. Bitwise | Performs the | operation on individual bits

The Bitwise Operators Bitwise &. Bitwise | Performs the | operation on individual bits Bitwise ^ (exclusive OR) Performs the & operation on individual bits (1 is true, 0 is false) XOR is true if there is exactly one true and one false Complement ~ Reverses the bits of a variable (1 -> 0, 0 -> 1)

The Bitwise Operators << (left shift) Shifts the bits of a variable to the

The Bitwise Operators << (left shift) Shifts the bits of a variable to the left >> (right shift) Shifts the bits of a variable to the right