Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
OOP: Inheritance By: Lamiaa Said.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
C++ Inheritance Systems Programming.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Rossella Lau Lecture 7, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 7: OOP: Inheritance  Inheritance and composition.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Inheritance and Composition.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 12: Inheritance and Composition.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 12: Inheritance and Composition.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
LECTURE 07 Programming using C# Inheritance
Inheritance using Java
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 13: Inheritance and Composition.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 12: Adding Functionality to Your Classes.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 13: Inheritance and Composition
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 10 Inheritance and Polymorphism
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Foundations: Language mechanisms and primitive OO concepts Lecture 1: Classification and Inheritance Michigan State University Spring 2008 E. Kraemer Notes.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
Chapter -6 Polymorphism
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.
Chapter 3 Pointers and Array-Based Lists Dr. Youssef Harrath
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.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
1 Chapter 4: Another way to define a class Inheritance..!!
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Classes - Intermediate
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
1 CS 132 Spring 2008 Chapter 1 Software Engineering Principles and C++ Classes.
A First Book of C++ Chapter 12 Extending Your Classes.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
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.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
1 More About Derived Classes and Inheritance Chapter 9.
Classes (Part 1) Lecture 3
CHAPTER 14 INHERITANCE AND COMPOSITION
Object-Oriented Design (OOD) and C++
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11: Inheritance and Composition
CIS 199 Final Review.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Another way to define a class
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath

Outline 1. Inheritance 2. Composition 3. Polymorphism Function Overloading Templates 2 Dr. Youssef Harrath – University of Bahrain – 2010/2011

Inheritance Inheritance is an Object-Oriented Design concept which relates many classes together to allow specific classes using members of general classes. It’s a kind of a hierarchy relation between different classes of different levels. The general syntax to define a derived class is: Where memberAccessSpecifier is public, protected, or private (by default). class className: memberAccessSpecifier baseClassName { member list }; 3 Dr. Youssef Harrath – University of Bahrain – 2010/2011

Inheritance private members of the base can not be directly accessed by members of the derived class. public members of a base class can be inherited either as public or private members by the derived class. The derived class can include additional data/functions The derived class can redefine the public functions of the base class. All the data/function members of the base class are members of the derived class (except the redefined functions). 4 Dr. Youssef Harrath – University of Bahrain – 2010/2011

Inheritance: Example shape circlerectangle square “is a” 5 Dr. Youssef Harrath – University of Bahrain – 2010/2011

