Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CSE 250: Data Structures Week 3 January 28 – February 1, 2008.
C++ fundamentals.
 By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes.
OOP Languages: Java vs C++
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Programming Languages and Paradigms Object-Oriented Programming.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Chapter 12: Adding Functionality to Your Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
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.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Week 1 - Friday.  What did we talk about last time?  Basic programming model  Other Java stuff  References  Static  Inner classes  Exceptions.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Programming Languages and Paradigms Object-Oriented Programming.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Object-Oriented Programming in C++
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Overview of C++ Polymorphism
Classes - Intermediate
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CSCI 171 Presentation 15 Introduction to Object–Oriented Programming (OOP) in C++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance and Polymorphism
Week 14 - Wednesday CS222.
C++, OBJECT ORIENTED PROGRAMMING
Week 14 - Monday CS222.
Object-Oriented Programming Part 1
Java Programming Language
Overview of C++ Polymorphism
Java Programming Language
C++ Object Oriented 1.
Presentation transcript:

Week 14 - Monday

 What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass by reference

 Let's write a complete C++ program that reads in:  A string  An integer  Then, it prints out the string however many times the integer specified

 When you want to dynamically allocate memory in C++, you use new (instead of malloc() )  No cast needed  It "feels" a lot like Java int* value = new int(); //make an int int* array = new int[100]; //array of ints Wombat* wombat = new Wombat(); //make a Wombat Wombat* zoo = new Wombat[100]; //makes 100 Wombats with the default constructor int* value = new int(); //make an int int* array = new int[100]; //array of ints Wombat* wombat = new Wombat(); //make a Wombat Wombat* zoo = new Wombat[100]; //makes 100 Wombats with the default constructor

 When you want to free dynamically allocated memory in C++, use delete (instead of free() )  If an array was allocated, you have to use delete[] int* value = new int(); //make an int delete value; Wombat* wombat = new Wombat(); delete wombat; Wombat* zoo = new Wombat[100]; delete[] zoo; //array delete needed int* value = new int(); //make an int delete value; Wombat* wombat = new Wombat(); delete wombat; Wombat* zoo = new Wombat[100]; delete[] zoo; //array delete needed

 You can compile C code with C++  Weird things can happen, but we aren't going to going into those subtle issues  However, you now know and love the standard C libraries  You can use them in C++ too  You just have to include different header files C Library Header C++ EquivalentPurpose ctype.hcctype Character manipulation limits.hclimits Constants for integer limits math.hcmath Math functions stdio.hcstdio C I/O functions stdlib.hcstdlib Random values, conversion, allocation string.hcstring Null-terminated string manipulation time.hctime Time functions

 A struct in C++ is actually just a class where all the members are public  You can even put methods in a struct in C++  Otherwise, it looks pretty similar  You don't have to use the struct keyword when declaring struct variables  Except in cases when it is needed for disambiguation

 Here's a TreeNode struct in C++  Write a tree insertion with the following signature struct TreeNode { int value; TreeNode* left; TreeNode* right; }; struct TreeNode { int value; TreeNode* left; TreeNode* right; }; void insert(TreeNode* &root, int data);

 Let's see how objects work in C++ by looking at classically important elements of OOP  Encapsulation  Dynamic dispatch  Polymorphism  Inheritance  Self-reference

 Information hiding  We want to bind operations and data tightly together  Consequently, we don't want you to touch our privates  Encapsulation in C++ is provided by the private and protected keywords  Unlike Java, you mark sections as public, private, or protected, not individual members and methods  Hardcore OOP people think that all data should be private and most methods should be public

class A { private: int a; public: int getA() { return a; } void setA(int value) { a = value; } }; class A { private: int a; public: int getA() { return a; } void setA(int value) { a = value; } };

 Allows code reuse  Is thought of as an "is-a" relationship  C++ allows multiple inheritance, but you should only use it if you know what you're doing, usually as part of a design pattern  Deriving a subclass usually means creating a "refined" or "more specific" version of a superclass

class B : public A { //has member and methods from A }; class C : public A { private: //has A stuff and more int c; public: int getC(){ return c; } void increment() { c++; } }; class B : public A { //has member and methods from A }; class C : public A { private: //has A stuff and more int c; public: int getC(){ return c; } void increment() { c++; } };

 A confusing word whose underlying concept many programmers misunderstand  Polymorphism is when code is designed for a superclass but can be used with a subclass  If AudiRS5 is a subtype of Car, then you can use an AudiRS5 where you could use a Car

void drive( Car* c ); //defined somewhere … class AudiRS5 : public Car {}; … Car car; AudiRS5 audi; drive( &audi ); //okay drive( &car ); //okay void drive( Car* c ); //defined somewhere … class AudiRS5 : public Car {}; … Car car; AudiRS5 audi; drive( &audi ); //okay drive( &car ); //okay

 Polymorphism can be used to extend the functionality of an existing method using dynamic dispatch  In dynamic dispatch, the method that is actually called is not known until run time

class A { public: virtual void print() { cout << "A";} }; class B : public A { public: void print() { cout << "B";} }; class A { public: virtual void print() { cout << "A";} }; class B : public A { public: void print() { cout << "B";} };

A a; B b; A* p; a.print(); // A b.print(); // B p = &a; p->print(); // A p = &b; p->print(); // B A a; B b; A* p; a.print(); // A b.print(); // B p = &a; p->print(); // A p = &b; p->print(); // B

 Objects are able to refer to themselves  This can be used to explicitly reference variables in the class  Or, it can be used to provide the object itself as an argument to other methods  Self-reference in C++ is provided in part through the this keyword  this is a pointer to the object you're inside of

class Stuff { private: int things; public: void setThings(int things) { this->things = things; } }; class Stuff { private: int things; public: void setThings(int things) { this->things = things; } };

class SelfAdder { public: void addToList(List& list) { list.add(this); } }; class SelfAdder { public: void addToList(List& list) { list.add(this); } };

 C++ madness  Templates in C++

 Keep working on Project 6