Abstract ClassestMyn1 Abstract Classes Virtual functions in a base class must be defined unless they are declared to be pure virtual (abstract) functions.

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
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.
C++ Review. User Defined Types Built-in data types are simple. Specific problems may demand aggregate/more complex data types. – Ex: polygons, matrices,
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
UML Class Diagram: class Rectangle
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
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.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
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.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Polymorphic support in C# Let us now examine third pillar of OOP i.e. Polymorphism.. Recall that the Employee base class defined a method named GiveBonus(),
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
C# G 1 CSC 298 Object Oriented Programming Part 2.
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
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.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CSC 143 O 1 CSC 143 Inheritance and Object Oriented Design.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
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.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
Learning Objectives Relationships Among Objects in an Inheritance Hierarchy. Invoking Base-class Functions from Derived Class Objects. Aiming Derived-Class.
Web Design & Development Lecture 9
Advanced Programming in Java
Advanced Programming in Java
Polymorphism.
CS250 Introduction to Computer Science II
UML Class Diagram: class Rectangle
Polymorphism Lec
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
Programming II Polymorphism A.AlOsaimi.
Polymorphism CMSC 202, Version 4/02.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Jeff West - Quiz Section 12
Advanced Programming in Java
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Overview of C++ Polymorphism
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
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.
Computer Science II for Majors
SPL – PS4 C++ Advanced OOP.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Abstract ClassestMyn1 Abstract Classes Virtual functions in a base class must be defined unless they are declared to be pure virtual (abstract) functions using the pure-specifier (with the syntax “=0”). An abstract class is a class that cannot be instantiated and is usually implemented as a class that has one or more pure virtual functions. A pure virtual function is one which must be overridden by any concrete (i.e., non-abstract) derived class.

Abstract ClassestMyn2 In general an abstract class is used to define an implementation and is intended to be inherited from by concrete classes. It is a way of forcing a contact between the class designer and the users of that class. If we wish to create a concrete class from an abstract class we must declare and define a matching member function for each abstract member function of the base class. Otherwise we will create a new abstract class.

Abstract ClassestMyn3 Sometimes the phrase ”pure abstract class” is used to mean a class that exclusively has pure virtual functions (and no data members). The concept of interface is mapped to pure abstract classes in C++, as there is no construction “interface” in C++.

Abstract ClassestMyn4 Since an abstract class contains one or more functions for which there is no definition, no objects can be created using an abstract class. However, you can create pointers to an abstract class. This allows an abstract class to be used as a base class. A pointer to an abstract class can be used to select the proper virtual function.

Abstract ClassestMyn5 #include "stdafx.h" #include using namespace System; using namespace std; //Abstract class: class Shape { public: Shape(int, int); //pure virtual function: virtual void draw()=0; protected: int posX, posY; }; Shape::Shape(int pX, int pY) { posX=pX; posY=pY; }

Abstract ClassestMyn6 class Rectangle: public Shape { public: Rectangle(int, int); virtual void draw(); }; Rectangle::Rectangle(int xVal, int yVal): Shape(xVal, yVal) {} void Rectangle::draw() { cout<<"Drawing rectangle at ("<<posX <<", "<<posY<<")."<<endl; } class Pentagon: public Shape { public: Pentagon(int, int); virtual void draw(); };

Abstract ClassestMyn7 Pentagon::Pentagon(int xVal, int yVal): Shape(xVal, yVal) {} void Pentagon::draw() { cout<<"Drawing pentagon at ("<<posX <<", "<<posY<<")."<<endl; } class Hexagon: public Shape { public: Hexagon(int, int); virtual void draw(); }; Hexagon::Hexagon(int xVal, int yVal): Shape(xVal, yVal) {}

Abstract ClassestMyn8 void Hexagon::draw() { cout<<"Drawing hexagon at ("<<posX <<", "<<posY<<")."<<endl; } int main(array ^args) { Shape* first=new Rectangle(5, 15); first->draw(); Shape* second=new Pentagon(6, 16); second->draw(); Shape* third=new Hexagon(7, 17); third->draw(); delete first; delete second; delete third; return 0; }