Andy Wang Object Oriented Programming in C++ COP 3330

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance, Polymorphism, and Virtual Functions
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Inheritance Version 1.1. Topics Inheritance Constructors and Inheritance Function Over-riding (Redefinition) The Stringstream class Shadowing Variables.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 13:Object 物件 Oriented 面向 Programming (OOP)
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Inheritance One of the most powerful features of C++
1 Inheritance Chapter 9. 2 What You Will Learn Software reusability (Recycling) Inheriting data members and functions from previously defined classes.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
Chapter 10 Inheritance and Polymorphism
Object-Oriented Programming in C++ More examples of Association.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 5.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
From C to C++. What is C++ C++ is the work of Bjarne Stroustrup of AT&T Bell Labs. C++ is a mostly upward compatible extension of C that provides: A better.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
1 Introduction to Object Oriented Programming Chapter 10.
Topics Instance variables, set and get methods Encapsulation
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
1 CSE 2341 Object Oriented Programming with C++ Note Set #12.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Pointer to an Object Can define a pointer to an object:
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Procedural and Object-Oriented Programming
Access Functions and Friend Functions
Andy Wang Object Oriented Programming in C++ COP 3330
Andy Wang Object Oriented Programming in C++ COP 3330
Inheritance Basics Fall 2008
Review: Two Programming Paradigms
Chapter 5 Classes.
Computing with C# and the .NET Framework
CS1201: Programming Language 2
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
This technique is Called “Divide and Conquer”.
CS 302 Week 11 Jim Williams, PhD.
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
CS1201: Programming Language 2
Learning Objectives Inheritance Virtual Function.
Andy Wang Object Oriented Programming in C++ COP 3330
Inheritance, Polymorphism, and Virtual Functions
Screen output // Definition and use of variables
CS1201: Programming Language 2
CS1201: Programming Language 2
CLASSES AND OBJECTS.
CPS120: Introduction to Computer Science
Introduction to Classes and Objects
CS1201: Programming Language 2
B.FathimaMary Dept. of Commerce CA SJC Trichy-02
Andy Wang Object Oriented Programming in C++ COP 3330
CPS120: Introduction to Computer Science
COP 3330 Object-oriented Programming in C++
Presentation transcript:

Andy Wang Object Oriented Programming in C++ COP 3330 Inheritance Andy Wang Object Oriented Programming in C++ COP 3330

Hierarchical Relationships Many types of classes share similarities Similar private member data Similar member functions Inheritance Factoring out common features into a single base class Build derived classes that will share or inherit all of the data and functionality of the base class Except constructors, destructors, and assignment operators

Inheritance The base class/derived class relationship is an “is a” relationship Derived object is an instance of the base object Usually a subcategory Vs. “has a” relationship An object of one class is embedded inside another class

Examples Base class Derived class GeometricObject Sport BankAccount Vehicle Car Circle, Square, Line Football, Baseball Savings, Checking Car, Train, Bus Honda, Toyota, Ford

Declaring Derived Class Format class derivedClassName : public baseClassName public The base class must be declared somewhere The derived class has the same level as the base class Other protection levels can be ignored for now

Examples class Sport {…}; class Football : public Sport {…}; class Baseball : public Sport {…}; class BankAccount {…}; class Checking : public BankAccount {…}; class Vehicle {…}; class Car : public Vehicle {…}; class Honda : public Car {…};

Note Car inherits everything in the Vehicle class Honda inherits everything in the Car class Thus, Honda inherits everything from the Vehicle class

Example: Drawing Program http://www.cs.fsu.edu/~myers/cop3330/examples/i nher/geombuild/geom1.h

geom1.h class GeometricObject { public: void Draw(); void Erase(); void Move(int h, int v); int top, left, bottom, right; };

geom1.h class Two_D_Object : public GeometricObject { public: int fillPattern; }; class Rectangle : public Two_D_Object { int length, width; }

Protection Levels Public Private Protected Can be accessed by name from anywhere Private Can be accessed directly only by the class in which it is declared Protected Can be accessed by directly by the class in which is declared and by any classes derived from that class

