Esempi Ereditarietà1 // Definition of class Point #ifndef POINT_H #define POINT_H #include using std::ostream; class Point { friend ostream &operator<<(

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Inheritance Math 130 B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours B Smith: Consider using the example in SAMS Teach Yourself.
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Shape.h Shape Point Circle Cylinder // SHAPE.H
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
CPSC 231 C++ Review1 Learning Objectives §Review of the object oriented design goals. §Review of C++ classes l data members l member functions (methods)
Synthetic OO Design Concepts & Reuse Lecture 1: Program fragments and abstract classes CSE335 Spring 2008 E. Kraemer adapted from K. Stirewalt.
Outline 1 Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 - Inheritance Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes 19.3Protected.
1 Review (Week7)_ Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
1 Lecture Note 9_Polymorphism Outline Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived-Class object Virtual.
Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class.
Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public:
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
CS342 Data Structure Review of C++: Overriding base- class functions.
C++ Lecture 7 Thursday, 21 July Chapter 9 Inheritance l Creating new classes from the existing classes l The notions of base classes and derived.
Object-Oriented Programming: Polymorphism Zhen Jiang West Chester University
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
 2003 Prentice Hall, Inc. All rights reserved 1 Object-Oriented Programming Inheritance Main Objectives: To understand how inheritance.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 19 - C++ Inheritance Outline 19.1Introduction.
CMSC 2021 Polymorphism. CMSC 2022 Static vs. Dynamic Binding Binding The determination of which method in the class hierarchy is to be used for a particular.
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
Inheritance One of the most powerful features of C++
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
SMIE-121 Software Design II QQ: School of Mobile Information.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
Copyright  Hannu Laine C++-programming Part 9 Hannu Laine.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
Class 3 (L33) u Client of a Class u Purpose of Public Members u Private Class Members u Controlling Access u Access Functions u Predicate Functions u Utility.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Polymorphism Lecture 9 March 16, 2004.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2003 Prentice Hall, Inc. All rights reserved Virtual Functions Polymorphism –Same message, “print”, given to many objects All through a base.
 2003 Prentice Hall, Inc. All rights reserved. 1 Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among Objects.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class.
Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class.
1 Inheritance inheritance –a mechanism that build a new class by deriving from an existing class –derived class (or subclass) inherit from based class.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Chapter 19 - C++ Inheritance
C++ Programming Functions
Chapter 9 - Object-Oriented Programming: Inheritance
Chapter 10 - Object-Oriented Programming: Polymorphism
Lab6_II Object-Oriented Programming: Polymorphism
Operator Overloading, Inheritance Lecture 7 September 23, 2004
Chapter 20- Virtual Functions and Polymorphism
Chapter 19 - Inheritance Outline 19.1 Introduction
Chapter 20 - C++ Virtual Functions and Polymorphism
Object-Oriented Programming: Polymorphism
Capitolo 9 - Inheritance
Chapter 19 - Inheritance Outline 19.1 Introduction
Capitolo 10- Virtual Functions and Polymorphism
Chapter 9 - Object-Oriented Programming: Inheritance
Virtual Functions Example
Object-Oriented Programming: Polymorphism
Presentation transcript:

Esempi Ereditarietà1 // Definition of class Point #ifndef POINT_H #define POINT_H #include using std::ostream; class Point { friend ostream &operator<<( ostream &, const Point & ); public: Point( int = 0, int = 0 ); // default constructor void setPoint( int, int ); // set coordinates int getX() const { return x; } // get x coordinate int getY() const { return y; } // get y coordinate protected: // accessible by derived classes int x, y; // x and y coordinates of the Point }; #endif

Esempi Ereditarietà2 // Member functions for class Point #include #include "point.h" // Constructor for class Point Point::Point( int a, int b ) { setPoint( a, b ); } // Set x and y coordinates of Point void Point::setPoint( int a, int b ) { x = a; y = b; } // Output Point (with overloaded stream insertion operator) ostream &operator<<( ostream &output, const Point &p ) { output << '[' << p.x << ", " << p.y << ']'; return output; // enables cascaded calls }

