CPSC 427a: Object-Oriented Programming

Similar documents
CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

ECE 122. Engineering Problem Solving with Java

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

Inheritance, and Polymorphism.

CPSC 427a: Object-Oriented Programming

Rushikesh K Joshi. Department of Computer Science and Engineering Indian Institute of Technology Bombay

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

C++ STREAMS; INHERITANCE AS

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

G52CPP C++ Programming Lecture 13

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

CSE 12, Week Six, Lecture Two Discussion: Getting started on hw7 & hw8

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

CPSC 427: Object-Oriented Programming

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

CPSC 427: Object-Oriented Programming

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CPSC 427: Object-Oriented Programming

C++ Important Questions with Answers

Object-Oriented Principles and Practice / C++

INHERITANCE PART 2. Constructors and Destructors under. Multiple Inheritance. Common Programming Errors. CSC 330 OO Software Design 1

CPSC 427a: Object-Oriented Programming

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Data Structures Unit 02

Fast Introduction to Object Oriented Programming and C++

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Array Elements as Function Parameters

Object-Oriented Programming, Iouliia Skliarova

Chapter 6 Introduction to Defining Classes

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

C++ Programming: Polymorphism

Study Guide to Exam 2

OOP. Unit:3.3 Inheritance

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Tokens, Expressions and Control Structures

ENERGY 211 / CME 211. Functions

Short Notes of CS201

Pointers and References

CE221 Programming in C++ Part 1 Introduction

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Functions and Parameter Passing

6.096 Introduction to C++ January (IAP) 2009

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Instantiation of Template class

CS201 - Introduction to Programming Glossary By

Object Oriented Design

CHAPTER 9 INHERITANCE. 9.1 Introduction

General Computer Science II Course: B International University Bremen Date: Dr. Jürgen Schönwälder Deadline:

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Final Exam. Name: Student ID: Section: Signature:

Object-Oriented Principles and Practice / C++

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

CS105 C++ Lecture 7. More on Classes, Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

EMBEDDED SYSTEMS PROGRAMMING OO Basics

C++ Inheritance and Encapsulation

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

Chapter 1: Object-Oriented Programming Using C++

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Dr. Md. Humayun Kabir CSE Department, BUET

CSCI 1370 APRIL 26, 2017

ECE260: Fundamentals of Computer Engineering

COMP322 - Introduction to C++

Homework 6. Reading. Problems. Handout 7 CS242: Autumn November

Increases Program Structure which results in greater reliability. Polymorphism

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

CS2141 Software Development using C/C++ C++ Basics

G52CPP C++ Programming Lecture 18

Overriding Variables: Shadowing

SFU CMPT Topic: Class Templates

G Programming Languages - Fall 2012

C++ For Science and Engineering Lecture 15

Multiple and Virtual Inheritance in C++

CompSci 125 Lecture 20. Inheritance: Introduction, Overrides UML: Introduction to Class Diagrams

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

C++ Programming Lecture 8 Software Engineering Group

Lecture 23: Pointer Arithmetic

IS 0020 Program Design and Software Tools

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Programming in C and C++

Inheritance and Polymorphism

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

More Tutorial on C++:

What is Inheritance?

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

void insert( Type const & ) void push_front( Type const & )

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Transcription:

CPSC 427a: Object-Oriented Programming Michael J. Fischer Lecture 16 November 1, 2012 CPSC 427a, Lecture 16 1/29

Unicode Characters and PS7 Name Visibility CPSC 427a, Lecture 16 2/29

Unicode Characters and PS7 CPSC 427a, Lecture 16 3/29

Unicode Unicode is a standard for identifying characters in any language through a unique number called its code point. Click here for further information. Unicode box characters can be used to draw a standard Sudoku grid without having to go to a full graphics drawing system. The box characters are on the U-2500 code page. We use the box characters in PS7 to draw a standard Sudoku grid. CPSC 427a, Lecture 16 4/29

