CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
SPL/2010 Pointers and Parameter Passing in C++ 1.
CSE 332: C++ copy control I Copy Control (Part I) Copy control consists of 5 distinct operations –A copy constructor initializes an object by duplicating.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
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];
Classes: A Deeper Look Systems Programming.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
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.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
OOP Languages: Java vs C++
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
More C++ Features True object initialisation
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
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:
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Introduction to Object Oriented Programming Chapter 10.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
ECE 264 Object-Oriented Software Development
Learners Support Publications Constructors and Destructors.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Pointers and Dynamic Arrays
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.
Programming with ANSI C ++
Constructor & Destructor
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Object Oriented Analysis and Design
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
CS212: Object Oriented Analysis and Design
Contents Introduction to Constructor Characteristics of Constructor
Constructors and destructors
Constructors and Destructors
References, const and classes
C++ Constructor Insanity CSE 333 Summer 2018
Java Programming Language
C++ Constructor Insanity CSE 333 Autumn 2018
C++ Constructor Insanity CSE 333 Winter 2019
CSE 333 – Section 5 C++ (1m).
SPL – PS3 C++ Classes.
Presentation transcript:

CSE 333 – SECTION 4

Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading

This or That? Consider the following code: Pointers:References:int i; int *pi = &i;int &ri = i; They are used differently in expressions: *pi = 4;ri = 4;

Pointers and References Once a reference is created, it cannot be later made to reference another object. Compare to pointers, which are often reassigned. References cannot be null, whereas pointers can. References can never be uninitialized. It is also impossible to reinitialize a reference.

When to Use? Function parameter types and return types and functions that declare overloaded operators. Pointers: may point to many different objects during its lifetime. Pointer arithmetic (++ or --) enables moving from one address to another. (Arrays, for e.g.) References: can refer to only one object during its lifetime. Style Guide Tip: use const reference parameters to pass input use pointers to pass output parameters input parameters first, then output parameters last

C++ const Declaration As a declaration specifier, const changes the type to make the data bits unmodifiable. const int m = 255; Pointer/Reference to constant integer: int n = 100; const int *pi = &n; //*pi is read only const int &ri = n; //ri is read only

C++ Classes /* Note: This code is unfinished! Beware! */ class Point { public: Point(const int x, const int y); // constructor int get_x() { return x_; } // inline member function int get_y() { return y_; } // inline member function double distance(const Point &p); // member function void setLocation(const int x, const int y); //member function private: int x_; // data member int y_; // data member }; // class Point

C++ Classes Encapsulation and Abstraction Access specifiers: Public: anything outside the class can access it Protected: only this class and derived classes can access it Private: only this class can access it Polymorphism Multiple Inheritance

Constructors and Destructors Function called when an object of a class is created Initializes the data members of a class Has the same name as the class Types – Default – also called the empty constructor Parameterized – Has arguments Copy – Pass another already constructed object of the same class Destructors are invoked implicitly when a class instance is deleted / goes out of scope

new and delete new is used to allocate objects and primitive data types on the heap delete is used to deallocate these heap allocated objects Use “delete [ ] array” on an array Unlike malloc() and free(), new and delete are operators

Initialization vs Assignment #define MAXSIZE 3 class IntArrayList { public: IntArrayList() : array_(new int[MAXSIZE]), len_(0), maxsize_(MAXSIZE) { } IntArrayList(const int *const arr, size_t len) : len_(len), maxsize_(len_*2) { array_ = new int[maxsize_]; memcopy(array_, arr, len * sizeof(int)); } IntArrayList(const IntArrayList &rhs) { len_ = rhs.len_; maxsize_ = rhs.maxsize_; array_ = new int[maxsize_]; memcopy(array_, rhs.array_, maxsize_ * sizeof(int)); }... private: int *array_; size_t len_; size_t maxsize_; };

Operator Overloading A form of polymorphism. Give special meanings to operators in user-defined classes Special member functions in classes with a particular naming convention For E.g., for overloading the ‘+’ operator, define a member function named operator+

Common Operators The most commonly overloaded operators are = (assignment operator) + - * (binary arithmetic operators) += -= *= (compound assignment operators) == != (comparison operators)