CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Operator Overloading Fundamentals
Class and Objects.
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
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
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
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
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.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
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.
Pointer Data Type and Pointer Variables
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.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
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.
Chapter 13: Inheritance and Composition
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
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.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
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-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
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.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
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.
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 14: More About Classes.
1 CS 132 Spring 2008 Chapter 1 Software Engineering Principles and C++ Classes.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
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
C++ Templates.
Overloading Operator MySting Example
Object-Oriented Design (OOD) and C++
Chapter 15: Overloading and Templates
Operator Overloading.
Overview of C++ Overloading
CISC/CMPE320 - Prof. McLeod
Chapter 11: Inheritance and Composition
COP 3330 Object-oriented Programming in C++
Classes C++ representation of an object
Presentation transcript:

CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer this * Operator overloading with friends ** Templates

Three Basic Principles of OOD Encapsulation –combining data and operations into a single unit –where have we done this? Inheritance –ability to create new data types from existing ones Polymorphism –using the same expression to denote different meanings, e.g., overloading, overriding –where have we done this?

Inheritance person student facultyUSresident Derive student from person: public members (e.g. setName) in person can be used in student student has additional members (gpa in private, setGPA in public) Multiple inheritance (we won't cover): faculty and USresident

person → student class personType { public: void print() const; void setName(string first, string last); void getName(string& first, string& last); personType(string first = "", string last = ""); private: string firstName; string lastName; }; class studentType: public personType { public: void setStudent(string first, string last, int gpa); void setGPA(int gpa); float getGPA(); studentType(string first = "", string last = "", int gpa = 0); private: int GPA; }; public members of personType are public in studentType: studentType student; student.setName("Fred", "Fink") //works

person → student class personType { public: void print() const; void setName(string first, string last); void getName(string& first, string& last); personType(string first = "", string last = ""); private: string firstName; string lastName; }; class studentType: private personType { public: void setStudent(string first, string last, int gpa); void setGPA(int gpa); float getGPA(); studentType(string first = "", string last = "", int gpa = 0); private: int GPA; }; public members of personType are not public in studentType: studentType student; student.setName("Fred", "Fink") //does not work different

Protected Members of a Class Problem: members of studentType cannot access firstName and lastName because they are private in personType If derived class needs access to members of the base class the members of base class are “protected” E.g. change "private" to "protected" in the parent class

person → student class personType { public: void print() const; void setName(string first, string last); void getName(string& first, string& last); personType(string first = "", string last = ""); protected: string firstName; string lastName; }; class studentType: public personType { public: void setStudent(string first, string last, int gpa); void setGPA(int gpa); float getGPA(); studentType(string first = "", string last = "", int gpa = 0); private: int GPA; }; private members of personType can be used in studentType implementation See person2.h different

Inheritance In person.h: public: void print() const; In student.h class studentType: public personType //public members of person can be used in derived class instances e.g. studentType aStudent; aStudent.print(); In student.h class studentType: private personType //person public members can be used only to implement derived class //but not on objects of that type e.g. studentType aStudent; aStudent.print(); //NOT ALLOWED Rarely used Example: a derived class patientType where you don't want to reveal patient names personp.h

Inheritance In person.h: protected: string firstName; //visible to derived classes string lastName; //visible to derived classes private: string firstName; //not visible to anyone except personTypeImp

Redefining Base Class Members print() defined in personType does not print the GPA Definition in studentType overrides print in personType studentTypeImp.cpp illustrates –redefinition –accessing protected members of personType (allowed because they are protected) –using the inherited function for print in studentType Pitfall: –constructors must be redefined

Inheritance Summary Public members of the base class –inherited by the derived class –can be directly accessed anyone Private members of base class –private to base class –cannot be directly accessed by anyone Protected members of base class –can be directly accessed only by derived class Derived class can redefine member functions of base class –redefined function must have the same name and parameters –redefinition applies only to objects of the derived class –if the same name is used with different parameters, it is different

Preprocessor Commands (p. 78) //Header file person.h #ifndef H_person #define H_person... the contents of personType.h #endif #ifndef H_ person means “if not defined H_person” #define H_ person means “define H_person” #endif means “end if” This prevents compiling a file twice Not required in Visual C++ See person2c.h

Composition (p. 80) Another (easier) way to relate two classes A member of a class in an object of another class type Analogous to records having other records as components –e.g. a person has a birthday –“has-a” relation between classes E.g.: class personalInfoType {... private: personType person; dateType bDay; int personID; };

Confusing section: Pointer this Pointer: –a variable that contains an address of another variable –when you declare a variable, its name and type is stored in a symbol table along with a pointer to the address of the variable –later: pointer types that will be an important way to implement dynamic data structures Every object of a class maintains a (hidden) pointer to itself called “this” (a reserved word) When the object invokes a member function, the member function references the pointer "this" of the object

Pointer this Example on p. 90 (personImp.cpp): // & means the object using the method is by reference // *this is the object that is modified personType& personType::setFirstName(string first) { firstName = first; return *this; } Better (personImpv.cpp): //the method operates directly on the object void personType::setFirstName(string first) { firstName = first; } Both are called via aPerson.setFirstName("Fred");

Polymorphism: Operator Overloading Recall how clumsy printing and addition are for complexType: num1.print(); num3 = num1.add(num2); Nicer: cout << num1; num3 = num1 + num2; Operator function: overloads an operator Syntax of the heading of an operator function: returnType operator operatorSymbol (arguments) e.g. complexType operator+(const complexType& otherComplex) const; See example files

Overloading + In driver (testComplex.cpp): num3 = num1.add(nu m2); // add is applied to object num1 with parameter num2 num3 = num1 + num2; // + is applied to object num1 with parameter num2 In complexType.h: complexType add(const complexType& otherComplex) const; complexType operator+(const complexType& otherComplex) const; Implementation in complexType.cpp (both the same!): complexType temp; temp.realPart = realPart + otherComplex.realPart; temp.imaginaryPart = imaginaryPart + oherComplex.imaginaryPart; return temp;

Overloading the Stream Insertion Operator (<<) Let's try this with: cout << aComplex object operator parameter But cout is ostream, not personType We need a friend: –a nonmember function with access to private data members To make function a friend to a class, reserved word friend precedes prototype friend appears only in prototype, not in the function definition

Overloading << Function prototype (included in the class): friend ostream& operator<<(ostream&, const complexType&); Function definition: ostream& operator<<(ostream& osObject, const complexType& complex) { osObject<<complex.realPart; osObject<<" + "; osObject<<complex.imaginaryPart; osObject<<"i"; return osObject; //pointer this format } text example displays data in (a,b) format. How would this differ? osObject<< "(" << complex.realPart << ", " << complex.imaginaryPart << ")";

Overloading >> in (a,b) format Earlier program: user entered a and b separately. What if the number is in (a,b) format? a. Read and discard left parenthesis( b. Read and store the real parta c. Read and discard the comma, d. Read and store the imaginary partb e. Read and discard the right parenthesis)

Overloading >> Function Prototype (included in the definition of the class): friend istream& operator>>(istream&, className&); Function Definition: istream& operator>>(istream& isObject, complexType& object) { char ch; isObject>>ch; isObject>>complex.realPart; isObject>>ch; isObject>>complex.imaginaryPart; isObject>>ch; return isObject; } no const since the object will change

Bad News Example programs do not compile 1.Change #include to #include 2.Remove using namespace std Why? A peculiarity of Visual C++

Overloading Unary Operators Unary operators: one argument E.g. a++ (unary) versus a + b (binary) The second argument was a parameter in the overloaded definition complexType operator+(const complexType& otherComplex) const; cComplex = aComplex + bComplex the object the parameter But there is no second argument: dateType operator++(); //get the next day aDate++; the object the operator

Templates Powerful C++ tools Use a function template to write a single code segment for a set of related functions that may operate on different types (E.G. max, last semester)max Use a class template for classes that may have similar functionality on different data types Example: –listType –we will use this in the next program to review arrays

Syntax Syntax for a function template: template //Type is the parameter function definition; Syntax for a class template: template class declaration New rule –the class header and implementation must be compiled together Options – put them both in the same file (what we will do) – put an include for the.cpp at the end of the header