Andy Wang Object Oriented Programming in C++ COP 3330

Slides:



Advertisements
Similar presentations
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Advertisements

Classes: A Deeper Look Systems Programming.
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.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 12: Adding Functionality to Your Classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
 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.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
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.
More C++ Features True object initialisation
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
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.
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.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
ECE 264 Object-Oriented Software Development
Learners Support Publications Constructors and Destructors.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Design issues for Object-Oriented Languages
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Andy Wang Object Oriented Programming in C++ COP 3330
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
7. Inheritance and Polymorphism
Object-Oriented Programming
classes and objects review
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Automatics, Copy Constructor, and Assignment Operator
Introduction to Classes
Automatics, Copy Constructor, and Assignment Operator
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Andy Wang Object Oriented Programming in C++ COP 3330
Constructors and destructors
Constructors and Destructors
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Object-Oriented Programming in PHP
CISC/CMPE320 - Prof. McLeod
9-10 Classes: A Deeper Look.
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Andy Wang Object Oriented Programming in C++ COP 3330
COP 3330 Object-oriented Programming in C++
A Deeper Look at Classes
COP 3330 Object-oriented Programming in C++
Chapter 9 Introduction To Classes
Class: Special Topics 2 For classes using memory allocation
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Presentation transcript:

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 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 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 “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

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

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

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

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

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

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

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