[CS 2337] Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Address of Operator and Classes

•& operator can create aliases to an object •Example: int x; int &y = x; //x and y refer to the same memory location //y is like a constant pointer variable y = 25; //sets the value of y (and of x) to 25 x = 2 * x + 30; //updates value of x and y •Address of operator can also be used to return the address of a private member variable of a class •However, if you are not careful, this operation can result in serious errors in the program

Pointers and Function Return Values

•A function can return a value of type pointer: int* testExp(...) { . . . }

Address of operator (&)

A unary operator that returns the address of its operand int x; int *p; p = &x; //Assigns the address of x to p

pointer data type

No name is associated with the ___ in C++

Dereferencing Operator (*)

When used as a unary operator, * refers to object to which its operand points cout << *p << endl; •Prints the value stored in the memory location pointed to by p

delete

___ operator: used to destroy dynamic variables

pointer variable

a variable whose content is a memory address.

Dynamic array

array created during program execution. int *p; p = new int[10]; *p = 25; //stores 25 in the first memory location p++; //to point to next array component *p = 35; // stores 35 into the second memory location

Memory leak

previously allocated memory that cannot be reallocated. To avoid a memory leak, when a dynamic variable is no longer needed, destroy it to deallocate its memory.

Compile-time binding

the necessary code to call specific function is generated by compiler (static binding, early binding)

Operations on Pointer Variables

•Assignment: value of one pointer variable can be assigned to another pointer of same type •Relational operations: two pointer variables of same type can be compared for equality, etc. •Some limited arithmetic operations •Integer values can be added and subtracted from a pointer variable •Value of one pointer variable can be subtracted from another pointer variable •Pointer arithmetic can be very dangerous: •Program can accidentally access memory locations of other variables and change their content without warning -Some systems might terminate the program with an appropriate error message •Always exercise extra care when doing pointer arithmetic

Classes and Virtual Destructors

•Classes with pointer member variables should have the destructor •Destructor should deallocate storage for dynamic objects •If a derived class object is passed to a formal parameter of the base class type, destructor of the base class executes •Regardless of whether object is passed by reference or by value •Solution: use a virtual destructor (base class) •Virtual destructor of a base class automatically makes the destructor of a derived class virtual •After executing the destructor of the derived class, the destructor of the base class executes •If a base class contains virtual functions, make the destructor of the base class virtual

Run-time binding

•Compiler does not generate code to call a specific function: it generates information to enable run-time system to generate specific code for the function call •Also known as late binding or dynamic binding •Note: cannot pass an object of base class type to a formal parameter of the derived class type

Array-Based Lists

•Lists can be unordered or ordered •Either type can be derived from abstract class arrayListType •Ordered list includes function insert to insert an item in its proper place

Abstract Classes and Pure Virtual Functions

•New classes can be derived through inheritance without designing them from scratch •Derived classes: •Inherit existing members of base class •Can add their own members •Can redefine or override public and protected member functions •Base class can contain functions that you would want each derived class to implement •However, base class may contain functions that may not have meaningful definitions in the base class •Pure virtual functions do not have definitions (bodies have no code) •Example: virtual void draw() = 0; •An abstract class is a class with one or more virtual functions •It can contain instance variables, constructors, and functions that are not pure virtual •It must provide the definitions of the constructor and functions that are not pure virtual

Functions and Pointers

•Pointer variable can be passed as a parameter either by value or by reference •As a reference parameter in a function heading, use &: void pointerParameters(int* &p, double *q) { . . . }

Quick Review

•Pointer variables contain the addresses of other variables as their values •Declare a pointer variable with an asterisk, *, between the data type and the variable •Address of operator (&) returns the address of its operand •Unary operator * is the dereferencing operator •Member access operator (->) accesses the object component pointed to by a pointer •Dynamic variable: created during execution •Created using new •Deallocated using delete •Shallow copy: two or more pointers of the same type point to the same memory •Deep copy: two or more pointers of the same type have their own copies of the data •Binding of virtual functions occurs at execution time (dynamic or run-time binding) •A list is a collection of elements of the same type. Can be ordered or unordered •Common set of list operations

member access operator arrow

•Syntax to access a class (struct) member using the operator -> •Thus, (*studentPtr).gpa = 3.9; is equivalent to: studentPtr->gpa = 3.9;

Declaring Pointer Variables

•The statements below each declare a pointer: •int *p; •char *ch; •These statements are equivalent: •int *p; •int* p; •int * p; •In the statement: int* p, q; •Only p is a pointer variable •q is an int variable •To avoid confusion, attach the character * to the variable name: int *p, q; int *p, *q;

Slicing problem

•Values of a derived class object can be copied into a base class object if derived class has more data members than base class, some data could be lost. •Solution: use pointers for both base and derived class objects

Classes, structs, and Pointer Variables

•You can declare pointers to other data types, such as a struct: struct studentType { char name[26]; double gpa; int sID; char grade; }; studentType student; studentType* studentPtr; • •student is an object of type studentType •studentPtr is a pointer variable of type studentType •To store address of student in studentPtr: studentPtr = &student; •To store 3.9 in component gpa of student: (*studentPtr).gpa = 3.9; •( ) used because dot operator has higher precedence than dereferencing operator •Alternative: use member access operator arrow (->)

Virtual function

•binding occurs at program execution time, not at compile time •Declared with reserved word virtual

Copy Constructor

•provided by the compiler •Performs this initialization •Leads to a shallow copying of the data if class has pointer member variables •automatically executes in three situations: •When an object is declared and initialized by using the value of another object •When an object is passed by value as a parameter When the return value of a function is an object. solution: override the copy constructor (see syntax). •For classes with pointer member variables, three things are normally done: •Include the destructor in the class •Overload the assignment operator for the class •Include the copy constructor

Deep copy

•when the contents of the memory pointed to by a pointer are copied to the memory location of another pointer •Two copies of the data

Shallow copy

•when two or more pointers of the same types point to the same memory •They point to the same data •Danger: deleting one deletes the data pointed to by all of them


Set pelajaran terkait

Personal Finance Planning Quiz 1 Chapters 1-2

View Set

Anatomy II: Ch. 16 -Special Senses Activity

View Set

Data Communications and Network Services

View Set