Classes in C++ C++ originally called "C with classes":

Slides:



Advertisements
Similar presentations
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advertisements

Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism CS351 – Programming Paradigms.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Chapter 10 Inheritance and Polymorphism
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
ISBN Object-Oriented Programming Chapter Chapter
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Overview of C++ Polymorphism
Variations on Inheritance Object-Oriented Programming Spring
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
258 3/30/98 CSE 143 Object-Oriented Programming [Chapter 11]
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
CMSC 202 Polymorphism.
Advanced Program Design with C++
Inheritance and Polymorphism
Object-Oriented Programming
Polymorphism.
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Interfaces and Inheritance
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Object Oriented Analysis and Design
Chapter 9 Inheritance and Polymorphism
Learning Objectives Inheritance Virtual Function.
Classes in C++ C++ originally called "C with classes":
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Programming II Polymorphism A.AlOsaimi.
Java – Inheritance.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Polymorphism.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Overview of C++ Polymorphism
Final and Abstract Classes
COP 3330 Object-oriented Programming in C++
Chapter 11 Inheritance and Encapsulation and Polymorphism
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Classes in C++ C++ originally called "C with classes": Swedish connection: Bjarne Stoustrup borrowed from Simula (’67) Simulating classes of real world objects C++ continues to evolve: Version 1.0 released by AT&T in 1986 Version 2.0 in 1990 (added multiple inheritance) Version 3.0 in 1992 (templates) ANSI standard in 1996 (exception handling, run time type info) C++ became dominant OOPL in early 90's Now Java and C# challenge A class extends the C++ type system: class Account { //creates a new type }; //Note: you need this semi-colon in C++ Account a1,a2; //define variables of type Account September 18

C structs A data structure for dates in C: What’s the problem? struct Date { int month, day, year; }; int set(struct Date*, int m, int d, int y); int print(struct Date*); What’s the problem? No information hiding No way to control access to data (obscure side effects) No way to prevent assigning an illegal value to month Changing representation of Date breaks all client code September 18

C++ structs C++ provides for closer coupling of data and functions: struct Date { int month, day, year; void set(int m, int d, int y); void print(); //implement elsewhere }; Invoke functions with variable . memberFunction(): Date today; //In C++, structs automatically define types today.set(9,29,1953); today.print(); Now we have data abstraction: Procedural abstraction hides details of code in functions Data abstraction couples data structure and functions Still, no information hiding: today.set(50,-10,0); September 18

C++ adds class C++ adds new keywords to support information hiding: class Date { int month, day, year; public: void set(int m, int d, int y); void print(); //implement elsewhere }; Members after public: are visible to clients: Date today; today.print(); today.month = 50; //is this legal? Members after class are by default private September 18

Member functions What can we add to Date to allow access to month? class Date { int month, day, year; public: void set(int m, int d, int y); void print(); //implement elsewhere int getMonth() { return month; } }; cout << today.getMonth(); //outside class Date Pros and cons of this approach? inline function is efficient, though it tends to break information hiding Let’s define set() and protects the month data: void Date :: set(int m, int d, int y) { assert(m >= 1 && m <= 31); //#include <assert.h> month = m; } How can we ensure that month data is set to valid values? September 18

Inheritance (class derivation) class Account generalizes many kinds of bank accounts: checking, savings, etc. C++ class derivation captures this generation: class Checking : public Account { public: Checking(float balance); Checking(); //default constructor }; : denotes derivation—Checking inherits from Account public derivation denotes subtype inheritance Account’s public methods accessible to instances of Checking Checking myChecking(200); //Checking constructor myChecking.getBalance(); //Account function September 18

OOP = Data abstraction + inheritance + dynamic binding Polymorphism: a function can mean different things at runtime Dynamic binding: defer function binding to a subtype until runtime Suppose we want to draw a heterogeneous collection of shapes? class Point { ... }; //a Point has x and y coordinates class Shape { protected: //accessible to subclasses but otherwise private Point center; //all Shapes have a center Point public: Point where() { return center; } virtual void move(Point to) //virtual can be overridden { center = to; draw(); } // by derived classes virtual void draw()=0; //a "pure" virtual function //draw() must be implemented by derived classes //... } September 18

Subclasses override virtual functions class Triangle: public Shape { Point sw, se, top; //Three points define triangle public: Triangle(Point a, Point b, Point c) : sw(a), se(b), top(c) {} draw() //implementing pure virtual function { put_line(sw,top); //draw line from sw to top put_line(top,se); //draw line from top to se put_line(se,sw); //draw line from se to sw } }; class Circle : public Shape { int radius; Circle(Point a, int r) : center(a), radius(r) {} draw(); //draw a circle using center and radius September 18

Calling a virtual function { //Construct some shapes Shape aShape; //illegal--why? Circle c(Point(20,30),7); //legal--what does it do? //Create an array of various shapes Shape* shapes[10]; //Why is this legal? shapes[0] = new Circle(Point(20,30),7); //assign a Circle shapes[1] = new Triangle(Point(50,50),Point(30,30),Point(40,40)); //... maybe assign other shapes, Rectangles, Squares, etc. for (int i=0; i < 10; i++) //draw all the shapes shapes[i]->draw(); //each shape draws itself! } Why do we say that elements of shapes array are polymorphic? How does polymorphic design support Open-Closed principle? September 18

Why dynamic binding? What kind of code does dynamic binding avoid? Avoids lots of switch statements, e.g.: switch (shapes[i]->isa) //each Shape derived class has an isa data member { case(triangle) Triangle::draw(); //test enumeration case(circle) Circle::draw(); //run specific draw() // ... } Why is the dynamic binding version better for big, growing programs? September 18