Inheritance: Example Suppose that we have the class shape: class circle: public shape { // public inheritance. }; class circle: private shape //by default { // private inheritance. }; // the public members of shape become private members of circle (any object of type circle can not directly access these members) 6 Dr. Youssef Harrath – University of Bahrain – 2010/2011

Inheritance: Overriding Public functions of the base class can be redefined (overrided) in a derived class (subclass). The redefined function in the subclass must have the same name, number, and types of parameters (formal parameter list). If both functions in the base class and in the derived class have the same name but different formal parameter list, this is called overloading (allowed). 7 Dr. Youssef Harrath – University of Bahrain – 2010/2011

Inheritance: Overriding class baseClass { public: void print () ; private: int u, v; // not accessible directly in the derived class char ch; }; void baseClass::print() { cout <<"Base Class: u = "<<u <<", v = "<<v <<" ch = "<<ch<<endl; } 8 Dr. Youssef Harrath – University of Bahrain – 2010/2011

Inheritance: Overriding 9 class derivedClass: public baseClass { public: void print () ; private: int first; double second; }; void derivedClass::print() { baseClass::print(); //call of the public print function of the // base class to print the private members cout <<"Derived Class: first = "<<first <<", second = "<<second<<endl; } Dr. Youssef Harrath – University of Bahrain – 2010/2011

Inheritance: Constructors 10 class baseClass { public: void print (); baseClass(); // constructor 1 baseClass(int x, int y); // constructor 2 baseClass(int x, int y, char w); // constructor 3 private: int u, v; // not accessible directly in the derived class char ch; }; Dr. Youssef Harrath – University of Bahrain – 2010/2011

Inheritance: Constructors 11 baseClass -u: int -v: int -ch: char +print(): void +baseClass() +baseClass(int, int) +baseClass(int, int, char) UML diagram of the class baseClass Dr. Youssef Harrath – University of Bahrain – 2010/2011

Inheritance: Constructors 12 void baseClass::print() { // see slide 8 } baseClass:: baseClass() // constructor 1 { u = 0; v = 0; ch = ‘*’; } baseClass:: baseClass(int x, int y) // constructor 2 { u = x; v = y; ch = ‘*’; } baseClass:: baseClass(int x, int y, char w) // constructor 3 { u = x; v = y; ch = w; } Dr. Youssef Harrath – University of Bahrain – 2010/2011

Inheritance: Constructors 13 class derivedClass: public baseClass { public: void print (); derivedClass(); // constructor 1 derivedClass(int x, int y, int one, double two); // constructor 2 derivedClass(int x, int y, char w, int one, double two); // constructor 3 private: int first ; double second; }; Dr. Youssef Harrath – University of Bahrain – 2010/2011

Inheritance: Constructors 14 derivedClass -first: int -second: double +print(): void +derivedClass() +derivedClass(int, int, int, double) +derivedClass(int, int, char, int, double) UML diagram of the class derivedClass and inheritance hierarchy Dr. Youssef Harrath – University of Bahrain – 2010/2011 baseClass derivedClass

Inheritance: Constructors Dr. Youssef Harrath – University of Bahrain – 2010/ void derivedClass::print() { // see slide 9 } derivedClass::derivedClass() // default constructor { // default constructor of the base class will be invoked first = 0; second = 0; } derivedClass::derivedClass(int x, int y, int one, double two) :baseClass(x, y) { first = one; second = two; } derivedClass::derivedClass(int x, int y, char w, int one, double two) :baseClass(x, y, w) { first = one; second = two; }

Inheritance: Header files A C++ programming practice is to create header files (.h extension) for the classes Suppose that baseClass is placed in the header file baseClass.h, the definition of the class derivedClass in a new file must start with #include “baseClass.h” 16 Dr. Youssef Harrath – University of Bahrain – 2010/2011

17 Composition Composition is another way to relate two classes. Composition is used for objects that have a “has-a” relationship to each other. In order to facilitate the building of complex classes from simpler ones, C++ allows us to do object composition in a very simple way — by using classes as member variables in other classes.

Dr. Youssef Harrath – University of Bahrain – 2010/ class personType { public: void print(); void setName(string first, string last); void getName(string& first, string& last); personType(string first, string last); private: string firstName; string lastName; }; Composition

Dr. Youssef Harrath – University of Bahrain – 2010/ Composition class dateType { public: void printDate(); void setDate(int month, int day, int year); void getDate(int& month, int& day, int& year); dateType(int month, int day, int year); private: int dMonth; int dDay; int dYear; };

Dr. Youssef Harrath – University of Bahrain – 2010/ Composition class personalInfoType { public: void printpersonalInfo(); void setpersonalInfo(string first, string last, int month, int day, int year, int ID); personalInfoType(string first, string last, int month, int day, int year, int ID); private: personType name; dateType bDate; int personID; };

Dr. Youssef Harrath – University of Bahrain – 2010/ Composition void personalInfoType::printpersonalInfo() { name.print(); cout<<“ ‘s date of birth is “; bDay.printDate(); cout<<endl; cout<<“and personal ID is “<<personID; }

Dr. Youssef Harrath – University of Bahrain – 2010/ Composition void personalInfoType::setpersonalInfo(string first, string last, int month, int day, int year, int ID) { name.setName(first, last); bDay.setDate(month, day, year); personID = ID; }

Dr. Youssef Harrath – University of Bahrain – 2010/ Composition personalInfoType::personalInfoType(string first, string last, int month, int day, int year, int ID) :name(first, last), bDay(month, day, year) { personID = ID; }

Dr. Youssef Harrath – University of Bahrain – 2010/ Polymorphism Polymorphism is one of the principles of Object-Oriented Design (OOD). Polymorphism will be discussed via overloading and then via templates. Overloading is a OOD concept to allow the programmer defining operators/functions with same name but different formal parameter list. Templates enable the programmer to write generic codes for related function and classes.

Dr. Youssef Harrath – University of Bahrain – 2010/ Polymorphism: Function Overloading int largerInt(int x, int y); char largerChar(char first, char second); double largerDouble(double u, double v); string largerString(string first, string second); int larger(int x, int y); char larger(char first, char second); double larger(double u, double v); string larger(string first, string second);

Dr. Youssef Harrath – University of Bahrain – 2010/ Polymorphism: Templates Templates are very powerful features in C++. Templates enable the user writing a single code segment for a set of related functions: function template, and for related classes: class template. The syntax for function templates is: template function definition; The syntax for class templates is: template class declaration

Dr. Youssef Harrath – University of Bahrain – 2010/ Polymorphism: Function Templates template // Part 1 Type larger(Type x, Type y); void main() { cout<<“Line 1: Larger of 5 and 6 = “<<larger(5, 6)<<endl; cout<<“Line 2: Larger of A and B = “<<larger(‘A’, ‘B’)<<endl; cout<<“Line 3: Larger of 5.6 and 3.2 = “<<larger(5.6, 3.2)<<endl; string s1 = “Hello”, s2 = “Happy”; cout<<“Line 4: Larger of “<<s1<<“ and “<<s2<<“ = “<<larger(s1,s2)<<endl; }

Dr. Youssef Harrath – University of Bahrain – 2010/ Polymorphism: Function Templates template // Part 2 Type larger(Type x, Type y) { if( x >= y) return x; else return y; } Output Line 1: Larger of 5 and 6 = 6 Line 2: Larger of A and B = B Line 3: Larger of 5.6 and 3.2 = 5.6 Line 4: Larger of Hello and Happy = Hello

Dr. Youssef Harrath – University of Bahrain – 2010/ Polymorphism: Class Templates template class listType { public: bool isEmpty(); bool isFull(); void search(const elemType& searchItem, bool& found); void insert(const elemType& newElement); private: elemType list[100]; int length; }; … listType intList; // declares intList to be a list of 100 integers listType stringList; // declares stringList to be a list of 100 strings