Esempi Ereditarietà3 // Definition of class Circle #ifndef CIRCLE_H #define CIRCLE_H #include using std::ostream; #include using std::ios; using std::setiosflags; using std::setprecision; #include "point.h" class Circle : public Point { // Circle inherits from Point friend ostream &operator<<( ostream &, const Circle & ); #endif

Esempi Ereditarietà4 public: // default constructor Circle( double r = 0.0, int x = 0, int y = 0 ); void setRadius( double ); // set radius double getRadius() const; // return radius double area() const; // calculate area protected: double radius; };

Esempi Ereditarietà5 // Member function definitions for class Circle #include "circle.h" // Constructor for Circle calls constructor for Point // with a member initializer then initializes radius. Circle::Circle( double r, int a, int b ) : Point( a, b ) // call base-class constructor { setRadius( r ); } // Set radius of Circle void Circle::setRadius( double r ) { radius = ( r >= 0 ? r : 0 ); } // Get radius of Circle double Circle::getRadius() const { return radius; }

Esempi Ereditarietà6 // Calculate area of Circle double Circle::area() const { return * radius * radius; } // Output a Circle in the form: // Center = [x, y]; Radius = #.## ostream &operator<<( ostream &output, const Circle &c ) { output ( c ) << "; Radius = " << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 ) << c.radius; return output; // enables cascaded calls }

Esempi Ereditarietà7 // Casting base-class pointers to derived-class pointers #include using std::cout; using std::endl; #include #include "point.h" #include "circle.h" int main() { Point *pointPtr = 0, p( 30, 50 ); Circle *circlePtr = 0, c( 2.7, 120, 89 ); cout << "Point p: " << p << "\nCircle c: " << c << '\n'; // Treat a Circle as a Point (see only the base class part) pointPtr = &c; // assign address of Circle to pointPtr cout << "\nCircle c (via *pointPtr): " << *pointPtr << '\n';

Esempi Ereditarietà8 // Treat a Circle as a Circle (with some casting) // cast base-class pointer to derived-class pointer circlePtr = static_cast ( pointPtr ); cout << "\nCircle c (via *circlePtr):\n" << *circlePtr << "\nArea of c (via circlePtr): " area() << '\n'; // DANGEROUS: Treat a Point as a Circle pointPtr = &p; // assign address of Point to pointPtr // cast base-class pointer to derived-class pointer circlePtr = static_cast ( pointPtr ); cout << "\nPoint p (via *circlePtr):\n" << *circlePtr << "\nArea of object circlePtr points to: " area() << endl; return 0; }

Esempi Ereditarietà9 Point p: [30, 50] Circle c: Center = [120, 89]; Radius = 2.70 Circle c (via *pointPtr): [120, 89] Circle c (via *circlePtr): Center = [120, 89]; Radius = 2.70 Area of c (via circlePtr): Point p (via *circlePtr): Center = [30, 50]; Radius = 0.00 Area of object circlePtr points to: 0. 00

Esempi Ereditarietà10 // Definition of class Point #ifndef POINT2_H #define POINT2_H #include using std::ostream; class Point { friend ostream &operator<<( ostream &, const Point & ); public: Point( int = 0, int = 0 ); // default constructor void setPoint( int, int ); // set coordinates int getX() const { return x; } // get x coordinate int getY() const { return y; } // get y coordinate protected: // accessible to derived classes int x, y; // coordinates of the point }; #endif

Esempi Ereditarietà11 // Member functions for class Point #include "point2.h" // Constructor for class Point Point::Point( int a, int b ) { setPoint( a, b ); } // Set the x and y coordinates void Point::setPoint( int a, int b ) { x = a; y = b;} // Output the Point ostream &operator<<( ostream &output, const Point &p ) { output << '[' << p.x << ", " << p.y << ']'; return output; // enables cascading }

Esempi Ereditarietà12 // Definition of class Circle #ifndef CIRCLE2_H #define CIRCLE2_H #include using std::ostream; #include "point2.h" class Circle : public Point { friend ostream &operator<<( ostream &, const Circle & ); public: // default constructor Circle( double r = 0.0, int x = 0, int y = 0 ); void setRadius( double ); // set radius double getRadius() const; // return radius double area() const; // calculate area protected: // accessible to derived classes double radius; // radius of the Circle }; #endif

