Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Chapter 12: Inheritance and Composition
Class and Objects.
Inheritance and Composition (aka containment) Textbook Chapter –
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
Chapter 14: Overloading and Templates
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 12: Inheritance and Composition.
Chapter 13: Overloading.
Chapter 15: Operator Overloading
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator overloading Object Oriented Programming.
Operator Overloading in C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
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: From Problem Analysis to Program Design, Fifth Edition Chapter 13: Inheritance and Composition.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
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.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Chapter 13: Inheritance and Composition
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++1 Chapter 1 Software Engineering Principles and C++ Classes.
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.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Operator Overloading.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes C++ representation of an object
Chapter 13: Overloading and Templates
Object-Oriented Design (OOD) and C++
A First C++ Class – a Circle
Chapter 15: Overloading and Templates
Overview of C++ Overloading
Operator Overloading.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 11: Inheritance and Composition
COP 3330 Object-oriented Programming in C++
Chapter 8: Class Relationships
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Classes C++ representation of an object
Presentation transcript:

Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++

Data Structures Using C++2 Chapter Objectives Learn about inheritance Learn about derived and base classes Explore how to redefine the member functions of a base class Examine how the constructors of base and derived classes work Learn how to construct the header file of a derived class

Data Structures Using C++3 Chapter Objectives Explore three types of inheritance: public, private, and protected Learn about composition Become familiar with the three basic principles of object-oriented design Learn about overloading Learn the restrictions on operator overloading

Data Structures Using C++4 Chapter Objectives Examine the pointer ‘this’ Learn about friend functions Explore the members and nonmembers of a class Discover how to overload various operators Learn about templates Explore how to construct function templates and class templates

Data Structures Using C++5 Inheritance “is-a” relationship Single inheritance –New class derived from one existing class (base class) Multiple inheritance –New class derived from more than one base class

Data Structures Using C++6 Inheritance General syntax to define a derived class:

Data Structures Using C++7 Inheritance Private members of base class –private to base class; cannot be accessed directly by derived class Derived class can redefine member functions of base class; redefinition applies only to object of subclass Redefine: function member in derived class has same name, number, and type of parameters as function in base class

Data Structures Using C++8 Redefining Member Functions class baseclass { public: void print() const; private: int u,v; }; [ print prints u and v ] class derivedclass: public baseclass { public: void print() const; private: int x,y; }; [print prints x and y, then calls baseclass::print() ]

Data Structures Using C++9 Constructors of Derived and Base Classes Derived class can have its own private data members Constructors of derived class can (directly) initialize only private data members of the derived class Execution of derived class’s constructor triggers execution of one of base class’s constructors Call to base class’s constructor specified in heading of derived class constructor’s definition

