Learning Objectives Pointers as dada members

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
What is a copy constructor? A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance during initialization.
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];
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
C++ Review (3) Structs, Classes, Data Abstraction.
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+
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
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.
Chapter 8 Operator Overloading, Friends, and References.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Slide 1 Chapter 10 Pointers and Dynamic Arrays. Slide 2 Learning Objectives  Pointers  Pointer variables  Memory management  Dynamic Arrays  Creating.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Pointers and Dynamic Arrays
More C++ Features True object initialisation
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Chapter 10 Pointers and Dynamic Arrays. Learning Objectives Pointers – Pointer variables – Memory management Dynamic Arrays – Creating and using – Pointer.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Learners Support Publications Constructors and Destructors.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
Copy Constructor / Destructors Stacks and Queues
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Programming with ANSI C ++
CISC181 Introduction to Computer Science Dr
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
The dirty secrets of objects
group work #hifiTeam
Dynamically Allocated Memory
Operator Overloading CSCE 121 J. Michael Moore
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Constructors and destructors
Constructors and Destructors
Copy Assignment CSCE 121 J. Michael Moore.
Operator Overloading, Friends, and References
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
Class: Special Topics 2 For classes using memory allocation
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Copy Assignment CSCE 121.
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Presentation transcript:

Learning Objectives Pointers as dada members Friend functions, friend classes Pointers and classes, the this pointer Operator overloading The BIG THREE Destructors Assignment operator Copy constructor

Topic 1: Pointers as Data Members In constructor, use new keyword to allocate space for the member To access pointer member, we need to dereference the pointer

Pointer as a Data Member Example Class G { private: int x; Int * y; public: G(int, int); void Reset_Y(int); } G::G(int xx, int yy) { x = xx; y = new int; *y = yy; } void G::Reset_Y(int w) *y = w;

Topic 2: Friend Functions / Classes Friend function of a class Not a member function Has direct access to private members Just as member functions do Use keyword friend in front of function declaration friend return_type function_name(parameters) Specified in class definition (public field) But they’re NOT member functions!

Friend Function Example Class G { private: int x; public: G(int); Int get_x(); friend void F(int , int); } This means: function F is a friend of class G and F can directly access the private member of G

Friend Classes Entire classes can be friends Syntax: friend class F Example: class F is friend of class C All class F member functions are friends of C NOT reciprocated Friendship granted, not taken Syntax: friend class F Goes inside class definition of "authorizing" class

Friend Class Example Class G { private: int x; public: get_x(); friend class F; } This means: class F is a friend of class G and functions in F can directly access the private member of G

Topic 3: Pointer and Classes The -> operator Shorthand notation Combines dereference operator, *, and dot operator Specifies member of class "pointed to" by given pointer Example: MyClass *p; p = new MyClass; p->grade = "A"; Equivalent to: (*p).grade = "A";

The this Pointer Member function definitions might need to refer to calling object Use predefined this pointer Note: this is not the name of the calling object, but is the name of a pointer that points to the calling object. Why do we need this pointer To overcome the ambiguities of member variable name and parameter name

Topic 4: Operator Overloading Recall: Function overlaoding Operators +, -, %, ==, etc. Really just functions! Simply "called" with different syntax: x + 7 Think of it as: +(x, 7) "+" is the function name x, 7 are the arguments Function "+" returns "sum" of it’s arguments

Overloading Basics Overloading operator Operator itself is "name" of function NOT a member function Example: + operator overloading declaration: const Money operator +(const Money& a1, const Money& a2); Function name: operator + Uses reference parameters for efficiency Use const for parameters to avoid changes of objects Both qualifiers are optional Returned value is type Money

Overloading Basics Example: + operator overloading declaration: const Money operator +(const Money& a1, const Money& a2); Use const for return value to avoid code like this: MyClass a, b, c; ... (a + b) = c; // What does this mean? This statement would basically do nothing, but if the + operator returns a non-const value, it will compile! So, we want to return a const instance, so that such code will not even be allowed to compile.

Overloaded "==" Equality operator, == Enables comparison of Money objects Declaration: bool operator ==(const Money& a1, const Money& a2); Function name: operator == Returns bool type for true/false equality Uses reference parameters for efficiency Use const for parameters to avoid changes of objects Again, it’s a non-member function

Topic 5: The BIG THREE Dynamically-allocated variables Do not go away until "deleted" If member data are pointers They dynamically allocate memory (using new keyword) in constructors Must have means to "de-allocate" when object is destroyed Answer: destructor!

Destructors Opposite of constructor Automatically called when object is out-of-scope Default version only removes ordinary variables, not dynamic variables Defined like constructor, just add ~ MyClass::~MyClass() { //Perform clean-up duties for dynamic variables }

Destructors The destructor must have the same name as the class, but preceded with a tilde sign (~) and it must also return no value The use of destructors is especially suitable when an object assigns dynamic memory during its lifetime and at the moment of being destroyed we want to release the memory that the object was allocated

Assignment operator (=) Shallow copy Only copy value to value; Does not copy dynamically allocated field Works fine for classes that have no dynamic variables Has problems with classes that have dynamic variables

Operator Overloading (=) Deep Copy (solution to the problems of shallow copy) Assignment operator overloading Using copy constructor

Operator (=) Deep Copy (Assignment Operator Overloading) class_name & operator=(const class_name &) Pass by reference is for efficiency const for parameters to avoid changes of object The reason operator= returns a reference is so that you can concatenate multiple assignments in one statement MyClass a, b, c; a = b = c; It is a member function Why return value is not const? To allow code like this: (a = b) = c;

Operator (=) Deep Copy (copy constructor overloading) 2. Using copy constructor class_name(class_name&) It is a member function

Shallow copy and Deep copy Shallow copy is called when assignment operator and copy constructor are not overloaded Deep copy is called when assignment operator and copy constructor are overloaded Assignment operator is called when = operator is used between two initialized objects Copy constructor is called when one declared object is initialized by another object A function returns a value of the class type A function has call-by-value class type parameters

The BIG THREE Destructor, assignment operator overloading, copy constructor should be used together When to use The data members of a class are pointers