Programming Techniques Course

Slides:



Advertisements
Similar presentations
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Advertisements

Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
1 class Rectangle{ private: int numVertices; float *xCoord, *yCoord; public: void set(float *x, float *y, int nV); float area(); }; Inheritance Concept.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance and Composition (aka containment) Textbook Chapter –
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
1 Another Way to Define A Class - Inheritance. 2 Inheritance Concept Rectangle Triangle Polygon class Polygon { private: int width, length; public: void.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
1 Chapter 6 Object-Oriented Software Development.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Inheritence ( הורשה Inheritence ( הורשה ( השקפים מבוססים על שקפים של אליהו ברוטמן ומרינה.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
1 Chapter 14-1 Object- Oriented Software Development Dale/Weems.
1 Chapter 14-2 Object- Oriented Software Development Dale/Weems.
Chapter 12: Adding Functionality to Your Classes.
CMSC 202 Inheritance.
1 Chapter 14 Object-Oriented Software Development.
1 Chapter 14 Object- Oriented Software Development Dale/Weems.
1 Chapter 14 Object-Oriented Software Development Dale/Weems/Headington.
1 Lecture OOP Course Inheritance Basics Shape.
1 Chapter 14 Object-Oriented Software Development Dale/Weems.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
Inheritance One of the most powerful features of C++
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
An Equal Opportunity University CS Fall 2014 Lecture 11 Class Review, Inheritance, Polymorphism Ismail Abumuhfouz Slide modified from Link 1 Link.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Chapter 10 Inheritance and Polymorphism
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 12: Inheritance and Composition.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
10/18/2011ecs40 fall Software Development & Object- Oriented Programming ecs40 Fall 2012: Software Development & Object- Oriented Programming #02:
Chapter 2 Object-Oriented Design and C++ Dr. Youssef Harrath
1 Another way to define a class Inheritance..!!. 2 Why Inheritance ? Inheritance is a mechanism for building class types from existing class types defining.
1 Chapter 4: Another way to define a class Inheritance..!!
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Concept Polygon Rectangle Triangle class Rectangle{
Inheritance Saras M. Srivastava LEARN BY EXAMPLES PGT – Comp. Sc.
Inheritance CMSC 202, Version 4/02.
Ch 10- Advanced Object-Oriented Programming Features
03/10/14 Inheritance-2.
Polymorphism.
Review: Two Programming Paradigms
About the Presentations
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Understanding Inheritance
Inheritance Basics Programming with Inheritance
Comp 249 Programming Methodology
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Computer Programming with JAVA
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
COP 3330 Object-oriented Programming in C++
Chapter 14 Object-Oriented Software Development
C++ Programming CLASS This pointer Static Class Friend Class
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Another way to define a class
Inheritance in C++ Inheritance Protected Section
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Lecture 6: Polymorphism
Chapter 7 Inheritance.
Presentation transcript:

Programming Techniques Course Version 1.0 General comments regarding this course: Many slides have speaker notes. They include: additional information, exercises “motivation”, didactical suggestions, references to relevant code projects, etc. It is recommended to go over the speaker notes along with the slides as part of the course teaching preparation. Beside slides folder (printable) the following folders exist: Source – all code projects correspond the code sections in the slides and more – see in comment 3. Students get this folder on their diskette. Solutions – solutions for all course’ exercises. Recommended to provide them the solutions on R incrementally as the course advanced (when the exercise’ solution is presented). ForExercise – Some exercise can be based on previous projects to enable the students concentrate in the real point of the exercise. They are provided in this chapter. You can find the code projects relevant to each chapter under the chapter’s directory in the source folder. The code directory is sometimes split into few workspaces with significant names according to the topics in the chapter. Most of the code there appears in the slides as well however, it might be useful to use “real life” demonstrations. Though usually there are references from the speaker notes to the relevant projects it is recommended to go over projects of each chapter before teaching it so you can be sure that you are aware of all existing projects and find out which of them can help you. Appendices: Appendix A includes advanced topics which can be useful for high level students or as answers to advanced questions. Refer there to understand the structure of that appendix. Appendix B includes bibliography as well as some relevant references in the internet. Good Luck!

Chapter 7 – Inheritance Basics Version 1.0 General comments regarding this course: Many slides have speaker notes. They include: additional information, exercises “motivation”, didactical suggestions, references to relevant code projects, etc. It is recommended to go over the speaker notes along with the slides as part of the course teaching preparation. Beside slides folder (printable) the following folders exist: Source – all code projects correspond the code sections in the slides and more – see in comment 3. Students get this folder on their diskette. Solutions – solutions for all course’ exercises. Recommended to provide them the solutions on R incrementally as the course advanced (when the exercise’ solution is presented). ForExercise – Some exercise can be based on previous projects to enable the students concentrate in the real point of the exercise. They are provided in this chapter. You can find the code projects relevant to each chapter under the chapter’s directory in the source folder. The code directory is sometimes split into few workspaces with significant names according to the topics in the chapter. Most of the code there appears in the slides as well however, it might be useful to use “real life” demonstrations. Though usually there are references from the speaker notes to the relevant projects it is recommended to go over projects of each chapter before teaching it so you can be sure that you are aware of all existing projects and find out which of them can help you. Appendices: Appendix A includes advanced topics which can be useful for high level students or as answers to advanced questions. Refer there to understand the structure of that appendix. Appendix B includes bibliography as well as some relevant references in the internet. Good Luck!

What is Inheritance A worker is a person It inherits person’s characteristics And may add some of its own How shall we model this? Class person These are the objectives of a methodology that claim to be able to meet current software applications’ challenges. Fulfilling these objectives will enable coping with complex and scaleable projects in all their stages: analyze, design, development, manage and maintain. All the project’s design and modeling points improves project’s construction, management and maintainability. Class worker

Inheritance Concept Point Circle 3D-Point Augmenting the original class Specializing the original class Shape Point Rectangle Triangle Circle 3D-Point real imag ComplexNumber RealNumber ImaginaryNumber imag real

What is it used for? Code reuse The same code is used several times and not copied Polymorphism Similar reference to objects from different classes These are the objectives of a methodology that claim to be able to meet current software applications’ challenges. Fulfilling these objectives will enable coping with complex and scaleable projects in all their stages: analyze, design, development, manage and maintain. All the project’s design and modeling points improves project’s construction, management and maintainability.

Define a Class Hierarchy Syntax: class DerivedClassName : access-level BaseClassName where access-level specifies the type of derivation private by default, or public Any class can serve as a base class Thus a derived class can also be a base class

What to inherit? In principle, every member of a base class is inherited by a derived class just with different access permission

Access Control Over the Members Two levels of access control over class members class definition inheritance type class Point{ protected: int x, y; public: void set(int a, int b); } class Circle : public Point{ … … }

Access Rights of Derived Classes Type of Inheritance private protected public Access Control for Members The type of inheritance defines the minimum access level for the members of derived class that are inherited from the base class With public inheritance, the derived class follow the same access permission as in the base class With protected inheritance, only the public members inherited from the base class can be accessed in the derived class as protected members With private inheritance (default mode), none of the members of base class is accessible by the derived class

Class Derivation mother daughter son class daughter : public mother{ private: double a; public: void foo ( ); } mother daughter son class mother{ protected: int x, y; public: void set(int a, int b); private: int z; } void daughter :: foo ( ){ x = y = 20; set(5, 10); cout<<“value of a ”<<a<<endl; z = 100; // error, a private member } daughter can access 3 of the 4 inherited members

Class Derivation mother daughter son class son : protected mother{ private: double b; public: void foo ( ); } mother daughter son class mother{ protected: int x, y; public: void set(int a, int b); private: int z; } void son :: foo ( ){ x = y = 20; // error, not a public member set(5, 10); cout<<“value of b ”<<b<<endl; z = 100; // error, not a public member } son can access only 1 of the 4 inherited member

What to inherit? In principle, every member of a base class is inherited by a derived class just with different access permission However, there are exceptions for constructor and destructor operator=() member friends Since all these functions are class-specific

Constructor Rules for Derived Classes The default constructor and the destructor of the base class are always called when a new object of a derived class is created or destroyed. class A { public: A ( ) {cout<< “A:default”<<endl;} A (int a) {cout<<“A:parameter”<<endl;} } class B : public A { public: B (int a) {cout<<“B”<<endl;} } output: A:default B B test(1);

Constructor Rules for Derived Classes You can also specify a constructor of the base class other than the default constructor DerivedClassCon ( derivedClass args ) : BaseClassCon ( baseClass args ) { DerivedClass constructor body } class A { public: A ( ) {cout<< “A:default”<<endl;} A (int a) {cout<<“A:parameter”<<endl;} } class C : public A { public: C (int a) : A(a) {cout<<“C”<<endl;} } output: A:parameter C C test(1);

Define its Own Members Point Circle class Point{ protected: int x, y; public: void set(int a, int b); } The derived class can also define its own members, in addition to members inherited from the base class x y Point x y r Circle protected: int x, y; private: double r; public: void set(int a, int b); void set_r(double c); class Circle : public Point{ private: double r; public: void set_r(double c); }

Even more … A derived class can override methods defined in its parent class. With overriding, the method in the subclass has the identical signature to the method in the base class. a subclass implements its own version of a base class method. class A { protected: int x, y; public: void print () {cout<<“From A”<<endl;} } class B : public A { public: void print () {cout<<“From B”<<endl;} }

Access a Method class Point { protected: class Circle : public Point{ int x, y; public: void set(int a, int b) {x=a; y=b;} void foo (); void print(); } class Circle : public Point{ private: double r; public: void set (int a, int b, double c) { Point :: set(a, b); //same name function call r = c; } void print(); Circle c; c.set(10,10,100); // from class Circle c.foo (); // from base class Point c.print(); // from class Circle Point a; a.set(30,50); // from base class Point a.print(); // from base class Point

Putting Them Together Time ExtTime Time is the base class ExtTime is the derived class with public inheritance The derived class can inherit all members from the base class, except the constructor access all public and protected members of the base class define its private data member provide its own constructor define its public member functions override functions inherited from the base class Time ExtTime

class Time Specification // SPECIFICATION FILE ( time.h) class Time { public : void Set ( int h, int m, int s ) ; void Increment ( ) ; void Write ( ) const ; Time ( int initH, int initM, int initS ) ; // constructor Time ( ) ; // default constructor protected : int hrs ; int mins ; int secs ; } ;

Class Interface Diagram Time class Set Protected data: hrs mins secs Increment Write Time Time

Derived Class ExtTime #include “time.h” // SPECIFICATION FILE ( exttime.h) #include “time.h” enum ZoneType {EST, CST, MST, PST, EDT, CDT, MDT, PDT } ; class ExtTime : public Time // Time is the base class and use public inheritance { public : void Set ( int h, int m, int s, ZoneType timeZone ) ; void Write ( ) const; //overridden ExtTime (int initH, int initM, int initS, ZoneType initZone ) ; ExtTime (); // default constructor private : ZoneType zone ; // added data member } ;

Class Interface Diagram ExtTime class Set Set Protected data: hrs mins secs Increment Increment Write Write ExtTime Time ExtTime Time Private data: zone

Implementation of ExtTime Default Constructor ExtTime :: ExtTime ( ) { zone = EST ; } ExtTime et1; et1 hrs = 0 mins = 0 secs = 0 zone = EST The default constructor of base class, Time(), is automatically called, when an ExtTime object is created.

Implementation of ExtTime Another Constructor ExtTime :: ExtTime (int initH, int initM, int initS, ZoneType initZone) : Time (initH, initM, initS) // constructor initializer { zone = initZone ; } ExtTime *et2 = new ExtTime(8,30,0,EST); hrs = 8 mins = 30 secs = 0 zone = EST et2 5000 ??? 6000

Implementation of ExtTime void ExtTime :: Set (int h, int m, int s, ZoneType timeZone) { Time :: Set (hours, minutes, seconds); // same name function call zone = timeZone ; } void ExtTime :: Write ( ) const // function overriding { string zoneString[8] = {“EST”, “CST”, MST”, “PST”, “EDT”, “CDT”, “MDT”, “PDT”} ; Time :: Write ( ) ; cout <<‘ ‘<<zoneString[zone]<<endl; }

Working with ExtTime #include “exttime.h” … … int main() { ExtTime thisTime ( 8, 35, 0, PST ) ; ExtTime thatTime ; // default constructor called thatTime.Write( ) ; // outputs 00:00:00 EST thatTime.Set (16, 49, 23, CDT) ; thatTime.Write( ) ; // outputs 16:49:23 CDT thisTime.Increment ( ) ; thisTime.Write ( ) ; // outputs 08:35:02 PST }