SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Advertisements

Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Classes: A Deeper Look Systems Programming.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
1 Chapter 14-2 Object- Oriented Software Development Dale/Weems.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Structured Types, Data Abstraction and Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Pointers You are not responsible for virtual functions (starting on.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
1 Data Structures and Algorithms Week 3 Data Abstraction: Part II Structured Types, and Classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Structural Types, Data Abstractions and Classes.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
1 Final Exam Tues 3/16/10 (2-3:50) (Same classroom) Old Textbook - Chapters 11-16, 18 Focus is on 15, 16 and 18 Final Exam Tues 3/16/10 (2-3:50) (Same.
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 Review: C++ class represents an ADT 2 kinds of class members: data members and function members Class members are private by default Data members are.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
1 Structured Types, Data Abstraction and Classes.
Chapter 12 Classes and Abstraction
Dale/Weems/Headington
Review: Two Programming Paradigms
Chapter Structured Types, Data Abstraction and Classes
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Introduction to Structured Data Types and Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
C++ Plus Data Structures
Review: C++ class represents an ADT
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required

Topics Covered C++ basic programming concepts Struct and Union Concepts C++ Classes Inheritance Object-Oriented Programming Pointers You will have to write a class You will have to write a struct

Structured Data Type Definition: A structured data type is a type in which each value is a collection of component items. The entire collection has a single name each component can be accessed individually Know what is meant by a structured data type. You do not need to create a struct or use one on the final exam.

Accessing struct Members Dot ( period ) is the member selection operator. After the struct type declaration, the various members can be used in your program only when they are preceded by a struct variable name and a dot. EXAMPLES thisAnimal.weight anotherAnimal.country

Abstraction Abstraction is the separation of the essential qualities of an object from the details of how it works or is composed It focuses on what, not how It is necessary for managing large, complex software projects

Abstract Data Type (ADT) Is a programmer-defined type with a set of values and allowable operations for the type. Some ways to define a new C++ type are: using struct using class

ADT Implementation means Choosing a specific data representation for the abstract data using data types that already exist (built-in or programmer-defined) Called “Data Members” Writing functions (member functions) for each allowable operation

Information Hiding Class implementation details are hidden from the client’s view. This is called information hiding. Public functions of a class provide the interface between the client code and the class objects. client code abstraction barrier specification implementation

Benefits of information hiding Data and details can be concealed from the client of the abstraction. Code can be changed without affecting the client because the specification and interface are unchanged.

class TimeType Specification // Specification File ( timetype.h ) class TimeType // declares a class data type { // does not allocate memory public : // 5 public function members void Set ( int hours , int mins , int secs ) ; void Increment ( ) ; void Write ( ) const ; bool Equal ( TimeType otherTime ) const ; bool LessThan ( TimeType otherTime ) const ; private : // 3 private data members int hrs ; int mins ; int secs ; } ;

Use of C++ data Type class Facilitates re-use of C++ code for an ADT Software that uses the class is called a client Variables of the class type are called class objects or class instances Client code uses public member functions to handle its class objects

Using class A class is a programmer-defined type whose components (called class members) can be variables or functions. Class members are private by default. Compiler does not permit client code to access private class members. Class members declared public form the interface between the client and the class. In most classes, the private members contain data, and the public members are functions to manipulate that data.

Member functions categorized by task CONSTRUCTOR -- a member function that actually creates a new instance and initialized some or all of its data members ACCESS FUNCTION or OBSERVER -- a member function that can inspect (use but not modify) the data members of a class without changing their values. Such a function is declared with const following the parameter list in both the specification and the implementation files.

Client Code Using TimeType #include “timetype.h” // includes specification of the class using namespace std ; int main ( ) { TimeType currentTime ; // declares 2 objects of TimeType TimeType endTime ; bool done = false ; currentTime.Set ( 5, 30, 0 ) ; endTime.Set ( 18, 30, 0 ) ; while ( ! done ) { . . . currentTime.Increment ( ) ; if ( currentTime.Equal ( endTime ) ) done = true ; } ; } 14

class type Declaration The class declaration creates a data type and names the members of the class. It does not allocate memory for any variables of that type! Client code still needs to declare class variables.

C++ Data Type class represents an ADT 2 kinds of class members: data members and function members Class members are private by default Data members are generally private Function members are generally declared public Private class members can be accessed only by the class member functions (and friend functions), not by client code.

Aggregate class Operations Built-in operations valid on class objects are: Member selection using dot ( . ) operator , Assignment to another class variable using ( = ), Pass to a function as argument (by value or by reference), Return as value of a function Other operations can be defined as class member functions

2 separate files Generally Used for class Type // Specification File ( timetype .h ) // Specifies the data and function members. class TimeType { public: . . . private: } ; // Implementation File ( timetype.cpp ) // Implements the TimeType member functions.

Implementation File for TimeType // Implementation File ( timetype.cpp ) // Implements the TimeType member functions. #include “ timetype.h” // also must appear in client code #include <iostream> . . . bool TimeType :: Equal (TimeType otherTime ) const // Function value == true, if this time equals otherTime // == false , otherwise { return ( (hrs == otherTime.hrs) && (mins == otherTime.mins) && (secs == otherTime.secs) ) ; }

Scope Resolution Operator ( :: ) C++ programs typically use several class types Different classes can have member functions with the same identifier, like Write( ) Member selection operator is used to determine the class whose member function Write( ) is invoked currentTime .Write( ) ; // class TimeType numberZ .Write( ) ; // class ComplexNumberType In the implementation file, the scope resolution operator is used in the heading before the function member’s name to specify its class void TimeType :: Write ( ) const { . . . }

Class Constructors A class constructor is a member function whose purpose is to initialize the private data members of a class object The name of a constructor is always the name of the class, and there is no return type for the constructor A class may have several constructors with different parameter lists. A constructor with no parameters is the default constructor A constructor is implicitly invoked when a class object is declared--if there are parameters, their values are listed in parentheses in the declaration

Specification of TimeType Class Constructors class TimeType // timetype.h { public : // 7 function members void Set ( int hours , int minutes , int seconds ) ; void Increment ( ) ; void Write ( ) const ; bool Equal ( TimeType otherTime ) const ; bool LessThan ( TimeType otherTime ) const ; TimeType ( int initHrs , int initMins , int initSecs ) ; // constructor TimeType ( ) ; // default constructor private : // 3 data members int hrs ; int mins ; int secs ; } ; 22

Implementation of TimeType Default Constructor TimeType :: TimeType ( ) // Default Constructor // Postcondition: // hrs == 0 && mins == 0 && secs == 0 { hrs = 0 ; mins = 0 ; secs = 0 ; }

Implementation of Another TimeType Class Constructor TimeType :: TimeType ( int initHrs, int initMins, int initSecs ) // Constructor // Precondition: 0 <= initHrs <= 23 && 0 <= initMins <= 59 // 0 <= initSecs <= 59 // Postcondition: // hrs == initHrs && mins == initMins && secs == initSecs { hrs = initHrs ; mins = initMins ; secs = initSecs ; }

For the Final Exam Note: You will be asked to write a simple class, without using dynamic memory. (no Copy Constructor or Destructor writing will be required.) You should know what a copy constructor and a destructor is in concept.

Pointers A pointer holds the memory address of another object. Through the pointer we can indirectly manipulate the referenced object. Pointers are useful for Creating linked data structures such as linked lists, management of dynamically allocated objects, and as a function parameter type for passing large objects such as arrays.

Pass-by-value sends a copy of the contents of the actual parameter CALLING BLOCK FUNCTION CALLED SO, the actual parameter cannot be changed by the function. 27

Pass-by-reference sends the location (memory address) of the actual parameter CALLING BLOCK FUNCTION CALLED can change value of actual parameter 28

Obtaining Memory Addresses the address of a non-array variable can be obtained by using the address-of operator & int x; float number; char ch; cout << “Address of x is “ << &x << endl; cout << “Address of number is “ << &number << endl; cout << “Address of ch is “ << &ch << endl;

What is a pointer variable? A pointer variable is a variable whose value is the address of a location in memory. to declare a pointer variable, you must specify the type of value that the pointer will point to, for example, int* ptr; // ptr will hold the address of an int char* q; // q will hold the address of a char

Using a Pointer Variable 2000 12 x 3000 ptr int x; x = 12; int* ptr; ptr = &x; NOTE: Because ptr holds the address of x, we say that ptr “points to” x

Unary operator * is the indirection (deference) operator 2000 12 x 3000 ptr int x; x = 12; int* ptr; ptr = &x; cout << *ptr; NOTE: The value pointed to by ptr is denoted by *ptr

Using the Dereference Operator int x; x = 12; int* ptr; ptr = &x; *ptr = 5; // changes the value // at address ptr to 5 2000 12 5 x 3000 ptr

Another Example char ch; ch = ‘A’; char* q; q = &ch; *q = ‘Z’; char* p; p = q; // now p and q both point to ch 4000 A Z ch 5000 6000 4000 4000 q p

Operator new Syntax new DataType new DataType [IntExpression] If memory is available, in an area called the heap (or free store) new allocates the requested object or array, and returns a pointer to (address of ) the memory allocated. Otherwise, program terminates with error message. The dynamically allocated object exists until the delete operator destroys it. 35

3 Kinds of Program Data STATIC DATA: memory allocation exists throughout execution of program AUTOMATIC DATA: automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function DYNAMIC DATA: explicitly allocated and deallocated during program execution by C++ instructions written by programmer using operators new and delete

Dynamically Allocated Data char* ptr; ptr = new char; *ptr = ‘B’; cout << *ptr; NOTE: Dynamic data has no variable name 2000 ptr ‘B’

Dynamically Allocated Data char* ptr; ptr = new char; *ptr = ‘B’; cout << *ptr; delete ptr; 2000 ptr NOTE: delete de-allocates the memory pointed to by ptr ?

Using Operator delete Operator delete returns to the free store memory which was previously allocated at run-time by operator new. The object or array currently pointed to by the pointer is deallocated, and the pointer is considered unassigned. 39

Operator delete Syntax delete Pointer delete [ ] Pointer If the value of the pointer is 0 there is no effect. Otherwise, the object or array currently pointed to by Pointer is deallocated, and the value of Pointer is undefined. The memory is returned to the free store. Square brackets are used with delete to deallocate a dynamically allocated array. 40

Dynamic Array Deallocation char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, “Bye” ); ptr[ 1 ] = ‘u’; delete ptr; // deallocates array pointed to by ptr // ptr itself is not deallocated // the value of ptr is undefined. ? ptr

What happens here? int* ptr = new int; *ptr = 3; ptr = new int; // changes value of ptr *ptr = 4; 3 ptr 3 ptr 4

Inaccessible Object An inaccessible object is an unnamed object that was created by operator new and which a programmer has left without a pointer to it. int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5; 8 ptr -5 ptr2

Making an Object Inaccessible 8 ptr -5 ptr2 int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5; ptr = ptr2; // here the 8 becomes inaccessible 8 ptr -5 ptr2

Memory Leak A memory leak is the loss of available memory space that occurs when dynamic data is allocated but never deallocated.

Leaving a Dangling Pointer A pointer that points to dynamic memory that has been de-allocated 8 ptr -5 ptr2 int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5; ptr = ptr2; delete ptr2; // ptr is left dangling ptr2 = NULL; 8 ptr NULL ptr2

Why is a destructor needed? When a DynArray class variable goes out of scope, the memory space for data members size and pointer arr is deallocated. But the dynamic array that arr points to is not automatically deallocated. A class destructor is used to deallocate the dynamic memory pointed to by the data member.

class DynArray Destructor DynArray::~DynArray( ); // Destructor. // POST: Memory for dynamic array deallocated. { delete [ ] arr ; } 48

For the Final Exam Note: You will not have to write source code using pointers or dynamic memory. Questions related to these topics will be multiple choice or True/False

SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required