CS Lecture #14

Similar documents
Lecture 11. Demonstration #1. More Inheritance, More Constructors. Absolute C++ Chapter 15. Virtual Functions. Overriding

CS Lecture #6. Administrative... Assignment #2 Graded NN Turned In, Average Grade: XX Assignment #3 due on Thursday

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

Written by John Bell for CS 342, Spring 2018

C++ Inheritance and Encapsulation

Inheritance, and Polymorphism.

CS 162, Lecture 25: Exam II Review. 30 May 2018

Short Notes of CS201

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

Memory, Arrays, and Parameters

CS201 - Introduction to Programming Glossary By

VIRTUAL FUNCTIONS Chapter 10

CS1004: Intro to CS in Java, Spring 2005

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

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

Project Compiler. CS031 TA Help Session November 28, 2011

Pointers, Arrays and Parameters

C++ Inheritance and Encapsulation

And Even More and More C++ Fundamentals of Computer Science

Increases Program Structure which results in greater reliability. Polymorphism

Administrivia. CMSC433, Fall 2001 Programming Language Technology and Paradigms. Administrivia (cont.) Project 1. Copy constructor.

C++ (classes) Hwansoo Han

Lecture 20: templates

1.00 Lecture 8. Using An Existing Class, cont.

a data type is Types

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

Sixth lecture; classes, objects, reference operator.

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

G52CPP C++ Programming Lecture 15

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

COMP322 - Introduction to C++

Chapter 1 Getting Started

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

Lecture 2: C Programm

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

CSC 533: Organization of Programming Languages. Spring 2005

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

COP 3330 Final Exam Review

Pointers II. Class 31

Separate Compilation Model

Lecture 13: more class, C++ memory management

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

Introduction to Programming Using Java (98-388)

The following is an excerpt from Scott Meyers new book, Effective C++, Third Edition: 55 Specific Ways to Improve Your Programs and Designs.

Object-Oriented Programming, Iouliia Skliarova

These are notes for the third lecture; if statements and loops.

C++ for Java Programmers

CSE 303: Concepts and Tools for Software Development

Lecture 14: more class, C++ streams

CS201- Introduction to Programming Current Quizzes

Software Engineering /48

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

BLM2031 Structured Programming. Zeyneb KURT

QUIZ. What is wrong with this code that uses default arguments?

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Polymorphism Part 1 1

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

04-24/26 Discussion Notes

Lecture 3: C Programm

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Overview. Elements of Programming Languages. Objects. Self-Reference

CS3157: Advanced Programming. Outline

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

M.EC201 Programming language

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Pointers, Dynamic Data, and Reference Types

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

JAVA An overview for C++ programmers

6.096 Introduction to C++

Memory and C++ Pointers

CS1004: Intro to CS in Java, Spring 2005

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

Array Elements as Function Parameters

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

Classes. C++ Object Oriented Programming Pei-yih Ting NTOU CS

1: Introduction to Object (1)

An Object Oriented Programming with C

CS

Object-Oriented Programming

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

CS61B Lecture #13: Packages, Access, Etc.

CS61B Lecture #13: Packages, Access, Etc.

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Variables, Memory and Pointers

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Object-Oriented Principles and Practice / C++

void Transaction::print() { cout << "Name: " << name << " Amount: " << amount << endl; }

Transcription:

CS 213 -- Lecture #14 We re starting to catch up! Administrative... Late Night Guide to C++ Chapter 9 pg 222-239 MORE ABOUT CLASSES Part I Interfaces in C++ For those with Java experience, you know that Java implements the concept of an interface. An interface is a class which cannot exist by itself, its sole purpose is to provide a set of methods which must be implemented by any class that derives from it. It also provides a way for other code to work with derived classes of the interface (both currently defined and to be defined ) without needing to know any of the details of the derived class. (Assign #4) In Java, an interface declaration looks like this: // WARNING! WARNING! What follows is JAVA code! public interface Comparable public int compareto(object b); Any Java class may implement the interface by the addition of one keyword and by providing definitions for each of the methods defined in the interface. // WARNING! WARNING! What follows is JAVA code! public Rectangle implements Comparable public int compareto(object b) // definition goes here So where did the Java engineers get the idea for the interface and implements keywords? By looking at C++, no doubt! In C++ there are no separate keywords corresponding to the interface and implements keywords in Java. This makes interfaces more of a concept than a language construct as far as C++ goes. The same concept of an interface is implemented in C++ by using abstract classes with pure virtual member functions. class Comparable virtual int compareto(comparable &c)=0; class Rectangle : public Comparable // Incomplete definition... int compareto(comparable &c) Since there are no keywords which allow you to specify an interface in C++, the following are suggestions and not requirements Interfaces are not used as standalone objects. This implies that there is at least one pure virtual member function. Interfaces are used to define (abstractly) a set of member functions. Other functions/objects will be utilizing the set of member functions defined in the interface. To implement the interface, all member functions must be defined in the derived class. What this buys us is the ability to create truly pluggable objects which will require minimal recompile time. An example of this is a simple message box object. You can have multiple objects (graphical dialog box, text messages, etc.) which all derive from one interface. They may then be used interchangeably... 1

