Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 1Winter Quarter Inheritance and Overloading.

Slides:



Advertisements
Similar presentations
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Advertisements

Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
1 Templates Chapter What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
Function Overloading. 2 Function Overloading (Function Polymorphism) Function overloading is a feature of C++ that allows to create multiple functions.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Introduction to C++ Programming
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 1Winter Quarter C++: I/O and Classes Lecture 25.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Chapter 10 Inheritance and Polymorphism
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
Computer Programming & Applications Mohamed Iqbal Pallipurath Lecture 02P. 1 Inheritance and Overloading Lecture 28.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Overview of C++ Templates
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
 2006 Pearson Education, Inc. All rights reserved Templates.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
User-Written Functions
Exceptions, Templates, and the Standard Template Library (STL)
Inheritance and Overloading
Templates.
Introduction to Programming
CMSC 202 Lesson 22 Templates I.
Overview of C++ Overloading
Inheritance: Polymorphism and Virtual Functions
Exception Handling.
Inheritance: Polymorphism and Virtual Functions
CPS120: Introduction to Computer Science
Overview of C++ Polymorphism
Introduction to Classes and Objects
Templates I CMSC 202.
Java Programming Language
Inheritance: Polymorphism and Virtual Functions
Computer Science II for Majors
Presentation transcript:

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 1Winter Quarter Inheritance and Overloading Lecture 28

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 2Winter Quarter Inheritance Objects are often defined in terms of hierarchical classes with a base class and one or more levels of classes that inherit from the classes that are above it in the hierarchy. For instance, graphics objects might be defined as follows:

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 3Winter Quarter Inheritance (continued) This hierarchy could, of course, be continued for more levels. Each level inherits the attributes of the above level. Shape is the base class. 2-D and 3-D are derived from Shape and Circle, Square, and Triangle are derived from 2-D. Similarly, Sphere, Cube, and Tetrahedron are derived from 3-D.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 4Winter Quarter Inheritance (continued) class A : base class access specifier B { member access specifier(s):... member data and member function(s);... } Valid access specifiers include public, private, and protected

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 5Winter Quarter Public Inheritance public base class (B) public members protected members private members derived class (A) public protected inherited but not accessible class A : public B {// Class A now inherits the members of Class B // with no change in the “access specifier” for }// the inherited members

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 6Winter Quarter Protected Inheritance protected base class (B) public members protected members private members derived class (A) protected inherited but not accessible class A : protected B {// Class A now inherits the members of Class B // with public members “promoted” to protected }// but no other changes to the inherited members

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 7Winter Quarter Private Inheritance private base class (B) public members protected members private members derived class (A) private inherited but not accessible class A : private B {// Class A now inherits the members of Class B // with public and protected members }// “promoted” to private

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 8Winter Quarter Inheritance (continued) class Shape { public: int GetColor ( ) ; protected:// so derived classes can access it int color; }; class Two_D : public Shape { // put members specific to 2D shapes here }; class Three_D : public Shape { // put members specific to 3D shapes here };

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 9Winter Quarter Inheritance (continued) class Square : public Two_D { public: float getArea ( ) ; protected: float edge_length; } ; class Cube : public Three_D { public: float getVolume ( ) ; protected: float edge_length; } ;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 10Winter Quarter Inheritance (continued) int main ( ) { Square mySquare; Cube myCube; mySquare.getColor ( ); // Square inherits getColor() mySquare.getArea ( ); myCube.getColor ( ); // Cube inherits getColor() myCube.getVolume ( ); }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 11Winter Quarter Function Overloading C++ supports writing more than one function with the same name but different argument lists. This could include: –different data types –different number of arguments The advantage is that the same apparent function can be called to perform similar but different tasks. The following will show an example of this.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 12Winter Quarter Function Overloading void swap (int *a, int *b) ; void swap (float *c, float *d) ; void swap (char *p, char *q) ; int main ( ) { int a = 4, b = 6 ; float c = 16.7, d = ; char p = 'M', q = 'n' ; swap (&a, &b) ; swap (&c, &d) ; swap (&p, &q) ; }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 13Winter Quarter Function Overloading void swap (int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } void swap (float *c, float *d) { float temp; temp = *c; *c = *d; *d = temp; } void swap (char *p, char *q) { char temp; temp = *p; *p = *q; *q = temp; }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 14Winter Quarter Function Templates We have discussed overloaded functions as a way to perform similar operations on data of different types. The swap functions were an example. We wrote three functions with the same name but different data types to perform the swap operations. Then we could call swap (&a, &b), for example, and C++ would select which function to use by matching the data type of a and b to one of the functions.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 15Winter Quarter Function Templates Another way to perform this task would be to create a function template definition. With a function template defined, when we call swap (&a, &b), C++ will generate the object code functions for us. The program on the following slides is an example.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 16Winter Quarter Function Templates template void swap (T *a, T *b) { T temp; temp = *a; *a = *b; *b = temp; } T is a “dummy” type that will be filled in by the compiler as needed a and b are of “type” T temp is of “type” T swap is a function template, NOT a function

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 17Winter Quarter Function Templates int main ( ) { int a = 5, b = 6; float c = 7.6, d = 9.8; char e = 'M', f = 'Z'; swap (&a, &b);// compiler puts int in for T swap (&c, &d);// compiler puts float in for T swap (&e, &f);// compiler puts char in for T cout << "a=" << a << " and b=" << b << endl; cout << "c=" << c << " and d=" << d << endl; cout << "e=" << e << " and f=” << f << endl; }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 18Winter Quarter Operator Overloading C++ already has a number of types (e.g., int, float, char, etc.) that each have a number of built in operators. For example, a float can be added to another float and stored in yet another float with use of the + and = operators: floatC = floatA + floatB; In this statement, floatB is passed to floatA by way of the + operator. The + operator from floatA then generates another float that is passed to floatC via the = operator. That new float is then stored in floatC by some method outlined in the = function.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 19Winter Quarter Operator Overloading (continued) Operator overloading means that the operators: –Have multiple definitions that are distinguished by the types of their parameters, and –When the operator is used, the C++ compiler uses the types of the operands to determine which definition should be used.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 20Winter Quarter Operator Overloading (continued) A programmer has the ability to re-define or change how the operators (+, -, *, /, =, >, etc.) work on their own classes. Overloading operators usually consists of defining a class member function called operator+ (where + is any operator). Note that operator is a reserved word in C++. If anything usually follows that operator, it is passed to the function. That function acts exactly like any other member function; it has the same scope as other member functions and can return a value just like any other member function.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 21Winter Quarter Operator Overloading (continued) Steps for defining an overloaded operator: 1. Name the operator being overloaded. 2.Specify the (new) types of parameters (operands) the operator is to receive. 3.Specify the type of value returned by the operator. 4.Specify what action the operator is to perform.