Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.

Slides:



Advertisements
Similar presentations
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Advertisements

Shape.h Shape Point Circle Cylinder // SHAPE.H
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
. Plab – Tirgul 6 Classes. Point.h class Point { public: Point(int x, int y); ~Point(); int getX() const; int getY() const; private: int m_x, m_y; };
Inheritance1 Inheritance Software re-use with derived classes.
Outline 1 Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
1 CSE 303 Lecture 21 Classes and Objects in C++ slides created by Marty Stepp
1 Lecture Note 9_Polymorphism Outline Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived-Class object Virtual.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
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.
Binary Search Trees II Morse Code.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
C++ Lecture 7 Thursday, 21 July Chapter 9 Inheritance l Creating new classes from the existing classes l The notions of base classes and derived.
1 Huffman Codes Drozdek Chapter Objectives You will be able to Construct an optimal variable bit length code for an alphabet with known probability.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Abstract ClassestMyn1 Abstract Classes Virtual functions in a base class must be defined unless they are declared to be pure virtual (abstract) functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
LECTURE LECTURE 15 Inheritance Text book p
Chapter 10 Inheritance and Polymorphism
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
1 Original Source : and Problem and Problem Solving.ppt.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
1 Command Processor II. 2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
1 Huffman Codes Drozdek Chapter Encoding Next we will add the capability to encode a message entered as normal text. The Huffman Tree that we use.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 Introduction to Object Oriented Programming Chapter 10.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes. Understand the meaning of polymorphism and how it works.
1 The Standard Template Library Drozdek Section 3.7.
1 Implementing Ticket Printer. Class Diagram 2 Dependencies A class that contains objects of another class, or has a reference to another class, depends.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
Abstract Classes. Recall We have the ability to create hierarchies of classes. Good design practice suggests that we move common methods as high as possible.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
1 Using an XML Parser. 2 Objective You will be able to use a publically available open source parser to convert an XML file into an internal data structure.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
1 More About Derived Classes and Inheritance Chapter 9.
CMSC 202 Polymorphism.
Advanced Program Design with C++
Polymorphism.
Chapter 5 Classes.
CS250 Introduction to Computer Science II
3.3 Abstract Classes, Assignment, and Casting in a Hierarchy
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Chapter 20- Virtual Functions and Polymorphism
Inheritance: Polymorphism and Virtual Functions
Programming II Polymorphism A.AlOsaimi.
Polymorphism CMSC 202, Version 4/02.
Chapter 20 - C++ Virtual Functions and Polymorphism
Object-Oriented Programming: Polymorphism
Inheritance: Polymorphism and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Virtual Functions Example
Inheritance: Polymorphism and Virtual Functions
Presentation transcript:

Abstract Classes 1

Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2

Getting Started Create a new C++ console project. 3

4

5

Add New Item 6

main.cpp 7

8 Build and run

Program Running 9 Add Class Point and Class Circle

Point.h #pragma once #include using std::ostream; class Point { private: int x; int y; public: Point(int X, int Y); friend ostream& operator<<(ostream& os, const Point& p); }; 10

Point.cpp #include "Point.h" using namespace std; Point::Point(int X, int Y) : x(X), y(Y) {} ostream& operator<<(ostream& os, const Point& p) { os << "(" << p.x << "," << p.y << ")"; return os; } 11

Circle.h #pragma once #include #include "Point.h" using std::string; using std::ostream; class Circle { private: int radius; string name; Point center; public: Circle(const string& Name, const Point& Center, int Radius); string Name() const {return name;}; Point Center() const {return center;}; int Radius() const {return radius;}; double Area() const {return * radius;}; bool operator<(const Circle& rhs) const {return Area() < rhs.Area();}; void Display(ostream& os) const; }; 12

Circle.cpp #include "Circle.h" using namespace std; Circle::Circle(const string& Name, const Point& Center, int Radius) : name(Name), center(Center), radius(Radius) {} void Circle::Display(ostream& os) const { os << "Circle: " << name << ", " << "Center: " << center << ", " << "Radius: " << radius << endl; } 13

main.cpp #include #include "Point.h" #include "Circle.h" using namespace std; int main() { cout << "This is the Shapes program\n"; Point pt(10, 12); Circle* c1 = new Circle("C1", pt, 6); c1->Display(cout); cout Name() << " is " Area() << endl; Circle* c2 = new Circle("C2", pt, 10); c2->Display(cout); cout Name() << " is " Area() << endl; if (*c1 < *c2) { cout Name() Name() << endl; } cout << "Normal termination\n"; cin.get(); return 0; } 14

Add to main.cpp cout << "This is the Shapes program\n"; Point pt(10, 12); Circle* c1 = new Circle("C1", pt, 6); c1->Display(cout); cout Name() << " is " Area() << endl; Circle* c2 = new Circle("C2", pt, 10); c2->Display(cout); cout Name() << " is " Area() << endl; if (*c1 < *c2) { cout Name() Name(); } else { cout Name() Name(); } cout << endl << endl; cout << "Normal termination\n"; 15

Program Running 16

New Class Suppose we find that we need a Rectangle class. Add new class Rectangle to the project. 17

#pragma once #include #include "Point.h" using namespace std; class Rectangle { private: string name; Point center; int width; int height; public: Rectangle(string Name, const Point& Center, int W, int H); string Name() const {return name;}; Point Center() const {return center;}; int Width() const {return width;}; int Height() const {return height;}; double Area() const {return width*height;}; bool operator<(const Rectangle& rhs) const { return Area() < rhs.Area(); }; void Display(ostream& os) const; }; 18 Rectangle.h

