Before Introducing Inheritance … Here is another way to re-use what have been developed: This is known as the object composition! A real-world example.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Copyright © 2003 Pearson Education, Inc. Slide 1.
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Inheritance Juan Marquez 12_inheritance.ppt
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
C++ Inheritance Systems Programming.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Inheritance.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
CS31: Introduction to Computer Science I Discussion 1A 5/28/2010 Sungwon Yang
Inheritance, Polymorphism, and Virtual Functions
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Chapter 15 Inheritance. Slide Overview 15.1 Inheritance Basics 15.2 Inheritance Details 15.3 Polymorphism.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
1 Object-Oriented Programming Using C++ CLASS 11 Honors.
Polymorphism and Virtual Functions. Motivation Polymorphism is one of the fundamental mechanisms offered by OOP, and it is directly related to inheritance.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Inheritance One of the most powerful features of C++
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
Object Oriented Programming in C++ Chapter 6 Inheritance.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Review of Last Lecture. What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Inheritance and Composition Reusing the code and functionality Unit - 04.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
CSIS 123A Lecture 10 Inheritance. Organizing Code Why should programs be organized? –Humans are "complexity challenged" Need simplicity, clarity, efficiency.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
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.
Learners Support Publications Constructors and Destructors.
©Copyrights 2016 Eom, Hyeonsang All Rights Reserved Computer Programming Object Oriented Programming & C++ 1 st Lecture 엄현상 (Eom, Hyeonsang) Department.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
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.
Constructors and Destructors
COMP 2710 Software Construction Inheritance
Inheritance What is inheritance?
Polymorphism & Virtual Functions
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Learning Objectives Inheritance Virtual Function.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Inheritance, Polymorphism, and Virtual Functions
Constructors and Destructors
Object-Oriented Programming (OOP) Lecture No. 22
Introduction to Classes and Objects
Inheritance -I.
Lecture 10 Concepts of Programming Languages
Chapter 7 Inheritance.
Presentation transcript:

Before Introducing Inheritance … Here is another way to re-use what have been developed: This is known as the object composition! A real-world example of composition may be seen in the relation of an automobile to its parts.

How to generate complex object type from a number of basic types? This is known as the object composition! class Point { private: float x, y; }; class Edge { private: Point vertex1, vertex2 }; class Triangle{ public: private: Point vertex1, vertex2, vertex3; Edge edge1, edge2, edge3; };

How to initialize the composite objects? This is known as the object composition! class Point { private: float x, y; }; class Edge { public: Edge (const Point &p1, const Point &p2) : vertex1(p1), vertex2(p2) {…} private: Point vertex1, vertex2; }; class Triangle{ …… };

What if the composite class is defined as follows? class Point { private: float x, y; }; class Edge { private: Point vertex1, vertex2; }; class Polygon{ public: private: Point *vertices; Edge *edges; int nvertices, nedges; };

Inheritance and Derived Classes

Why do we need it? – It is one of the most powerful mechanisms offered by OOP. – It enables potentially infinite reuse of resources. – It can be used to enhance and improve the previous programs for the changing requirements. – It also matches what we know about the real- world.

An Example (from textbook) Consider the employees of a company. All of them have some common attributes, like SSN, name, employeeID, netPay, etc. But they may be paid differently. Some are paid hourly, while the others may be paid with a fixed wage (weekly/monthly) If we use two different classes to represent these two groups of employees who are paid differently, each of these classes will need to define those common attributes listed above. This is not very efficient in terms of coding. This situation can be more dramatic in the real cases of software development.

An Example (from textbook) Consider the employees of a company. All of them have some common attributes, like SSN, name, employeeID, netPay, etc. But they may be paid differently. Some are paid hourly, while the other may be paid with a fixed wage (weekly/monthly) To address that, we use the inheritance!

An Example (from textbook) #include using std::string; class Employee { public: // constructor list // member functions private: string name; string ssn; double netPay; }; We first define a class to implement the common properties and behaviors of all employees.

An Example (from textbook) #include using std::string; class Employee { public: // constructor list // member functions private: string name; string ssn; double netPay; }; Next, we define a new class to *inherit* these common attributes and behaviors. class HourlyEmployee : public Employee { public: // constructor list // new member functions private: double wageRate; double hours; };