Esempi Ereditarietà13 // Member function definitions for class Circle #include using std::ios; using std::setiosflags; using std::setprecision; #include "circle2.h" // Constructor for Circle calls constructor for Point // with a member initializer and initializes radius Circle::Circle( double r, int a, int b ) : Point( a, b ) // call base-class constructor { setRadius( r ); } // Set radius void Circle::setRadius( double r ) { radius = ( r >= 0 ? r : 0 ); }

Esempi Ereditarietà14 // Get radius double Circle::getRadius() const { return radius; } // Calculate area of Circle double Circle::area() const { return * radius * radius; } // Output a circle in the form: // Center = [x, y]; Radius = #.## ostream &operator<<( ostream &output, const Circle &c ) { output ( c ) << "; Radius = " << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 ) << c.radius; return output; // enables cascaded calls }

Esempi Ereditarietà15 // Definition of class Cylinder #ifndef CYLINDR2_H #define CYLINDR2_H #include using std::ostream; #include "circle2.h" class Cylinder : public Circle { friend ostream &operator<<( ostream &, const Cylinder & ); public:// default constructor: Cylinder( double h = 0.0, double r = 0.0, int x = 0, int y = 0 ); void setHeight( double ); // set height double getHeight() const; // return height double area() const; // calculate and return area double volume() const; // calculate and return volume protected: double height; // height of the Cylinder }; #endif

Esempi Ereditarietà16 // Member and friend function definitions // for class Cylinder. #include "cylindr2.h" // Cylinder constructor calls Circle constructor Cylinder::Cylinder( double h, double r, int x, int y ) : Circle( r, x, y ) // call base-class constructor { setHeight( h ); } // Set height of Cylinder void Cylinder::setHeight( double h ) { height = ( h >= 0 ? h : 0 ); } // Get height of Cylinder double Cylinder::getHeight() const { return height; }

Esempi Ereditarietà17 // Calculate area of Cylinder (i.e., surface area) double Cylinder::area() const { return 2 * Circle::area() + 2 * * radius * height; } // Calculate volume of Cylinder double Cylinder::volume() const { return Circle::area() * height; } // Output Cylinder dimensions ostream &operator<<( ostream &output, const Cylinder &c ) { output ( c ) << "; Height = " << c.height; return output; // enables cascaded calls }

Esempi Ereditarietà18 // Driver for class Cylinder #include using std::cout; using std::endl; #include "point2.h" #include "circle2.h" #include "cylindr2.h" int main( ) { // create Cylinder object Cylinder cyl( 5.7, 2.5, 12, 23 ); // use get functions to display the Cylinder cout << "X coordinate is " << cyl.getX() << "\nY coordinate is " << cyl.getY() << "\nRadius is " << cyl.getRadius() << "\nHeight is " << cyl.getHeight() << "\n\n";

Esempi Ereditarietà19 // use set functions to change the Cylinder's attributes cyl.setHeight( 10 ); cyl.setRadius( 4.25 ); cyl.setPoint( 2, 2 ); cout << "The new location, radius, and height of cyl are:\n" << cyl << '\n'; cout << "The area of cyl is:\n" << cyl.area() << '\n'; // display the Cylinder as a Point Point &pRef = cyl; // pRef "thinks" it is a Point cout << "\nCylinder printed as a Point is: " << pRef << "\n\n"; // display the Cylinder as a Circle Circle &circleRef = cyl; // circleRef thinks it is a Circle cout << "Cylinder printed as a Circle is:\n" << circleRef << "\nArea: " << circleRef.area() << endl; return 0;}

Esempi Ereditarietà20 X coordinate is 12 Y coordinate is 23 Radius is 2.5 Height is 5.7 The new location, radius, and height of cyl are: Center = [2, 2]; Radius = 4.25; Height = Cylinder printed as a point is: [2, 2] Cylinder printed as a Circle is: Center = [2, 2]; Radius = 4.25 Area = 56.74