References, const and classes

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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];
Const Parameters & In Line Functions 04/15/11. Next Time  Quiz, Monday, 04/18/11  Over 5.2 and 5.3 void functions pass-by-reference  Read 7.1 about.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
C++ data types. Structs vs. Classes C++ Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
P Improvements - Constructor p Parameter passing Constructors Problem Solving With C++
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
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 Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Object Oriented Programming (OOP) Lecture No. 11.
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.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Variables and memory addresses
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.
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.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Test 2 Review Outline.
Chapter 8 Arrays, Strings and Pointers
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.
C++ Programming Functions
Hassan Khosravi / Geoffrey Tien
Pointers in C.
Pointers.
C++ Programming Functions
FIGURE 9-5 Integer Constants and Variables
Pointers Revisited What is variable address, name, value?
INC 161 , CPE 100 Computer Programming
Chapter 9 Pointers Objectives
Lecture 6 C++ Programming
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Pass by Reference, const, readonly, struct
7 Arrays.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Constant pointers and pointers to constants
Pointers.
Pointers Lecture 2 Tue, Jan 24, 2006.
7 Arrays.
Dr Tripty Singh Arrays.
C++ Constructor Insanity CSE 333 Summer 2018
Suggested self-checks: Section 7.11 #1-11
Class.
Data Structures and Algorithms Introduction to Pointers
Java Programming Language
C++ Constructor Insanity CSE 333 Autumn 2018
C++ Constructor Insanity CSE 333 Winter 2019
Functions Reasons Concepts Passing arguments to a function
Programming Arrays.
Introduction to Pointers
Presentation transcript:

References, const and classes CSE 333 – Section 4 References, const and classes

HW2 Index the contents of files Search through documents containing specified words Feels good when you complete it

This or that? Consider the following code: Pointers: References: int i; int i; int *pi = &i; int &ri = i; In both cases, The difference lies in how they are used 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.

C++ const declaration As a declaration specifier, const is a type specifier that makes objects unmodifiable. const int m = 255; Reference to constant integer: int n = 100; const int &ri = n; //ri becomes read only

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++ 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