An Example (from textbook) #include using std::string; class Employee { public: // constructor list // member functions private: string name; string ssn; double netPay; }; Next, we define a new class to *inherit* these common attributes and behaviors. Base class or parent class derived class or child class class HourlyEmployee : public Employee { public: // constructor list // new member functions private: double wageRate; double hours; }; In other words, we create a new class *based on* an existing class

How to use inheritance From this example, we see the inheritance is achieved in the following format class DerivedClass : public BaseClass { }; From this example, we can also learn that the base class typically provides a *general* definition (for all objects), while the individual child classes provide more *specific* definition (sub-sets of the objects).

How to use inheritance class DerivedClass : public BaseClass { }; This inheritance can be carried on during the development of the program, and eventually we obtain an inheritance relation of the classes much similar to a family tree. A BC D E …… To represent this inheritance relation between classes and other relations including friend relationship, we will use UML!

What benefits can inheritance provide? Derived class *inherits* all the member variables and functions except the constructors of the base class. So what do you think is the benefit here? Derived class can define its own and new members that are for its specific purpose! AB

More on Inheritance Derived class can *overwrite (or re-define)* the inherited member functions from the base class. This will NOT affect the previous definition in the base class. Derived class can NOT change the types of the inherited member variables. Again, derived class does NOT inherit the private members of the base class and its constructors. class DerivedClass : public BaseClass {} class DerivedClass : private BaseClass {} We typically use public access to inherit base class. “private” or “protected” inheritance is possible, but seldom used. All members of the base class are now private to the derived class!

CORRECTION! Derived class *inherits* all the public member variables and functions except the constructors of the base class. Derived class DOES NOT inherits all the private member variables and functions of the base class. So what do you think is the benefit here? Derived class can define its own and new members that are for its specific purpose! Correction: the memory allocated for the derived object contains everything from the base class including its private members! But you cannot directly access that! AB

CORRECTION! Derived class *inherits* all the member variables and functions except the constructors and destructor of the base class. So what do you think is the benefit here? Derived class can define its own and new members that are for its specific purpose! Correction: the memory allocated for the derived object contains everything from the base class including its private members! But you cannot directly access that! AB Derived class "inherits" private member variables - But still cannot directly access them, not even in the member function of the derived class.