Example: Drawing class GeometricObject { public: GeometricObject(); void Draw(); void Erase(); void Move(int h, int v); protected: // accessible by this and derived classes int top, left, bottom, right private: int x; // inaccessible from outside };

Example: Drawing class Two_D_Object : public GeometricObject { public: Two_D_Object(); private: int fill Pattern; };

Example: Drawing class Rectangle : public Two_D_Object { public: Rectangle(); Rectangle(int t, int l, int b, int r, int f, int len, int wid); void FindCenter(int &, int &); void Draw(); double Area(); int Perimeter(); private: int length, width; };

Example: Drawing class Circle : public Two_D_Object { public: Circle(); Circle(int t, int l, int b, int r, int f, int cx, int cy, int rad); void FindCenter(int &, int &); void Draw(); double Area(); double Circumference(); private: int center_x, center_y, radius; };

Constructors in Derived Classes A derived object “is an” instance of the base class Thus, when a derived object is created, the constructors from the base and derived classes will run The base class will run first, then the derived

Examples Vehicle obj1; Car obj2; Honda obj3; Only the Vehicle() constructor runs Car obj2; Vehicle constructor runs, followed by the Car() constructor Honda obj3; Vehicle(), Car(), Honda()

Note Destructors will be invoked in the reverse order ~Honda(), ~Car(), ~Vehicle()

Examples http://www.cs.fsu.edu/~myers/cop3330/examples/i nher/sample.cpp

Inheritance #include <iostream> using namespace std; class A { public: A() { cout << “A()” << endl; } ~A() { cout << “~A” << endl; } };

Inheritance class B : public A { public: B() { cout << “B()” << endl; } ~B() { cout << “~B()” << endl; } }; class C : public B { C() { cout << “C()” << endl; } ~C() { cout << “~C()” << endl; }

Inheritance int main() { cout << “Declaring object of type A” << endl; A aobject; { cout << “Declaring object of type B” << endl; B bobject; cout << Declaring object of type C” << endl; C cobject;

Inheritance cout << “Object C now going out of scope” << endl; } cout << “Object B now going out of scope” << endl; cout << “Object A now going out of scope” << endl; return 0;

Constructors with Parameters Without parameters, the default constructors are called With a derived class, we might want to declare Honda h(2, 3, 4, “green”, “Accord”); 2 and 3 might be for variables like vehicleCategory and idNumber in the Vehicle Class 4 and “green” might be for variables like numDoors and carColor in the Car Class “Accord” might be for a variable model in the Honda class

How to Distribute the Parameters Use an initialization list function prototype : initialization list { function body } Use the initialization list to call the next higher constructor explicitly, in order to send the parameters to the parent constructor

Example Vehicle::Vehicle(int c, int id) { vehicleCategory = c; idNumber = id; } Car::Car(int c, int id, int nd, char *cc) : Vehicle(c, id) { numDoors = nd; strcpy(carColor, cc); Honda::Honda(int c, int id, int nd, char *cc, char *mod) : Car(c, id, nd, cc) { strcpy(model, mod);

Drawing Program Example http://www.cs.fsu.edu/~myers/cop3330/notes/inhe r1.html

geom.h class GeometriObject { public: GeometricObject(); GeometricObject(int t, int l, int b, int r); void Draw(); … protected: int top, left, bottom, right; };

geom.h class Two_D_Object : public GeometricObject { public: Two_D_Object(); Two_D_Object(int t, int l, int b, int r, int fill); … protected: int fillpattern; };

geom.h class Rectangle : public Two_D_Object { public: Rectangle(); Rectangle(int t, int l, int b, int r, int f, int len, int wid); … private: int length, width; };

geom.h class Circle : public Two_D_Object { public: Circle(); Circle(int , int l, int b, int r, int f, int cx, int cy, int rad); … private: int center_x, center_y, radius; };

geom.cpp #include <iostream> #include “geom.h” using namespace std; GeometricObject::GeometricObject() { cout << “Running GeometricObject default constructor\n”; top = left = bottom = right = 0; }

geom.cpp GeometricObject::GeometricObject(int t, int l, int b, int r) { cout << “Running GeometricObject constructor with parameters\n”; top = t; left = l; bottom = b; right = r; }

geom.cpp Two_D_Object::Two_D_Object() { cout << “Running Two_D_Object default constructor\n”; fillPattern = 0; } Two_D_Object::Two_D_Object(int t, int l, int b, int r, int fill) : GeometricObject(t, l, b, r) { cout << “Running Two_D_Object constructor with parameters\n”; fillPattern = fill;

geom.cpp Rectangle::Rectangle() { cout << “Running Rectangle default constructor\n”; length = width = 1; }

geom.cpp Rectangle::Rectangle(int , int l, int b, int r, int f, int len, int wid) : Two_D_Object(t, l, b, r, f) { cout << “Running Rectangle constructor with parameters\n”; length = len; width = wid; }

geom.cpp Circle::Circle() { radius = center_x = center_y = 1; } Circle::Circle(int t, int l, int b, int r, int f, int cx, int cy, int rad) : Two_D_Object(t, l, b, r, f) { center_x = cx; center_y = cy; radius = rad;

main.cpp #include <iostream> #include “geom.h” using namespace std; int main() { cout << “Rectangle r\n”; Rectangle r; cout << “Rectangle r1(1, 2, 3, 4, 5, 10, 20)\n”; Rectangle r1(1, 2, 3, 4, 5, 10, 20); return 0; }

Function Overriding Suppose we have the following base class class Student { public: void GradeReport(); … }; We also have the following derived classes class Grad : public Student class undergrad : public Student

Function Overriding Since Grad and Undergrad are derived from Student, they inherit everything from Student But suppose grade reports look different for undergrads and grads These classes need to have their own functions But using the exact same prototype

Function Overriding class Grad : public Student { public: void GradeReport(); … }; class Undergrad : public Student {

To Call the Parent Version void Student::GradeReport() { // processing done by parent function } void Grade::GradeReport() { // explicit call to parent function Student::GradeReport(); // other processing specific to Grad’s version

Drawing Example http://www.cs.fsu.edu/~myers/cop3330/examples/i nher/geom2/geom.h

geom.h class GeometricObject { public: … void Draw(); };

geom.h class Two_D_Object : public GeometricObject { public: … void Draw(); }

geom.h class Rectangle : public Two_D_Object { public: … void Draw(); };

geom.h class Circle: public Two_D_Object { public: … void Draw(); };

geom.cpp … void GeometricObject::Draw() { cout << “Running GeometricObject::Draw()\n” } void Two_D_Object::Draw() { cout << “Running Two_D_Object::Draw()\n”; // can call upon the base class version of Draw GeometricObject::Draw(); // and do any processing specific to this class cout << “Finishing Two_D_Object::Draw()\n”;

geom.cpp void Rectangle::Draw() { cout << “Running Rectangle::Draw()\n”; Two_D_Object::Draw(); // do pre-processing // draw rectangle cout << “Finishing Rectangle::Draw()\n”; } …

main.cpp #include <iostream> #include “geom.h” using namespace std; int main() { Rectangle r; … r.Draw(); return 0; }