March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
March 25, 2014CS410 – Software Engineering Lecture #12: Advanced C++ I 1 Alumni Party The Alumni Party will take place on Tuesday, May 13 It would be great.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
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.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract 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.
March 4, 2014CS410 – Software Engineering Lecture #9: C++ Basics III 1 Today’s Topics The operators new and deleteThe operators new and delete The scope.
Pointer Data Type and Pointer Variables
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
C++ Review (3) Structs, Classes, Data Abstraction.
Pointers OVERVIEW.
Object-Oriented Programming in C++
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
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.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
1 CSC241: Object Oriented Programming Lecture No 08.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CS410 – Software Engineering Lecture #12: C++ Basics V
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Dynamic Arrays
Popping Items Off a Stack Using a Function Lesson xx
References as Function Arguments
CS410 – Software Engineering Lecture #11: C++ Basics IV
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 16-2 Linked Structures
Popping Items Off a Stack Lesson xx
Java Programming Language
CS410 – Software Engineering Lecture #5: C++ Basics III
Overloading the << Operator
CS410 – Software Engineering Lecture #6: Advanced C++ I
Presentation transcript:

March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have used the member 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.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.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.

March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 2 Structure Pointer Operator Example: int main() { MyClass a, *b; a.SetValue(9);b->SetValue(5);(*b).SetValue(7);} Member access is correct in all three cases.

March 6, 2014CS410 – 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:two examples for classes with constructors and destructors: a singly linked lista singly linked list a two-dimensional arraya two-dimensional array overloaded operators and user-defined conversionsoverloaded operators and user-defined conversions

March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 4 A Singly Linked List We are going to develop a singly linked list datatype.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.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.Self-referential structures have pointer members that refer to objects of their own type. Such structures are the basis of many useful container classes.Such structures are the basis of many useful container classes.

March 6, 2014CS410 – 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 Prepend: adds an element to the front of the list First: returns the first element in the list First: returns the first element in the list Print: prints the list contents Print: prints the list contents Del: deletes the first element in the list Del: deletes the first element in the list Release: destroys the list Release: destroys the list

March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 6 A Singly Linked List struct 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 };

March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 7 A Singly Linked List void SList::Prepend(char c) { SListElem *temp = new SListElem; // create new element assert( temp != 0); temp->next = h;// link to SList temp->data = c; h = temp;// update head of SList }

March 6, 2014CS410 – 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 element delete temp;// delete 1st element }

March 6, 2014CS410 – 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 data "; temp = temp->next;// temp proceeds to next element } cout << "NULL\n"; }

March 6, 2014CS410 – 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. }

March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 11 A Singly Linked List int 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

March 6, 2014CS410 – 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.

March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 13 A Two-Dimensional Array class 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 element void Print() const; private: long **p;// pointer to a pointer to a long integer int dx, dy; };

March 6, 2014CS410 – 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 integers assert(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; }

March 6, 2014CS410 – 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 = 0 && y < dy); return p[x][y]; }

March 6, 2014CS410 – 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; }

March 6, 2014CS410 – 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(); }

March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 18 A Two-Dimensional Array Output:

March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 19 ADT Conversions Previously 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

March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 20 ADT Conversions However, 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?

March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 21 ADT Conversions The 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.

March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 22 ADT Conversions For 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.