1 CSC241: Object Oriented Programming Lecture No 16.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
1 CSC241: Object Oriented Programming Lecture No 21.
Introduction to Programming Lecture 39. Copy Constructor.
1 CSC241: Object Oriented Programming Lecture No 28.
Class Relationships. In systems with multiple classes, it can become difficult to keep track of relationships  e.g. the Student class requires the Course.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
Association & Aggregation Lecture-9. Vehicle Car Tyre Engine Bus Passenger.
Object Oriented Software Development
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
1 CSC241: Object Oriented Programming Lecture No 13.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
1 Object-Oriented Programming Using C++ CLASS 11 Honors.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
10/21/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01b Title: C++ as O-O Prog Lang (Review) Reference: COS240 Syllabus.
1 CSC241: Object Oriented Programming Lecture No 12.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Domain Modeling Part2: Domain Class Diagram Chapter 4 pp part 2 1.
1 CSC241: Object Oriented Programming Lecture No 06.
1 CSC241: Object Oriented Programming Lecture No 22.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Object Oriented Software Development
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Domain Modeling Yonglei Tao.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
1 CSC241: Object Oriented Programming Lecture No 02.
1 CSC241: Object Oriented Programming Lecture No 11.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
1 CSC241: Object Oriented Programming Lecture No 05.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
OO in Context Lecture 13: Dolores Zage. Confused about OO Not alone, there is much confusion about OO many programs are claimed to be OO but are not really.
1 CSC241: Object Oriented Programming Lecture No 03.
1 CSC241: Object Oriented Programming Lecture No 17.
Programming Techniques Classes II Important Class Features Spring 2009.
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 in C++ Chapter 7 Dynamic Binding.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
A First Book of C++ Chapter 12 Extending Your Classes.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Visit for more Learning Resources
CSC241: Object Oriented Programming
Overloaded Constructors and Multiple Classes
CSC241: Object Oriented Programming
Institute of Business & Technology (BIZTEK)
CSC241: Object Oriented Programming
CSC241: Object Oriented Programming
Object-Oriented Programming Using C++
Templates.
Software Engineering System Modeling Extra examples Dr.Doaa Sami
Object Oriented Programming Using C++
Constructors and Destructors
CPS120: Introduction to Computer Science
Object-Oriented Programming (Part 2)
Constructor, Destructor Mehroz Sadiq CS Faculty, UMT Department of CS&E, UET.
Structures Structured Data types Data abstraction structs ---
Objects as Function Arguments
Functions Chapter No. 5.
Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 16

2 Previous lecture Abstract class Public and private Inheritance – Difference come when creating objects of derived class Level of inheritance – Object of class C can access public member of class B and A class A { }; class B : public A { }; class C : public B { }; class A { }; class B : public A { }; class C : public B { };

3 Today’s Lecture Multiple inheritance – Example program: employ and student class Constructor in multiple inheritance – Example program Aggregation Composition

4 Multiple inheritance A class can be derived from more than one base class. This is called multiple inheritance class A { }; class B { }; class C : public A, public B{ };

5 Cont. Suppose – we need to record the educational experience of some of the employees in the EMPLOY program – In a different project, we’ve already developed a class called student that models students with different educational backgrounds Instead of modifying the employee class to incorporate educational data, we will add this data by multiple inheritance from the student class The student class stores the name of the school or university last attended and the highest degree received Educational information is not relevant to every class of employee

6

7 Miniprogram showing relationships class student { }; class employee { }; class manager : private employee, private student { }; class scientist : private employee, private student { }; class laborer : public employee { };

8 const int LEN = 80; class employee{ private: char name[LEN]; unsigned long number; public: void getdata(){ cout << “\n Enter last name: “; cin >> name; cout << “ Enter number: “; cin >> number; } void putdata() const { cout << “\n Name: “ << name; cout << “\n Number: “ << number; } }; const int LEN = 80; class student { private: char school[LEN]; char degree[LEN]; public: void getedu() { cout << “ Name of school : “; cin >> school; cout << “Highest degree : ”; cin >> degree; } void putedu() const { cout << “School: “ << school; cout << “Degree earned: “ << degree; } };

9 class manager : private employee, private student { private: char title[LEN]; double dues; public: void getdata() { employee::getdata(); cout > title; cout > dues; student::getedu(); } void putdata() const { employee::putdata(); cout << “\n Title: “ << title; cout << “\n Golf club dues: “ << dues; student::putedu(); } };