Rectangle.cpp #include "Rectangle.h" Rectangle::Rectangle(string Name, const Point& Center, int W, int H) : name(Name), center(Center), width(W), height(H) {} void Rectangle::Display(ostream& os) const { os << "Rectangle: " << name << ", " << "Center: " << center << ", " << "Width: " << width << ", " << "Height: " << height << endl; } 19

Add to main.cpp #include "Rectangle.h"... Rectangle* r1 = new Rectangle("R1", pt, 3, 5); r1->Display(cout); cout Name() << " is " Area() << endl; Rectangle* r2 = new Rectangle("R2", pt, 4, 4); r2->Display(cout); cout Name() << " is " Area() << endl; if (*r1 < *r2) { cout Name() Name() << endl; } else { cout Name() Name() << endl; } cout << endl; 20

Program Running 21

Duplicated Code There is a lot of overlap between class Circle and class Rectangle. name center Name() Center() operator<() 22

Duplicated Code Duplicated code is usually a bad idea! Let's factor out the common code as a base class and let Circle and Rectangle inherit it. Add class Shape. Modify class Circle and class Rectangle to inherit from class Shape 23

Class Shape #pragma once #include #include "Point.h" using std::string; using std:: ostream; class Shape { protected: string name; Point center; public: Shape(const string& Name, const Point& Center); string Name() const {return name;}; Point Center() const {return center;}; virtual double Area() const {return 0;}; virtual bool operator<(const Shape& rhs) const {return Area() < rhs.Area();}; virtual void Display(ostream& os) const; }; 24

Shape.cpp #include "Shape.h" using namespace std; Shape::Shape(const string& Name, const Point& Center) : name(Name), center(Center) {} void Shape::Display(ostream& os) const { os << "Shape: " << name << ", " << "Center: " << center << endl; } 25

Circle.h #pragma once #include #include "Shape.h" #include "Point.h" using std::string; using std:: ostream; class Circle : public Shape { private: int radius; //string name; //Point center; public: Circle(const string& Name, const Point& Center, int Radius); //string Name() const {return name;}; //Point Center() const {return center;}; int Radius() const {return radius;}; double Area() const {return * radius;}; //bool operator<(const Circle& rhs) const {return Area() < rhs.Area();}; void Display(ostream& os) const;}; 26

Circle.cpp #include "Circle.h" #include "Shape.h" using namespace std; Circle::Circle(const string& Name, const Point& Center, int Radius) : Shape(Name, Center), radius(Radius) {} void Circle::Display(ostream& os) const { os << "Circle: " << name << ", " << "Center: " << center << ", " << "Radius: " << radius << endl; } 27

Rectangle.h #pragma once #include #include "Point.h" #include "Shape.h" using namespace std; class Rectangle : public Shape { private: //string name; //Point center; int width; int height; public: Rectangle(string Name, Point Center, int W, int H); //string Name() const {return name;}; //Point Center() const {return center;}; int Width() const {return width;}; int Height() const {return height;}; double Area() const {return width*height;}; //bool operator<(const Rectangle& rhs) const {return Area() < rhs.Area();}; void Display(ostream& os) const; }; 28

Rectangle.cpp #include "Shape.h" #include "Rectangle.h" using namespace std; Rectangle::Rectangle(string Name, Point Center, int W, int H) : Shape(Name, Center), width(W), height(H) {} void Rectangle::Display(ostream& os) const { os << "Rectangle: " << name << ", " << "Center: " << center << ", " << "Width: " << width << ", " << "Height: " << height << endl; } 29

Build and Run 30 Works the same. Redundant code has been eliminated. DRY!

Class Shape We can create objects of class Shape even though that really doesn't make sense! Shape* s1 = new Shape("S1", pt); s1->Display(cout); cout Name() << " is " Area() << endl; cout << endl; Shape* s2 = new Shape("S2", pt); s2->Display(cout); cout Name() << " is " Area() << endl; 31

Program Running 32

Abstract Classes Class Shape isn't meant to be instantiated. Exists only to serve as the base class for derived classes. Shape::Area() isn't meant to be invoked. Exists only to be overriden in derived classes. We can express these ideas by making class Shape an abstract class. 33

Abstract Classes Redefine Shape::Area as: virtual double Area() const = 0; The method is defined, but has no body. Called a pure virtual method. Cannot be invoked. Must be overridden in derived classes. Any class with one or more pure virtual methods is called an abstract class. Cannot be instantiated. Exists only to be a base class. 34

Compile Again 35

Fix the Problems In main.cpp: Comment out c1, c2, r1, r2 Shape* s1 = new Circle("S1", pt, 10); s1->Display(cout); cout Name() << " is " Area() << endl; cout << endl; Shape* s2 = new Rectangle("S2", pt, 3, 5); s2->Display(cout); cout Name() << " is " Area() << endl; 36

Updated Shapes Program Running 37

Using the < operator We can even compare a Circle and a Rectangle as Shapes. if (*s2 < *s1) { cout Name() Name() << endl; } else { cout Name() Name() << endl; } 38

Using the < operator 39

Summary To define a pure virtual method, write = 0; following the declaration in the class definition. Derived classes must provide a defintion in order to be instantiated. To make a class abstract, give the class at least one pure virtual function. Then it can only be used as a base for derived classes. 40