Demonstration Interfaces in C++ Private Inheritance When looking at inheritance, we ve always used a declaration of the following form: class X : public Y I ve asked you to take it on faith that public Y is simply the syntax you must use to say that the class X is derived from. Now, consider the following partial definitions of X and Y... protected: int c,d; protected: int c,d; class X : public Y // Notice the change to private As we ve gone over before, a member function in class X has access to member variables a,b,c,d,h,i from the base. When working with X outside of the class, a,b,h,i are accessible. But, it we make use of private inheritance, the public (and protected) members of the base class become private members of the derived class. That is to say that no members from a privately inherited base class may be accessed from outside the scope of the derived class. It also means that the relationship X is a Y or X is a kind of Y doesn t really hold up here. Why? Because if a Y has certain public methods and X is a Y, then X should have the same public methods. With private inheritance this is not the case, as the public methods in Y are not public methods in X. So, then, what is private inheritance good for? LNG suggests the following (from pg. 231): This is what private inheritance is for. If A is to be implemented as a B, and if class B has virtual functions that A can usefully override, make A a private base class of B. Is this really useful? Wait before you decide... A pointer to a class derived privately from a base class may not be assigned directly to a pointer to the base class. X x; Y *yptr; yptr = &x; // declare an instance of the class X // pointer to an instance of base class // COMPILER ERROR. access violation Ouch! We ve been able to do this before but now the compiler won t let us :-(. Oh, wait, this is C++, it will let me do almost anything with a little coercion X x; Y *yptr; yptr = (Y *)&x; // declare an instance of the class X // pointer to an instance of base class // Oh, ok, this is fine 2

void dosomethinguseful(int); // arbitrary public member func Since dosomethinguseful() is a public member of Y, it is not accessible outside of the scope of X. That is... X anx; anx.dosomethinguseful(5); // access violation Attempting to access dosomethinguseful() from a variable of class X is an access violation. That s because dosomethinguseful() is a public method of a privately inherited base class. Bummer. Oh, wait, this is C++ Can I coerce my way around this restriction? You betcha! void dosomethinguseful(int); // arbitrary public member func Y::doSomethingUseful; Notice the bizarre syntax in the public section of Y. It is termed the fully qualified name of the member function dosomethinguseful() defined in. X anx; anx.dosomethinguseful(5); // now this is ok The addition of the line Y::doSomethingUseful; into the public section of the class definition of X solved our problem. Notice that only the name of the member function from the base class to be made public is used, there is no parameter list. So, back to my original question Is it useful? Let s see it in action first... Demonstration Private Inheritance Private Inheritance -- Is it Useful? Well, put a feature in a language and someone will find a way to use it! In my programming travels I have never seen it used. It s not for doing straight inheritance, because the relationships break down. It s not for doing interfaces since the pure virtual approach is much cleaner, simpler, and enforces the implementation of all members. The example given in the books shows a case where a generic base class is used to provide functionality to a more specific derived class. the derived class is, conceptually, different from the base class List vs. Stack Many of the features of the base class might not be applicable to the derived class. Removing the nth element of a list is not a stack-like function. In this case, private inheritance may be appropriate. 3

Declarations in Classes: Enumerations Since the class definition also provides a sense of scope, we can perform declarations and definitions within the class definition. Indeed, we do just that with member functions and member variables. But there are other types that can be declared and defined as well. We ll look at enumerations first. void setcounter(int argcounter=kinitialvalue); enum CounterValues kinitialvalue=0, klastvalue int counter; Declarations in Classes: Enumerations (cont) The constants defined in the enumeration may be used anywhere in the class, and in other classes by the following rules: If the enumerations are declared protected, they may be used only in derived classes. If the enumerations are declared public, they may be accessed from anywhere provided their fully qualified names are used: Example anexample; // In order to use the enumeration defined in Example // we must qualify it with the class name (Example::) anexample.setcounter(example::klastvalue); Declarations in Classes: Static Members A static member variable is a variable declared in the class definition but is treated like a global variable: There is only one copy of it no matter how many instances of its class exist If its value is changed by any one instance, the value is changed for all the instances In effect, it is a global variable but only visible outside the class if declared in the public section. Example() currenttotal = 0; void addtototal(int avalue); static int currenttotal; There s only one problem Since the static member doesn t really exist in the class, separate storage must be allocated for it outside the class. This is done in the form of a global variable using the fully qualified name of the static member... Example() currenttotal = 0; void addtototal(int avalue); static int currenttotal; int Example::currentTotal = 0; // Define the static member If you don t define space for the static member in this manner, you will get a linker error! The static member may be accessed outside of the class only if it is declared in one of the class public sections. If so, the static member may be accessed anywhere by using its fully qualified name: A static member function is a little different since there is already only one copy of the code used to implement a member function. A static member function has no this pointer and cannot access individual non-static member fields. However a static member function may be called without creating an instance of the class in which it is defined... Example e1; Example::currentTotal = 5; // There are two ways to cout << e1.currenttotal << endl; // access the same var! static void printbanner() cout << Hello World << endl; Example::printBanner(); 4

Demonstration Final Thoughts Assignment #7 will be posted on Thursday Only 11 assignments will be given Declarations in Classes 5