Unicode character encodings One common way to encode Unicode characters as bytes is called UTF-8 (or utf-8, UTF8, utf8). Here, ASCII characters have their standard 1-byte encoding, but other characters are encoded by a 3-byte sequence consisting of the hex byte 0xE2 followed by the two byte code point. C++ recognizes the escape sequence \u as specifying that the next four hex digits are the code point for a Unicode character. Example: The string "\u2503" contains the 3-byte code for a thick vertical bar. It s type is const char*. Thus, a thick vertical bar could be printed on a UTF-8 aware device using cout << "\u2503"; CPSC 427a, Lecture 16 5/29

Name Visibility CPSC 427a, Lecture 16 6/29

Private derivation (default) class B : A {... }; specifies private derivation of B from A. A class member inherited from A become private in B. Like other private members, it is inaccessible outside of B. If public in A, it can be accessed from within A or B or via an instance of A, but not via an instance of B. If private in A, it can only be accessed from within A. It cannot even be accessed from within B. CPSC 427a, Lecture 16 7/29

Private derivation example Example: class A { private: int x; public: int y; }; class B : A {... f() {... x++;...} // privacy violation }; //-------- outside of class definitions -------- A a; B b; a.x // privacy violation a.y // ok b.x // privacy violation b.y // privacy violation CPSC 427a, Lecture 16 8/29

Public derivation class B : public A {... from A. }; specifies public derivation of B A class member inherited from A retains its privacy status from A. If public in A, it can be accessed from within B and also via instances of A or B. If private in A, it can only be accessed from within A. It cannot even be accessed from within B. CPSC 427a, Lecture 16 9/29

Public derivation example Example: class A { private: int x; public: int y; }; class B : public A {... f() {... x++;...} // privacy violation }; //-------- outside of class definitions -------- A a; B b; a.x // privacy violation a.y // ok b.x // privacy violation b.y // ok CPSC 427a, Lecture 16 10/29

The protected keyword protected is a privacy status between public and private. Protected class members are inaccessible from outside the class (like private) but accessible within a derived class (like public). Example: class A { protected: int z; }; class B : A {... f() {... z++;...} // ok }; CPSC 427a, Lecture 16 11/29

Protected derivation class B : protected A {... derivation of B from A. }; specifies protected A public or protected class member inherited from A becomes protected in B. If public in A, it can be accessed from within B and also via instances of A but not via instances of B. If protected in A, it can be accessed from within A or B but not from outside. If private in A, it can only be accessed from within A. It cannot be accessed from within B. CPSC 427a, Lecture 16 12/29

Surprising example 1 1 class A { 2 protected: 3 int x; 4 }; 5 class B : public A { 6 public: 7 int f() { return x; } // ok 8 int g(a* a) { return a->x; } // privacy violation 9 }; Result: tryme1.cpp: In member function int B::g(A*) : tryme1.cpp:3: error: int A::x is protected tryme1.cpp:9: error: within this context CPSC 427a, Lecture 16 13/29

Surprising example 2: contrast the following 1 class A { }; 2 class B : public A {}; // <-- public derivation 3 int main() { A* ap; B* bp; 4 ap = bp; } Result: OK. 1 class A { }; 2 class B : private A {}; // <-- private derivation 3 int main() { A* ap; B* bp; 4 ap = bp; } Result: tryme2.cpp: In function int main() : tryme2.cpp:4: error: A is an inaccessible base of B CPSC 427a, Lecture 16 14/29

Surprising example 3 1 class A { protected: int x; }; 2 class B : protected A {}; 3 int main() { A* ap; B* bp; 4 ap = bp; } Result: tryme3.cpp: In function int main() : tryme3.cpp:4: error: A is an inaccessible base of B CPSC 427a, Lecture 16 15/29

Names, Members, and Contexts Data and function names can be declared in many different contexts in C++: in a class, globally, in function parameter lists, and in code blocks (viz. local variables). Often the same identifier will be declared multiple times in different contexts. Two steps to determining the meaning of an occurrence of an identifier: 1. Determine which declaration it refers to. 2. Determine its accessibility according to the privacy rules. CPSC 427a, Lecture 16 16/29

Declaration and reference contexts Every reference x to a class data or function member has two contexts associated with it: The declaration context is the context in which the referent of x appears. The reference context is the context in which the reference x appears. Accessibility rules apply to class data and function members depend on both the declaration context and the reference context of a reference x. CPSC 427a, Lecture 16 17/29

Declaration context example Example: int x = 3; class A { int x; void f(int x) {...} void g() {int x;... } }; // declaration context: global // declaration context: A // declaration context: parameter // declaration context: block local CPSC 427a, Lecture 16 18/29

Reference context example class A { int x; int f() {return x;} int g(a* p) {return p->x;} }; int main() { A obj; obj.x; } // reference context A // reference context A // reference context global All three commented occurrences of x have declaration context A because all three refer to A::x, the data member declared in class A. CPSC 427a, Lecture 16 19/29

Inside and outside class references A reference x to a data/function member of class A is inside class A if the reference context of x is A; outside class A otherwise. For simple classes: an inside reference x is always valid. an outside reference x is valid iff the referent is public. CPSC 427a, Lecture 16 20/29

Examples References to A::x class A { int x; int f() { return x; } int g(a* p) { return p->x; } int h(); }; int A::h () { return x; } #include <iostream> int main() { A aobject; std::cout << aobject.x; }; // inside // inside // inside // outside CPSC 427a, Lecture 16 21/29

Inherited names In a derived class, names from the base class are inherited by the derived class, but their privacy settings are altered as described in the last lecture. The result is that the same member exists in both classes but with possibly different privacy settings. Question: Which privacy setting is used to determine visibility? Answer: The one of the declaration context of the referent. CPSC 427a, Lecture 16 22/29

Inheritance example class A { protected: int x; }; class B : private A { int f() { return x; } int g(a* p) { return p->x; } }; // ok, x is inside B // not okay, x is outside A Let bb be an instance of class B. Then bb contains a field x, inherited from class A. This field has two names A::x and B::x. The names are distinct and may have different privacy attributes. In this example, A::x is protected and B::x is private. First reference is okay since the declaration context of x is B. Second reference is not since the declaration context of x is A. Both occurrences have reference context B. CPSC 427a, Lecture 16 23/29

Inaccessible base class A base class pointer can only reference an object of a derived class if doing so would not violate the derived class s privacy. Recall surprising example 2 (bottom): 1 class A { }; 2 class B : private A {}; // <-- private derivation 3 int main() { A* ap; B* bp; 4 ap = bp; } The idea is that with private derivation, the fact that B is derived from A should be completely invisible from the outside. With protected derivation, it should be completely invisible except to its descendants. CPSC 427a, Lecture 16 24/29

Visibility rules Every class member has one of four privacy attributes: public, protected, private, or hidden. These attributes determine the locations from which a class member can be seen. public members can be seen from any location. protected members can be seen from inside the class or its children. private members can only be seen from inside the class. hidden members cannot be seen at all. CPSC 427a, Lecture 16 25/29

Explicit privacy attributes The privacy attributes for declared class members are given explicitly by the privacy keywords public, protected, and private. There is no way to explicitly declare a hidden member. Example: class A { private: int x; protected: int y; public: int z; }; CPSC 427a, Lecture 16 26/29

Implicit privacy attributes Inherited class members are assigned implicit privacy attributes based on their attributes in the parent class and by the kind of derivation, whether public, protected, or private. 1. If the member is public in the parent class, then its attribute in the child class is given by the kind of derivation. 2. If the member is protected in the parent class, then its attribute in the child class is protected for public and protected derivation, and private for private derivation. 3. If the member is private or hidden in the parent class, then it is hidden in the child class. CPSC 427a, Lecture 16 27/29

Implicit privacy chart Below is a revision of the chart presented in lecture 10. Attribute in base class Kind of Derivation public protected private public public protected private protected protected protected private private hidden hidden hidden hidden hidden hidden hidden Attribute in derived class. CPSC 427a, Lecture 16 28/29

Summary 1. All members of the base class are inherited by the derived class and appear in every instantiation of that class. 2. All inherited members receive implicitly defined privacy attributes. 3. Visibility of all data members is determined solely by their privacy attributes. 4. Public and protected base class variables are always visible within a derived class. 5. Private and hidden base class variables are never visible in the derived class. 6. The kind of derivation never affects the visibility of inherited members in the derived class; only their implicit attributes. CPSC 427a, Lecture 16 29/29