Data Structures Using C++10 class baseclass { public: baseclass(); baseclass(int x,int y); private: …; }; class derivedclass: public baseclass { public: derivedclass(); derivedclass(int x,int y, int z); private: … ; }; derivedclass::derivedclass(int x,int y,int z) : baseclass(x,y) { // set derivedclass’s private vars here } Constructors of Derived and Base Classes (Example)

Data Structures Using C++11 Header Files Created to define new classes Definitions of base classes contained in header files Header files of new classes contain commands telling computer where to look for definitions of base classes

Data Structures Using C++12 Preprocessor Commands //Header file test.h #ifndef H_test #define H_test const int ONE = 1; const int TWO = 2; #endif  #ifndef H_test means “if not defined H_test”  #define H_test means “define H_test”  #endif means “end if” //Header file testA.h #include “test.h” //Program headerTest.cpp #include “test.h” #include “testA.h”

Data Structures Using C++13 Protected Members of a Class If derived class needs –access to member of base class AND –needs to prevent direct access of member outside base class Then member of base class declared with member access specifier “protected” See pages for inheritance types

Data Structures Using C++14 Composition One or more members of a class are objects of another class type “has-a” relation between classes E.g. class personalInfo has as members personType and dateType: class personalInfo { … private: personType name; dateType bDay; } Call to constructor of member objects specified in heading of definition of class’s constructor

Data Structures Using C++15 Basic Principles of OOD Abstraction –Separating logical properties of data from the implementation (did this in Chapter 1) Encapsulation –combining data and operations into a single unit (did this in Chapter 1) Inheritance –ability to create new data types from existing ones  Polymorphism –using the same expression to denote different meanings, e.g., overloading, overriding

Data Structures Using C++16 Operator Overloading Operator function: function that overloads an operator cube s(5), t(7); if (s < t) { ++s; } To overload an operator –Write function definition (header and body) –Name of function that overloads an operator is reserved word operator followed by operator overloaded

Data Structures Using C++17 Operator Overloading The syntax of the heading of an operator function: returnType operator operatorSymbol (arguments) Cube example: class cube { private: int size; public: bool operator<(const cube &) const; cube &operator++(); cube operator++(int);... }; bool cube::operator<(const cube &right) const { return (size < right.size); } cube &cube::operator++() { // prefix ++ size++; return (*this); } cube cube::operator++(int) { // postfix ++ cube x(*this); size++; return (x); }

Data Structures Using C++18 Operator Overloading Restrictions You cannot change the precedence of an operator The associativity cannot be changed You cannot use default arguments You cannot change the number of arguments You cannot create new operators The meaning of how an operator works with built-in types, such as int, remains the same Operators can be overloaded either for objects of the user- defined type, or for a combination of objects of the user- defined type and objects of the built-in type

Data Structures Using C++19 Pointer this Every object of a class maintains a (hidden) pointer to itself Name of pointer is this In C++, this is a reserved word Available for you to use When object invokes member function, the member function references the pointer this of the object

Data Structures Using C++20 friend Functions of Classes friend function: nonmember function with access to private data members To make function a friend to a class, reserved word friend precedes prototype Word friend appears only in prototype, not in definition of the friend function

Data Structures Using C++21 Friend Example class exampleclass { friend void friendfunc(exampleclass arg); public:... private: int x; } void friendfunc(exampleclass arg) { exampleclass local; arg.x = 7; local.y=9; }

Data Structures Using C++22 Operator Functions as Member and Nonmember Functions A function that overloads any of the operators (), [], ->, or = for a class must be declared as a member of the class Assume opOverClass has overloaded op –If the leftmost operand of op is not an object of opOverClass, the overloaded function must be a nonmember (friend of opOverClass ) –If the op function is a member of opOverClass, then when applying op to opOverClass objects, the leftmost operand of op must be of type opOverClass

Data Structures Using C++23 Overloading Binary Operators as Member Functions Function Prototype (to be included in the definition of the class): returnType operator op(const className&) const; Function Definition: returnType className::operator op ( const className& otherObject) const { //algorithm to perform the operation return (value); }

Data Structures Using C++24 Overloading Binary Operators as Nonmember Functions Function Prototype (to be included in the definition of the class): friend returnType operator op(const className&, const className&); Function Definition: returnType operator op(const className& firstObject, const className& secondObject) { //algorithm to perform the operation return (value); }

Data Structures Using C++25 Overloading the Stream Insertion Operator (<<) Function Prototype (to be included in the definition of the class): friend ostream& operator<<(ostream&, const className&); Function Definition: ostream& operator<<(ostream& osObject, const className& object) { //local declaration if any //Output the members of the object //osObject<<... //Return the ostream object return osObject; }

Data Structures Using C++26 Overloading the Stream Extraction Operator (>>) Function Prototype (to be included in the definition of the class): friend istream& operator>>(istream&, className&); Function Definition: istream& operator>>(istream& isObject, className& object) { //local declaration if any //Read the data into the object //isObject>>... //Return the istream object return isObject; }

Data Structures Using C++27 Overloading Unary Operators If the operator function is a member of the class, it has no parameters –Exception (sort of): dummy “int” argument for postfix ++, -- operators If the operator function is a non-member (a friend), it has one parameter

Data Structures Using C++28 Programming Example: Complex Numbers

Data Structures Using C++29 Overloading Functions Example: constructors cube::cube() {size = 0;} cube::cube(int s) {size = s;} Can do this with any function How does the complier know which one to use? –Correct function chosen by formal parameter list

Data Structures Using C++30 Templates Powerful C++ tools Use a function template to write a single code segment for a set of related (overloaded) functions Use a class template for related classes

Data Structures Using C++31 Syntax template declaration; Syntax of the function template: template function definition; Syntax for a class template: template class declaration

Data Structures Using C++32 Template Example Returns the larger of x and y of whatever type template Type larger(Type x, Type y) { if (x >= y) return x; else return y; } What Type will be used in the following? cout << larger(5,6) << endl;

Data Structures Using C++33 Class Templates E.g. the elements in a list ADT could be char s, int s, double s, etc. –Don’t want to have to define same class for each type! template class listType {... bool isEmpty(); void insert(const elemType & newElement);... elemType list[100];... } To instantiate a list of ints: listType intList;