Const declares immutable value void Test(char* t) { t++;// OK, address is not const t[0] = 'a'; // OK, char is not const } void Test2(const char* t) {

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Java Syntax Primitive data types Operators Control statements.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
1)Never start coding unless you understand the task! 2)Gather requirements first. This means identify the problem and ask questions about it. Now you kind.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
C++ Review (3) Structs, Classes, Data Abstraction.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
More C++ Features True object initialisation
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Chapter 4 Introduction to Classes, Objects, Methods and strings
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
1 Lecture 3 More about C++. 2 Topic for today More about classMore about class –Init list –Inline functions –Const –Default function parameters –Static.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Section 3 - Arrays and Methods. Arrays Array: collection of group of data variables of same type, sharing the same name for convenience - Easy to search.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
CSC Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Chapter 9 A Second Look at Classes and Objects - 3 Aggregation.
Pointers and Classes.
Pointers and Dynamic Arrays
Examples of Classes & Objects
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.
Methods Attributes Method Modifiers ‘static’
14.4 Copy Constructors.
Student Book An Introduction
C Basics.
Object-Oriented Programming Using C++
LinkedList Class.
Pass by Reference, const, readonly, struct
CS212: Object Oriented Analysis and Design
Classes & Objects: Examples
Object-Oriented Programming Using C++
Java Programming Language
Java Basics Data Types in Java.
Chapter 9: Pointers and String
String Class.
Destructors, Copy Constructors & Copy Assignment Operators
What is a String? String s = "compsci"; s c o m p s i
Destructors, Copy Constructors & Copy Assignment Operators
SPL – PS3 C++ Classes.
Chapter 5 Classes.
Presentation transcript:

const declares immutable value void Test(char* t) { t++;// OK, address is not const t[0] = 'a'; // OK, char is not const } void Test2(const char* t) { t++;// OK, address is not const t[0] = 'a'; // NO, char is const } const Value

typedef char* PCHAR; void Test3(const PCHAR t) { t++;// NO, address is const t[0] = 'a'; // OK, char is not const } typedef const char* PCONSTCHAR; void Test4(PCONSTCHAR t) { t++;// OK, address is not const t[0] = 'a'; // NO, char is const } void Test5(const PCONSTCHAR t) { t++;// NO, address is const t[0] = 'a'; // NO, char is const } const Address

char myChar; char& myChar2 = myChar;// myChar2 points to myChar myChar = ‘a’;// Now myChar2 = ‘a’ myChar2 = ‘b’;// Now myChar = ‘b’ Reference is similar to pointer because it holds address internally Reference is different from pointer because you cannot manipulate (or see) the address Reference is an always-dereferenced pointer Reference cannot be NULL or invalid void Change (char& myChar) { myChar = ‘b’; } void main() { char myChar = ‘a’; Change(myChar);// Now myChar = ‘b’ } & Reference

References provide efficient means for quickly passing data to functions (i.e. the data is passed by address aka by reference). Const make the data you pass read-only to safeguard against accidental modifications. void Change (const char& myChar) { myChar = ‘b’;// No, char is const char tempChar = myChar; // OK, myChar is read-only } void main() { char myChar = ‘a’; Change(myChar);// Now myChar = ‘b’ } const Reference

class String { public: String(const String& aString);// Copy constructor void MakeUpper();// Modifies string to make it upper case int GetLength() const;// Retrieves string length without modifying it }; void Test(String myString)// We pass myString by value hence a copy constructor will be called {// and a new String instance will be constructed on stack myString.MakeUpper(); } void Test2(String& myString)// Copy constructor will be NOT called {// because myString is passed by reference myString.MakeUpper(); } void Test3(const String& myString)// Copy constructor will be NOT called {// because myString is passed by reference myString.MakeUpper();// NO, MakeUpper() modifies string but we pass it by const reference int length = myString.GetLength();// OK, GetLength() does not modify String (declared as const) } void main() { String myString(“some text”); Test(myString);// myString = “some text” because we passed it by value Test2(myString); // myString = “SOME TEXT” because we passed it by reference } const Reference (contd.)

class String { public: int GetLength() const// Instance method { int i; for ( i = 0; Buffer[i] != ‘\0’; i++);// OK, can refer to instance prop. Buffer return i; } static GetLength(const char* buffer) // Static method { int i; for ( i = 0; Buffer[i] != ‘\0’ && I < MaxLength; i++ );// NO, can not refer to instance property return i;// OK, can refer to static MaxLength property } private: const char* Buffer;// Instance property static int MaxLength;// Static property }; void main() { String::MaxLength = 10;// set static property String::GetLength(“some text”);// static method invocation String MyString1, MyString2;// Have the same MaxLength value } Static Members: Shared Among All Class Instances

class String { public: String();// Default constructor String(const char* buffer);// Init constructor public: // Does not encapsulate immutable char pointer const char* Buffer; }; void main() { String myString(“test”); myString.Buffer = “test2”;// We can change Buffer pointer } String Class: Quick char* Encapsulation

class String { public: String();// Default constructor String(const char* buffer);// Init constructor const char* GetBuffer() const;// Returns encapsulated buffer char operator [](int index) const;// Index operator to retrieve character String& operator = (const char* buffer);// Assignment operator to initialize private: // Encapsulated immutable char pointer const char* Buffer; }; void main() { String myString(“test”); myString.Buffer = “test2”;// NO, Buffer is private const char* p = myString.GetBuffer();// OK, get encapsulated buffer char c = myString[0];// Get 0-th character } String Class: Robust char* Encapsulation