class my_emp{ public: my_emp() { name = NULL; age = 0; salary = 0.0; } my_emp(char *in_name, int in_age, float in_salary):salary(in_salary), age(in_age) { name = new char[strlen(in_name)+1]; strcpy(name, in_name); } ~my_emp() { delete [] name; } void print_name() const { cout << "This is " << name << endl;} int get_age () const { return age; } float get_salary() const { return salary; } private: char *name; int age; float salary; }; class manager : public my_emp { public: manager(char *in_name, short in_age, float in_salary, int in_level) : my_emp(in_name, in_age, in_salary) { level=in_level; } void print_level() { cout << "level: " << level << endl; cout<<“age: "<<age<<endl; //ILLEGAL! } private: int level; }; void main() { manager m ("Chen", 0, 0, 1); m.age = 25; //ILLEGAL! m.salary = ; //ILLEGAL! m.print_level(); m.print_name(); }

class my_emp{ public: my_emp() { name = NULL; age = 0; salary = 0.0; } my_emp(char *in_name, int in_age, float in_salary):salary(in_salary), age(in_age) { name = new char[strlen(in_name)+1]; strcpy(name, in_name); } ~my_emp() { delete [] name; } void print_name() const { cout << "This is " << name << endl;} int get_age () const { return age; } float get_salary() const { return salary; } private: char *name; int age; float salary; }; class manager : public my_emp { public: manager(char *in_name, short in_age, float in_salary, int in_level) : my_emp(in_name, in_age, in_salary) { level=in_level; } void print_level() { cout << "level: " << level << endl; } private: int level; }; void main() { manager m ("Chen", 0, 0, 1); m.print_level(); m.print_name(); } What is the output?

class my_emp{ public: my_emp() { name = NULL; age = 0; salary = 0.0; } my_emp(char *in_name, int in_age, float in_salary):salary(in_salary), age(in_age) { name = new char[strlen(in_name)+1]; strcpy(name, in_name); } ~my_emp() { delete [] name; } void print_name() const { cout << "This is " << name << endl;} int get_age () const { return age; } float get_salary() const { return salary; } private: char *name; int age; float salary; }; class manager : public my_emp { public: manager(char *in_name, short in_age, float in_salary, int in_level) : my_emp(in_name, in_age, in_salary) { level=in_level; } void print_level() { cout << "level: " << level << endl; } private: int level; }; void main() { manager m ("Chen", 0, 0, 1); m.print_level(); m.print_name(); } level: 1 This is Chen

class my_emp{ public: my_emp() { name = NULL; age = 0; salary = 0.0; } my_emp(char *in_name, int in_age, float in_salary):salary(in_salary), age(in_age) { name = new char[strlen(in_name)+1]; strcpy(name, in_name); } ~my_emp() { delete [] name; } void print_name() const { cout << "This is " << name << endl;} int get_age () const { return age; } float get_salary() const { return salary; } private: char *name; int age; float salary; }; class manager : private my_emp { public: manager(char *in_name, short in_age, float in_salary, int in_level) : my_emp(in_name, in_age, in_salary) { level=in_level; } void print_level() { cout << "level: " << level << endl; } private: int level; }; void main() { manager m ("Chen", 0, 0, 1); m.print_level(); m.print_name(); } How about now?

class my_emp{ public: my_emp() { name = NULL; age = 0; salary = 0.0; } my_emp(char *in_name, int in_age, float in_salary):salary(in_salary), age(in_age) { name = new char[strlen(in_name)+1]; strcpy(name, in_name); } ~my_emp() { delete [] name; } void print_name() const { cout << "This is " << name << endl;} int get_age () const { return age; } float get_salary() const { return salary; } private: char *name; int age; float salary; }; class manager : private my_emp { public: manager(char *in_name, short in_age, float in_salary, int in_level) : my_emp(in_name, in_age, in_salary) { level=in_level; } void print_level() { cout << "level: " << level << endl; } private: int level; }; void main() { manager m ("Chen", 0, 0, 1); m.print_level(); m.print_name(); //ILLEGAL! } How about now?

class my_emp{ public: my_emp() { name = NULL; age = 0; salary = 0.0; } my_emp(char *in_name, int in_age, float in_salary):salary(in_salary), age(in_age) { name = new char[strlen(in_name)+1]; strcpy(name, in_name); } ~my_emp() { delete [] name; } void print_name() const { cout << "This is " << name << endl;} int get_age () const { return age; } float get_salary() const { return salary; } private: char *name; int age; float salary; }; class manager : private my_emp { public: manager(char *in_name, short in_age, float in_salary, int in_level) : my_emp(in_name, in_age, in_salary) { level=in_level; } void print_level() { cout << "level: " << level << endl; pirnt_name(); // Call it here! } private: int level; }; void main() { manager m ("Chen", 0, 0, 1); m.print_level(); } To get the same output

The protected: Qualifier New classification of class members Allows access "by name" in derived class – But nowhere else – Still no access "by name" in other classes In class it’s defined  acts like private Considered "protected" in derived class – To allow future derivations

class my_emp{ public: my_emp() { name = NULL; age = 0; salary = 0.0; } my_emp(char *in_name, int in_age, float in_salary):salary(in_salary), age(in_age) { name = new char[strlen(in_name)+1]; strcpy(name, in_name); } ~my_emp() { delete [] name; } void print_name() const { cout << "This is " << name << endl;} int get_age () const { return age; } float get_salary() const { return salary; } protected: char *name; int age; float salary; }; class manager : public my_emp { public: manager(char *in_name, short in_age, float in_salary, int in_level) : my_emp(in_name, in_age, in_salary) { level=in_level; } void print_level() { cout << "level: " << level << endl; cout << “age: " << age << endl; //OK } private: int level; }; void main() { manager m ("Chen", 0, 0, 1); m.age = 25; //ILLEGAL! m.salary = ; //ILLEGAL! m.print_level(); m.print_name(); }

Only member functions of the same class private protected Member functions of the same class and the derived class public Every one !

Constructor of Derived Class class DerivedClass : public BaseClass {… public: DerivedClass (argument list) : BaseClass (argument list) { } … }; class DerivedClass : public BaseClass {… public: DerivedClass (argument list) { } … }; Or In the initialization section

class my_emp{ public: my_emp() { name = NULL; age = 0; salary = 0.0; } my_emp(char *in_name, int in_age, float in_salary):salary(in_salary), age(in_age) { name = new char[strlen(in_name)+1]; strcpy(name, in_name); } ~my_emp() { delete [] name; } void print_name() const { cout << "This is " << name << endl;} int get_age () const { return age; } float get_salary() const { return salary; } protected: char *name; int age; float salary; }; class manager : public my_emp { public: manager(char *in_name, short in_age, float in_salary, int in_level) : my_emp(in_name, in_age, in_salary) { level=in_level; } void print_level() { cout << "level: " << level << endl; cout << “age: " << age << endl; //OK } private: int level; }; void main() { manager m ("Chen", 0, 0, 1); m.print_level(); m.print_name(); }

The order of constructor calling In general, First, the constructor of the base class Second, the constructor of the object type of member variables Finally, the constructor of the derived class class DerivedClass : public BaseClass {… public: DerivedClass (argument list) : BaseClass (argument list), A (initial value) { } … private: A obj; //A is a pre-defined class! };

class my_emp{ public: my_emp() { name = NULL; age = 0; salary = 0.0; } my_emp(char *in_name, int in_age, float in_salary):salary(in_salary), age(in_age) { name = new char[strlen(in_name)+1]; strcpy(name, in_name); } ~my_emp() { delete [] name; } void print_name() const { cout << "This is " << name << endl;} int get_age () const { return age; } float get_salary() const { return salary; } protected: char *name; int age; float salary; }; class manager : public my_emp { public: manager(char *in_name, short in_age, float in_salary, int in_level) : my_emp(in_name, in_age, in_salary), level(in_level) { } void print_level() { cout << "level: " << level << endl; cout << “age: " << age << endl; //OK } private: int level; }; void main() { manager m ("Chen", 0, 0, 1); m.print_level(); m.print_name(); }

The order of the constructor calling and destructor calling for a derived object B C A class A { }; class B : public A { }; class C : public B { }; C obj_c; when create an object of C it will call the constructors in the order of A→B →C when the program (or function) is exiting it will call the destructors in the order of C→B →A

Redefinition class BaseClass { public: … void method1(argument list){//implementation in base class} }; class DerivedClass : public BaseClass {… public: void method1(same argument list!) {//new implementation } void method1(different argument list) {//new implementation } … };

Redefinition How to call the member functions of the base class that are re-defined. class BaseClass { public: … void method1(argument list){//implementation in base class} }; class DerivedClass : public BaseClass {… public: void method1(same argument list!) {//new implementation } … }; DerivedClass obj1; obj1.BaseClass::method1(input parameters);

Multiple Inheritance The new derived class can inherit from more than one base class class A { …… }; class B { …… }; class C : public A, public B { …… }; Like single inheritance, the derived class will automatically inherit all the public members from all its parent classes.

Multiple Inheritance The new derived class can inherit from more than one base class class A { …… }; class B { …… }; class C : public A, public B { …… public: C (argument list) : A(arguments), B(arguments) {…} }; Like single inheritance, the derived class will automatically inherit all the public members from all its parent classes. Be careful about the constructor

Multiple Inheritance Problem of multiple inheritance class A { …… public: int val; }; class C : public A, public B { …… }; C c_obj; c_obj.val=0; // which “val”, from A or from B? Ambiguity of inherited members! class B { …… public: int val; };

Multiple Inheritance Problem of multiple inheritance class A { …… public: int val; }; class C : public A, public B { …… }; C c_obj; c_obj.A::val=0; // legal c_obj.B::val=2; // legal as well Solution – use scope resolution “::” class B { …… public: int val; };

Overall, try to avoid multiple inheritance, as you may not know the ancestors of those base classes. The configuration to the right could happen! A BC D A class A { public: void vf() { cout<<"I come from class A"<<endl;} }; class B: public A{}; class C: public A{}; class D: public B, public C{}; void main() { D d; d.vf ();// error, multiple copies of vf() }

There is indeed one possible solution to address that i.e. using virtual inheritance A BC D A class A { public: void vf() { cout<<"I come from class A"<<endl;} }; class B: virtual public A{}; class C: virtual public A{}; class D: public B, public C{}; void main() { D d; d.vf ();// this OK now }

Another more complicated example class B1:virtual public B,virtual public A{ public: B1(int i){ cout<<"Constructing B1"<<endl; } }; class B2: public A, virtual public B { public: B2(int j){ cout<<"Constructing B2"<<endl; } }; class D: public B1, public B2 { public: D(int m,int n): B1(m),B2(n){ cout<<"Constructing D"<<endl; } A a; }; void main(){ D d(1,2); }