Structure Pointer Operator

22
March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have used the member operator ‘.’. Now we introduce the structure pointer operator ‘->’, which provides access to the members of a structure via a pointer. If a pointer variable is assigned the address of a structure or class object, a member of the object can be accessed by the ‘->’ operator.

description

Structure Pointer Operator. For accessing members in structures and classes we have used the member operator ‘.’. Now we introduce the structure pointer operator ‘->’, which provides access to the members of a structure via a pointer. - PowerPoint PPT Presentation

Transcript of Structure Pointer Operator

Page 1: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

1

Structure Pointer Operator• For accessing members in structures and classes we

have used the member operator ‘.’.• Now we introduce the structure pointer operator ‘->’,

which provides access to the members of a structure via a pointer.

• If a pointer variable is assigned the address of a structure or class object, a member of the object can be accessed by the ‘->’ operator.

Page 2: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

2

Structure Pointer OperatorExample:

int main(){

MyClass a, *b;

a.SetValue(9);b->SetValue(5);(*b).SetValue(7);

}

Member access is correct in all three cases.

Page 3: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

3

Constructors and Destructors

Now we are going to talk about

• two examples for classes with constructors and destructors:

•a singly linked list•a two-dimensional array

• overloaded operators and user-defined conversions

Page 4: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

4

A Singly Linked List

• We are going to develop a singly linked list datatype.

• This is the prototype of many useful dynamic abstract data types (ADTs) called self-referential structures.

• Self-referential structures have pointer members that refer to objects of their own type.

• Such structures are the basis of many useful container classes.

Page 5: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

5

A Singly Linked List

We want to be able to perform the following list operations:

• Prepend: adds an element to the front of the list• First: returns the first element in the list• Print: prints the list contents• Del: deletes the first element in the list• Release: destroys the list

Page 6: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

6

A Singly Linked Liststruct SListElem{

char data;SListElem *next;

};

class SList{public:

SList(): h(0) {} // 0 denotes empty list~SList() { Release(); }void Prepend(char c);void Del();SListElem *First() const { return h; }void Print() const;void Release();

private:SListElem *h; // head of SList

};

Page 7: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

7

A Singly Linked List

void SList::Prepend(char c){

SListElem *temp = new SListElem; // create new elementassert( temp != 0);temp->next = h; // link to SListtemp->data = c;h = temp; // update head of SList

}

Page 8: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

8

A Singly Linked List

void SList::Del(){

SListElem *temp = h;if (temp != 0){

h = h->next; // let head point to 2nd elementdelete temp; // delete 1st element

}}

Page 9: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

9

A Singly Linked List

void SList::Print() const{

SListElem *temp = h;while (temp != 0) // detect end of Slist{

cout << temp->data << " -> ";temp = temp->next; // temp proceeds to next element

}cout << "NULL\n";

}

Page 10: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

10

A Singly Linked List

void SList::Release(){

while (h != 0) // as long as there are elements...Del(); // ... delete them.

}

Page 11: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

11

A Singly Linked Listint main(){

SList MyList;MyList.Prepend('S');MyList.Prepend('T');MyList.Prepend('A');MyList.Prepend('R');MyList.Prepend('T');MyList.Del();MyList.Print();

}

Output: R -> A -> T -> S -> NULL

Page 12: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

12

A Two-Dimensional Array

• We are now going to implement a dynamic and safe two-dimensional array.

• We will use a pointer-to-pointer-to-base type structure for this implementation.

Page 13: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

13

A Two-Dimensional Arrayclass Matrix{public:

Matrix(int sizeX, int sizeY);~Matrix();int GetSizeX() const { return dx; }int GetSizeY() const { return dy; }long &Element(int x, int y); // return reference to an elementvoid Print() const;

private:long **p; // pointer to a pointer to a long integerint dx, dy;

};

Page 14: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

14

A Two-Dimensional Array

Matrix::Matrix(int sizeX, int sizeY) : dx(sizeX), dy(sizeY){

assert(sizeX > 0 && sizeY > 0);p = new long*[dx]; // create array of pointers to long integersassert(p != 0);for (int i = 0; i < dx; i++){

p[i] = new long[dy]; // for each pointer, create array of long integers

assert(p[i] != 0);for (int j = 0; j < dy; j++)

p[i][j] = 0;}

}

Page 15: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

15

A Two-Dimensional Array

Matrix::~Matrix(){

for (int i = 0; i < dx; i++)delete [] p[i]; // delete arrays of long integers

delete [] p; // delete array of pointers to long integers}

long &Matrix::Element(int x, int y){

assert(x >= 0 && x < dx && y >= 0 && y < dy);return p[x][y];

}

Page 16: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

16

A Two-Dimensional Array

void Matrix::Print() const{

cout << endl;for (int y = 0; y < dy; y++){

for (int x = 0; x < dx; x++)cout << p[x][y] << "\t";

cout << endl;}

}

Page 17: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

17

A Two-Dimensional Array

int main(){

Matrix MyMatrix(3, 5);

MyMatrix.Element(1, 1) = 88;MyMatrix.Element(2, 2) = 11;MyMatrix.Element(2, 4) = MyMatrix.Element(1, 1) +

MyMatrix.Element(2, 2);

cout << MyMatrix.Element(2, 4) << endl;MyMatrix.Print();

}

Page 18: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

18

A Two-Dimensional Array

Output:

99

0 0 00 88 00 0 110 0 00 0 99

Page 19: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

19

ADT ConversionsPreviously we talked about constructors as conversions.For example, in our class ModInt we provided the following constructor:

ModInt(int i = 0): v(i % modulus) {}

It is used automatically to convert values of type int into values of type ModInt:

int i = 39;ModInt m;m = i; // legal operation

Page 20: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

20

ADT ConversionsHowever, this constructor does not tell the compiler how to convert a ModInt value into an int value:

int i = 39;ModInt m;i = m; // illegal operation

It is not possible for the user to add a constructor to a built-in type such as int or double.How can we solve this problem?

Page 21: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

21

ADT ConversionsThe solution is to provide a special conversion function inside the user-defined class.The general form of such a member function is:

operator type() { … }

Such a member function must • be nonstatic,• have no parameters,• have no declared return type,• return an expression of the designated type.

Page 22: Structure Pointer Operator

March 6, 2014 CS410 – Software Engineering Lecture #10: C++ Basics IV

22

ADT ConversionsFor example, we could add such a special conversion function to the ModInt class:

ModInt::operator int(){

return v;}

Then the following code is correct:int i = 39;ModInt m;i = m;Here, I receives the value 0.