10 class scientist : private employee, private student { private: int pubs; public: void getdata() { employee::getdata(); cout > pubs; student::getedu(); } void putdata() const { employee::putdata(); cout << “\n Number of publications: “ << pubs; student::putedu(); } }; class laborer : public employee { };

11 main(){ manager m1; scientist s1, s2; laborer l1; cout << “\nEnter data: manager 1”; m1.getdata(); cout << “\nEnter data : scientist 1”; s1.getdata(); cout << “\nEnter data : laborer 1”; l1.getdata(); cout << “\nData on manager 1”; m1.putdata(); cout << “\nData on scientist 1”; s1.putdata(); cout << “\nData on laborer 1”; l1.putdata(); } Program Output Enter data manager 1 Enter last name: Bradley Enter number: 12 Enter title: Vice-President Enter golf club dues: Enter name of school : Yale Enter degree earned: MS Enter data for scientist 1 Enter last name: Twilling Enter number: 764 Enter number of pubs: 12 Enter name of school : MIT Enter degree earned PhD Enter data for laborer 1 Enter last name: Jones Enter number: Go to program

12 Constructors in Multiple Inheritance Suppose we’re writing a program for building contractors, and that models lumber-supply items. A class that represents a quantity of lumber of a certain type: e.g foot-long construction grade 2.4s

13 Lumber class Class store various kinds of data about each such lumber item. E.g. – Length – Number of such pieces of lumber – Unit cost – Description of the lumber: dimensions of cross-section of lumber – Grade of lumber: rough-cut, construction grade, surface- four-side

14 Cont. It convenient to create a Type class to hold – Description of the lumber (Dimensions) e.g. 2x6 – Grade of lumber e.g. construction class Type { private: string dimensions; string grade; public: Type() : dimensions(“N/A”), grade(“N/A”) { } Type(string di, string gr) : dimensions(di), grade(gr) { } void gettype() { cout << “ Enter dimension”; cin >> dimensions;” cout << “ Enter grade “; cin >> grade; } void showtype() const { cout << dimensions <<“:”grade; } };

15 class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) { } void getdist() { cout > feet; cout > inches; } void showdist() const { cout << feet << “ : ” << inches ; } };

16 class Lumber : public Type, public Distance { private: int quantity; double price; public: Lumber() : Type(), Distance(), quantity(0), price(0.0) { } Lumber( string di, string gr, int ft, float in, int qu, float prc ) :Type(di, gr), Distance(ft, in), quantity(qu), price(prc) { } void getlumber() { Type::gettype(); Distance::getdist(); cout > quantity; cout > price; } void showlumber() const { Type::showtype(); cout << “\n Length: “; Distance::showdist(); cout << “\n Price for “ << quantity << “$” << price * quantity; } }; Go to program

17 Ambiguity in Multiple Inheritance Two base class may have same function names, while derived class has no function with that name class A { public: void show() { cout << “Class A”; } }; class B{ public: void show() { cout <<“Class B”;} }; class C : public A, public B { }; main() { C objC; objC.show(); objCA::show(); objCB::show(); } Both B and C contain a copy of func(), inherited from A. The compiler can’t decide which copy to use, and signals an error

18 Cont. class A { public: void show() { cout << “Class A”; } }; class B : public A { }; class C : public A { }; class D : public B, public C { }; main() { D objD; objD.show(); } class A show() class A show() class B show() class B show() class C show() class C show() class D show() class D show()

19 Aggregation and Composition Inheritance gives us an 'is-a' relationship. – B is a kind of A. This is because B has all the characteristics of A and in addition some of its own Aggregation gives us a 'has-a' relationship – Aggregation would make sense in this situation, a Customer 'has-a' Address.

20 Aggregation: Classes Within Classes E.g. an invoice has an item line, Aggregation is also called a “part-whole” relationship In Object oriented programming, aggregation occur when one object is an attribute of another class A{ }; class B{ A objA; };

21 Cont. A composition hierarchy defines how an object is composed of other objects in a fixed relationship. Aggregate object cannot exist without its components an object of a container class which is able to contain other objects. Its existence is independent of whether it actually contains anything. Contained objects will probably be dynamic and vary over time. E.g. a car boot is a container which is able to contain some or no objects

22 UML class diagram showing aggregation

23 Aggregation in the EMPLOY Program class student { }; class employee { }; class manager { student stu; employee emp; }; class scientist { student stu; employee emp; }; class laborer { employee emp; }; Go to program