CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
Introduction to Programming Lecture 31. Operator Overloading.
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
C++ Operator Overloading Gordon College CPS212 Gordon College CPS212.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
CMSC 202 Lesson 14 Copy and Assignment. Warmup Write the constructor for the following class: class CellPhone { public: CellPhone(int number, const string&
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
11 Introduction to Object Oriented Programming (Continued) Cats.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
1 Working with Pointers An exercise in destroying your computer.
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
CS 11 C++ track: lecture 3 Today const and const -correctness operator overloading.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
W 5 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH ; p197 – 226 Week 4 lesson 2Operators 1H ; p237 – 254 Week 5 lesson.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
CS 11 C++ track: lecture 6 Today: Default function arguments friend classes Introduction to exception handling.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 Introduction to Object Oriented Programming Chapter 10.
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 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
CS32 Discussion Section 1B Week 2 TA: Hao Yu (Cody)
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
CS410 – Software Engineering Lecture #12: C++ Basics V
Yan Shi CS/SE 2630 Lecture Notes
Introduction to Programming
A First C++ Class – a Circle
CISC181 Introduction to Computer Science Dr
Chapter 4 Linked Lists.
Traversing a Linked List
CS410 – Software Engineering Lecture #11: C++ Basics IV
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Overloading the << operator
Dynamic Memory A whole heap of fun….
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Introduction to Programming
Jordi Cortadella and Jordi Petit Department of Computer Science
CS148 Introduction to Programming II
CS410 – Software Engineering Lecture #5: C++ Basics III
The Stack.
Linked List Functions.
Review of Function Overloading
CS31 Discussion 1H Winter19: week 9
Lists CMSC 202, Version 4/02.
Lists CMSC 202, Version 4/02.
Overloading the << operator
CS410 – Software Engineering Lecture #6: Advanced C++ I
Data Structures & Programming
Presentation transcript:

CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions

Member initialization lists (1) class Foo { private: int x, y; public: Foo(int nx, int ny) { x = nx; y = ny; } // other methods... };

Member initialization lists (2) class Bar { private: int w, z; Foo f; public: Bar(int nw, int nz) : w(nw), z(nz), f(nw, nz) {} // other methods... };

Member initialization lists (3) Initialization list not essential for primitive types......but needed to pass arguments to constructors of member objects

Member initialization lists (4) class Bar { private: int w, z; Foo *f; // now a pointer! public: Bar(int nw, int nz) { w = nw; z = nz; f = new Foo(nw, nz); } };

Member initialization lists (5) class Bar { private: int w, z; Foo f; public: Bar(int nw, int nz) { w = nw; z = nz; Foo f2(nw, nz); f = f2; // inefficient; copies f2 } // f2 deallocated here };

Linked lists (1) struct node { int data; node *next; node(int d, node *n) : data(d), next(n) { } };

Linked lists (2) Create a linked list: Node *prev = 0; // null pointer Node *head = 0; for (int i = 10; i > 0; i--) { head = new Node(i, prev); prev = head; }

Linked lists (3) Create a linked list: // Simpler: Node *head = 0; for (int i = 10; i > 0; i--) { head = new Node(i, head); }

Linked lists (4) Print a linked list: for (Node *n = head; n; n = n->next) { cout data << endl; } iterate until reach a null pointer (n == 0)

Linked lists (5) Free a linked list: // This WON’T work: for (Node *n = head; n; n = n->next) { delete n; } Why won’t it work?

Linked lists (6) Free a linked list: // This WILL work: Node *n = head; while (n != 0) { Node *next = n->next; delete n; n = next; }

friend functions (1) Want to be able to say: Matrix m(2, 3); cout << m << endl; but this really means: cout.operator<<(m); // etc. No such method exists in cout’s class ( ostream )! Can’t add methods to library classes

friend functions (2) Can define overloaded function: ostream& operator<<(ostream& os, const Matrix& m); return type is for chaining os is cout m is the Matrix to output

friend functions (3) Problem: not a member fn of Matrix so can’t access private Matrix data could rely on public accessor fns or could make it a friend of the Matrix class

friend functions (4) class Matrix { //... friend ostream& operator<<( ostream& os, const Matrix& m); //... };

friend functions (5) // Definition of friend function: ostream& operator<<( ostream& os, const Matrix& m) { // can use private Matrix data: os << ”Matrix(” << m.rows << ”, ” << m.cols << ”)”; return os; }

friend functions (6) Usage: Matrix m(2, 3); cout << m << endl; // Prints: ”Matrix(2, 3)”. // Could define to print elems e.g. // ”Matrix(2, 3):{{0,0,0},{0,0,0}}”

Next week Default function arguments friend classes Introduction to exception handling