C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Size: px
Start display at page:

Download "C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved."

Transcription

1 C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

2 Inheritance is a form of software reuse in which you create a class that absorbs an existing class s data and behaviors and enhances them with new capabilities. You can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. A derived class represents a more specialized group of objects. A derived class contains behaviors inherited from its base class and can contain additional behaviors. A derived class can also customize behaviors inherited from the base class by Pearson Education, Inc. All Rights Reserved. 2

3 Base class Student Shape Loan Employee Account Derived classes GraduateStudent UndergraduateStudent Circle Triangle Rectangle CarLoan HomeImprovementLoan MortgageLoan FacultyMember StaffMember CheckingAccount SavingsAccount 3

4 A GraduadeStudent is a Student An UndergraduateStudent is also a Student GraduadeStudent is a specialization of Student OR Student is a generalization of GraduadeStudent Base class Student Derived classes GraduateStudent UndergraduateStudent 4

5 A direct base class is the base class from which a derived class explicitly inherits. An indirect base class is inherited from two or more levels up in the class hierarchy. In the case of single inheritance, a class is derived from one base class. C++ also supports multiple inheritance, in which a derived class inherits from multiple (possibly unrelated) base classes by Pearson Education, Inc. All Rights Reserved. 5

6 With object-oriented programming, you focus on the commonalities among objects in the system rather than on the special cases. We distinguish between the is-a relationship and the has-a relationship. The is-a relationship represents inheritance. In an is-a relationship, an object of a derived class also can be treated as an object of its base class. By contrast, the has-a relationship represents composition by Pearson Education, Inc. All Rights Reserved. 6

7 How to derive a class from an existing one? class derivedclassname : access baseclassname { } access can be public, protected, or private, or it can be omitted (defaults to private) // Base Class class Base { // data members and methods of class Base... // Derived Class class Derived : public Base { // data members and methods of class Derived... Derived class is derived from Base class publicly. 7

8 // Base Class class Base {... // Derived Class class Derived : public Base {... main() { Base b; Derived c; b = c; // Allowed. c IS-A b. Base *ptr = new Derived(); // Allowed.... } 8

9 With public inheritance, every object of a derived class is also an object of that derived class s base class. However, base-class objects are not objects of their derived classes. main() { Base b; Derived c; b = c; // Allowed. c IS-A b. c = b; // WRONG. b IS NOT c.... } 9

10 Point (x,y) radius Circle height Cylinder 10

11 Circle x,y and radius Point x, y 3D-Point x, y and z class Point { protected: int x, y; public: void set(int a, int b); } class Circle : public Point { private: double radius; } class 3D-Point: public Point { private: int z; } 11

12 Circle Point x, y class Point { protected: int x, y; public: void set(int a, int b); } class Circle : public Point { private: } x,y and radius double radius; class Circle { protected: int x,y; private: double radius; public: void set(int a, int b); } 12

13 C++ class Point { public: int x, y; class Circle : public Point { double radius; Equivalent C implementation struct Point { int x, y struct Circle { int x, y; double radius; 13

14 Inheritance augment + specialize Augmenting the original class Point Circle 3D-Point Specializing the original class ComplexNumber real imag real RealNumber ImaginaryNumber imag 14

15 derive from members go to base class/superclass / parent class Two levels of access control over class members class definition inheritance type derived class/subclass / child class class Point { protected: int x, y; public: void set(int a,int b); class Circle : public Point { 15

16 Derived-class member functions might require access to base class data members and member functions. A derived class can access the non-private members of its base class. Base class members that should not be accessible to the member functions of derived classes should be declared private in the base class. A derived class can change the values of private base-class members, but only through non-private member functions provided in the base class and inherited into the derived class by Pearson Education, Inc. All Rights Reserved. 16

17 class Base { /* */ class Derived_1 : public Base { // data members and methods of class Derived_1 class Derived_2 : Base { // data members and methods of class Derived_2 The keyword public in the declaration of Derived_1 shows that derivation is public. Public members of Base is public to Derived_1. (all public members are public, protected members are protected and private members are invisible!!) If no keyword (public, private or protected) is used as in Derived_1, then inheritance is private 17

18 The base class may be inherited through public, protected or private inheritance. Use of protected and private inheritance is rare, and each should be used only with great care; public inheritance is common. A base class s private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected methods of the base class. Figure summarizes for each type of inheritance the accessibility of base-class members in a derived class by Pearson Education, Inc. All Rights Reserved. 18

19 public, protected and private Inheritance by Pearson Education, Inc. All Rights Reserved. 19

20 public and private Inheritance When base class inherited as public: class Circle : public Point { } public in base class public in derived class private in base class Inaccessible in derived class When base class inherited as private class Circle : private Point { } public in base class private in derived class private in base class Inaccessible in derived class 20

21 class Base { public: int x; private: int y: class Derived_1 : public Base { // Can you access x here? // Can you access y here? class Derived_2 : Base { // Can you access x here? // Can you access y here? 21

22 Every derived class object is an object of its base class, and one base class can have many derived classes. So, the set of objects represented by a base class typically is larger than the set of objects represented by any of its derived classes (See next slide). A base class exists in a hierarchical relationship with its derived classes. A class becomes either a base class supplying members to other classes, a derived class inheriting its members from other classes, or both by Pearson Education, Inc. All Rights Reserved. 22

23 class Base { // Base Class... class Derived_1 : public Base { // Derived Class 1... class Derived_2 : public Base { // Derived Class 2... class Derived_3 : public Derived_1 {... main() { Base b; Derived_1 c1; Derived_2 c2 b = c1; b = c2; // b can be both Derived_1 or Derived_2 } 23

24 Case Study: Point, Circle, Cylinder A Point can be specified by its position (coordinates). A Circle can be specified by its position and a radius. A Cylinder can be specified by a Circle (position, radius) and height. Point (x,y) radius Circle height Cylinder 24

25 class Circle { public: Circle(); void setrad(double r); double getrad(); double Diameter(); double Circumference(); private: double radius; class Cylinder : public Circle{ public: double Volume(); void setheigth(double h); private: double height; Base class Circle - radius + setradius() + getradius() + Diameter() + Circumference() Cylinder -height +Volume() +setheigth() Derived class 25

26 class Circle { public: Circle(); void setrad(double r); double getrad(); double Diameter(); double Circumference(); private: double radius; class Cylinder:public Circle { public: double Volume(); void setheigth(double h); private: double height; MEMBERS FOR CYLINDER Member double radius Access Status in Cylinder Not accessible How Obtained from Circle setrad() public from Circle getrad() public from Circle Diameter() public from Circle Circumference() public from Circle double height setheigth() Volume() private public public its own member its own member its own member 26

27 #include <iostream> using namespace std; #define PI 3.14 class Circle { public: void setrad(double r){ radius = r; } double getrad() { return radius;} double Diameter() { return radius * 2;} double Circumference() { return 2*PI*radius;} double Area() {return PI * radius * radius;} private: double radius; int main() { Cylinder x; x.setrad(5); x.setheight(2); cout << x.getrad(); cout << endl; cout << x.volume(); } class Cylinder : public Circle { public: double Volume() { return PI*getRad()*getRad()*height; } // Can NOT access to radius use getrad() void setheight(double h) { height = h; } private: double height; 27

28 class BC { protected: int x; int y; public: int z; class DC: private BC { protected: using BC::x; public: void output() { cout << x; } using BC::z; } void main() { DC dc; cout << dc.z; } Access to an inherited member maybe adusted to that of the base class by declaring it public or protected in the derived class. 28

29 #define PI 3.14 class Circle { public: void setrad(double r){ radius = r; } double getrad(){ return radius; } double Area() { return radius * radius * PI;} private: X double radius; class Cylinder : public Circle { public: double Volume() { return PI * getrad() * getrad()* height; } double Area() { return 2 * PI * getrad() * height + 2 * PI * getrad() * getrad(); } // 2.pi.r.h + 2.pi.r.r void setheight(double h) { height = h; } private: double height; int main() { Cylinder x; x.setrad(5); x.setheight(2); cout << x.area(); cout << endl; cout << x.volume(); } If a derived class has a member with the same name as a member in base class, local member hides the inherited member 29

30 When a derived-class member function redefines a baseclass member function, the base-class member can be accessed from the derived class by preceding the base-class member name with the base-class name and the binary scope resolution operator (::). class Circle { public: double Area() { return radius * radius* PI;}... class Cylinder : public Circle { public: double Area() { return 2 * PI* getrad() * height + 2 * Circle::Area(); }... 30

31 Let s develop a simple inheritance hierarchy with five levels (represented by the UML class diagram in Fig. 20.2). A university community has thousands of members. Employees are either faculty members or staff members. Faculty members are either administrators (such as deans and department chairpersons) or teachers. Some administrators, however, also teach classes. Note that we ve used multiple inheritance to form class AdministratorTeacher. Also, this inheritance hierarchy could contain many other classes by Pearson Education, Inc. All Rights Reserved. 31

32 by Pearson Education, Inc. All Rights Reserved. 32

33 Each arrow in the hierarchy (Fig. 20.2) represents an is-a relationship. As we follow the arrows in this class hierarchy, we can state an Employee is a CommunityMember and a Teacher is a Faculty member. CommunityMember is the direct base class of Employee, Student and Alumnus. CommunityMember is an indirect base class of all the other classes in the diagram. Starting from the bottom of the diagram, you can follow the arrows and apply the is-a relationship to the topmost base class. An AdministratorTeacher is an Administrator, is a Faculty member, is an Employee and is a CommunityMember by Pearson Education, Inc. All Rights Reserved. 33

34 Consider the Shape inheritance hierarchy in Fig Begins with base class Shape. Classes TwoDimensionalShape and ThreeDimensionalShape derive from base class Shape Shapes are either TwoDimensionalShapes or Three-DimensionalShapes. The third level of this hierarchy contains some more specific types of TwoDimensionalShapes and ThreeDimensionalShapes. As in Fig. 20.2, we can follow the arrows from the bottom of the diagram to the topmost base class in this class hierarchy to identify several is-a relationships by Pearson Education, Inc. All Rights Reserved. 34

35 35

36 A base class s public members are accessible within its body and anywhere that the program has a handle (i.e., a name, reference or pointer) to an object of that class or one of its derived classes. A base class s private members are accessible only within its body and to the friends of that base class. In this section, we introduce the access specifier protected. Using protected access offers an intermediate level of protection between public and private access. A base class s protected members can be accessed within the body of that base class, by members and friends of that base class, and by members and friends of any classes derived from that base class. Derived-class member functions can refer to public and protected members of the base class simply by using the member names by Pearson Education, Inc. All Rights Reserved. 36

37 In the absence of inheritance protected members are just like private members In public inheritance, a protected member can be accessed ( is visible) as a protected member by the derived class class BC { public : void init_x(); protected: int get_x(); private : int x; void print_x(); class DC : public BC { public: void g() { init_x(); // POSSIBLE get_x(); // POSSIBLE cout << x; // IMPOSSIBLE print_x(); // IMPOSSIBLE } 37

38 public, protected and private Inheritance When base class inherited as public: class Circle : public Point { } public in base class public in derived class protected in base class protected in derived class When base class inherited as protected: class Circle : protected Point { } public in base class protected in derived class protected in base class protected in derived class When base class inherited as private class Circle : private Point { } public in base class private in derived class protected in base class private in derived class 38

39 class Animal { public: string species; float lifeexpectancy; bool warmblooded; class Cat : public Animal { public : string range[100]; float favoriteprey[100][100]; class HouseCat: public Cat { public: string toys [10000]; string catdentist; string catdoctor; HouseCat has totally 8 members; 3 of them is its own member 2 of them are directly inherited from Cat; 3 of them are inherited indirectly from Animal 39

40 When we use inheritance to create a new class from an existing one, the new class inherits the data members and member functions of the existing class. We can customize the new class to meet our needs by including additional members and by redefining base-class members. The derived-class programmer does this in C++ without accessing the base class s source code. The derived class must be able to link to the base class s object code by Pearson Education, Inc. All Rights Reserved. 40

41 ISVs can develop proprietary classes for sale or license and make these classes available to users in object-code format. Users can derive new classes from these library classes rapidly and without accessing the ISVs proprietary source code. All the ISVs need to supply with the object code are the header files. The availability of substantial and useful class libraries delivers the maxi-mum benefits of software reuse through inheritance by Pearson Education, Inc. All Rights Reserved. 41

42 END OF WEEK by Pearson Education, Inc. All Rights Reserved.

Cpt S 122 Data Structures. Inheritance

Cpt S 122 Data Structures. Inheritance Cpt S 122 Data Structures Inheritance Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Base Classes & Derived Classes Relationship between

More information

Inheritance (Deitel chapter 9)

Inheritance (Deitel chapter 9) Inheritance (Deitel chapter 9) 1 2 Plan Introduction Superclasses and Subclasses protected Members Constructors and Finalizers in Subclasses Software Engineering with Inheritance 3 Introduction Inheritance

More information

Programming in C# Inheritance and Polymorphism

Programming in C# Inheritance and Polymorphism Programming in C# Inheritance and Polymorphism C# Classes Classes are used to accomplish: Modularity: Scope for global (static) methods Blueprints for generating objects or instances: Per instance data

More information

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy CISC181 Introduction to Computer Science Dr. McCoy Lecture 27 December 8, 2009 Object Oriented Programming Classes categorize entities that occur in applications. Class teacher captures commonalities of

More information

INHERITANCE - Part 1. CSC 330 OO Software Design 1

INHERITANCE - Part 1. CSC 330 OO Software Design 1 INHERITANCE - Part 1 Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors CSC 330 OO Software Design 1

More information

A A B U n i v e r s i t y

A A B U n i v e r s i t y A A B U n i v e r s i t y Faculty of Computer Sciences O b j e c t O r i e n t e d P r o g r a m m i n g Week 10: I n h e r i t a n c e Asst. Prof. Dr. M entor Hamiti mentor.hamiti@universitetiaab.com

More information

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance?

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance? INHERITANCE - Part 1 OOP Major Capabilities Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors encapsulation

More information

Inheritance Motivation

Inheritance Motivation Inheritance Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: Code re-use Grouping similar code Flexibility to customize Inheritance Concepts Many real-life

More information

INHERITANCE - Part 1. CSC 330 OO Software Design 1

INHERITANCE - Part 1. CSC 330 OO Software Design 1 INHERITANCE - Part 1 Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors CSC 330 OO Software Design 1

More information

Chapter 9 - Object-Oriented Programming: Inheritance

Chapter 9 - Object-Oriented Programming: Inheritance Chapter 9 - Object-Oriented Programming: Inheritance 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship between Superclasses and Subclasses 9.5 Case Study: Three-Level

More information

Problem: Given a vector of pointers to Shape objects, print the area of each of the shape.

Problem: Given a vector of pointers to Shape objects, print the area of each of the shape. Problem: Given a vector of pointers to Shape objects, print the area of each of the shape. 1 Problem: Given a vector of pointers to Shape objects, sort the shape objects by areas in decreasing order. Std++sort:

More information

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory C++ 프로그래밍실습 Visual Studio 2015 Contents Inheritance Exercise Practice1 Inheritance Practice 1-1 : Inheritance What is inheritance? The heart of OO programming A mechanism to build a new class by deriving

More information

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All Inheritance A form of software reuse in which a new class is created by absorbing an existing class s members and enriching them with

More information

Chapter 19 - C++ Inheritance

Chapter 19 - C++ Inheritance Chapter 19 - C++ Inheritance 19.1 Introduction 19.2 Inheritance: Base Classes and Derived Classes 19.3 Protected Members 19.4 Casting Base-Class Pointers to Derived-Class Pointers 19.5 Using Member Functions

More information

Chapter 19 C++ Inheritance

Chapter 19 C++ Inheritance Chapter 19 C++ Inheritance Angela Chih-Wei i Tang Department of Communication Engineering National Central University JhongLi, Taiwan 2009 Fall Outline 19.11 Introduction ti 19.2 Inheritance: Base Classes

More information

Fig. 9.1 Fig. 9.2 Fig. 9.3 Fig. 9.4 Fig. 9.5 Fig. 9.6 Fig. 9.7 Fig. 9.8 Fig. 9.9 Fig Fig. 9.11

Fig. 9.1 Fig. 9.2 Fig. 9.3 Fig. 9.4 Fig. 9.5 Fig. 9.6 Fig. 9.7 Fig. 9.8 Fig. 9.9 Fig Fig. 9.11 CHAPTER 9 INHERITANCE 1 Illustrations List (Main Page) Fig. 9.1 Fig. 9.2 Fig. 9.3 Fig. 9.4 Fig. 9.5 Fig. 9.6 Fig. 9.7 Fig. 9.8 Fig. 9.9 Fig. 9.10 Fig. 9.11 Some simple inheritance examples. An inheritance

More information

Introduction to inheritance

Introduction to inheritance C++ Inheritance and Polymorphism CSIE Department, NTUT Woei-Kae Chen 1 Introduction to inheritance Inheritance Derived class inherits data members and member functions from base class Single inheritance

More information

CHAPTER 6 Class-Advanced Concepts - Inheritance

CHAPTER 6 Class-Advanced Concepts - Inheritance CHAPTER 6 Class-Advanced Concepts - Inheritance Page 1 Introduction: The idea of deriving a new class from the existing class. Provides the idea of code reusability. Existing class is called as base class

More information

BBM 102 Introduction to Programming II Spring Inheritance

BBM 102 Introduction to Programming II Spring Inheritance BBM 102 Introduction to Programming II Spring 2018 Inheritance 1 Today Inheritance Notion of subclasses and superclasses protected members UML Class Diagrams for inheritance 2 Inheritance A form of software

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 4&5: Inheritance Lecture Contents What is Inheritance? Super-class & sub class The object class Using extends keyword @override keyword

More information

Inheritance Introduction. 9.1 Introduction 361

Inheritance Introduction. 9.1 Introduction 361 www.thestudycampus.com Inheritance 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship Between Superclasses and Subclasses 9.4.1 Creating and Using a CommissionEmployee

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Operator Overloading, Inheritance Lecture 6 February 10, 2005 Fundamentals of Operator Overloading 2 Use operators with objects

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

Object Oriented Programming

Object Oriented Programming OOP Object Oriented Programming Object 2 Object 1 Object 3 For : COP 3330. Object oriented Programming (Using C++) Object 4 http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Objects: State (fields),

More information

C++ Programming: Inheritance

C++ Programming: Inheritance C++ Programming: Inheritance 2018 년도 2 학기 Instructor: Young-guk Ha Dept. of Computer Science & Engineering Contents Basics on inheritance C++ syntax for inheritance protected members Constructors and destructors

More information

Class (Inheritance) SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Class (Inheritance) SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong Class (Inheritance) Prof. Jinkyu Jeong (Jinkyu@skku.edu) TA -- Minwoo Ahn (minwoo.ahn@csl.skku.edu) TA -- Donghyun Kim (donghyun.kim@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

More information

Computer Programming Inheritance 10 th Lecture

Computer Programming Inheritance 10 th Lecture Computer Programming Inheritance 10 th Lecture 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University Copyrights 2015 Eom, Hyeonsang All Rights Reserved 순서 Inheritance

More information

Data Structures (INE2011)

Data Structures (INE2011) Data Structures (INE2011) Electronics and Communication Engineering Hanyang University Haewoon Nam ( hnam@hanyang.ac.kr ) Lecture 1 1 Data Structures Data? Songs in a smartphone Photos in a camera Files

More information

Chapter 1: Object-Oriented Programming Using C++

Chapter 1: Object-Oriented Programming Using C++ Chapter 1: Object-Oriented Programming Using C++ Objectives Looking ahead in this chapter, we ll consider: Abstract Data Types Encapsulation Inheritance Pointers Polymorphism Data Structures and Algorithms

More information

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Section 1 Multiple Choice Questions (40 pts total, 2 pts each): Q1: Employee is a base class and HourlyWorker is a derived class, with

More information

More C++ : Vectors, Classes, Inheritance, Templates

More C++ : Vectors, Classes, Inheritance, Templates Vectors More C++ : Vectors,, Inheritance, Templates vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes defined differently can be resized without explicit

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com More C++ : Vectors, Classes, Inheritance, Templates with content from cplusplus.com, codeguru.com 2 Vectors vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes

More information

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?

More information

Module 7 b. -Namespaces -Exceptions handling

Module 7 b. -Namespaces -Exceptions handling Module 7 b -Namespaces -Exceptions handling C++ Namespace Often, a solution to a problem will have groups of related classes and other declarations, such as functions, types, and constants. C++provides

More information

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism Review from Lecture 22 Added parent pointers to the TreeNode to implement increment and decrement operations on tree

More information

ECE 3574: Dynamic Polymorphism using Inheritance

ECE 3574: Dynamic Polymorphism using Inheritance 1 ECE 3574: Dynamic Polymorphism using Inheritance Changwoo Min 2 Administrivia Survey on class will be out tonight or tomorrow night Please, let me share your idea to improve the class! 3 Meeting 10:

More information

Extending Classes (contd.) (Chapter 15) Questions:

Extending Classes (contd.) (Chapter 15) Questions: Extending Classes (contd.) (Chapter 15) Questions: 1 1. The following C++ program compiles without any problems. When run, it even prints out the hello called for in line (B) of main. But subsequently

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

Lecture 5: Inheritance

Lecture 5: Inheritance McGill University Computer Science Department COMP 322 : Introduction to C++ Winter 2009 Lecture 5: Inheritance Sami Zhioua March 11 th, 2009 1 Inheritance Inheritance is a form of software reusability

More information

Where do we stand on inheritance?

Where do we stand on inheritance? In C++: Where do we stand on inheritance? Classes can be derived from other classes Basic Info about inheritance: To declare a derived class: class :public

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ Inheritance Concept Polygon Rectangle Triangle class Polygon{ private: int numvertices; float *xcoord, *ycoord; void set(float *x, float *y, int nv); class Rectangle{

More information

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am CMSC 202 Section 010x Spring 2007 Computer Science II Final Exam Name: Username: Score Max Section: (check one) 0101 - Justin Martineau, Tuesday 11:30am 0102 - Sandeep Balijepalli, Thursday 11:30am 0103

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Lecture 3: Introduction to C++ (Continue) Examples using declarations that eliminate the need to repeat the std:: prefix 1 Examples using namespace std; enables a program to use

More information

Chapter 20 - C++ Virtual Functions and Polymorphism

Chapter 20 - C++ Virtual Functions and Polymorphism Chapter 20 - C++ Virtual Functions and Polymorphism Outline 20.1 Introduction 20.2 Type Fields and switch Statements 20.3 Virtual Functions 20.4 Abstract Base Classes and Concrete Classes 20.5 Polymorphism

More information

Friend Functions, Inheritance

Friend Functions, Inheritance Friend Functions, Inheritance Friend Function Private data member of a class can not be accessed by an object of another class Similarly protected data member function of a class can not be accessed by

More information

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class Chapter 6 Inheritance Extending a Class Introduction; Need for Inheritance; Different form of Inheritance; Derived and Base Classes; Inheritance and Access control; Multiple Inheritance Revisited; Multilevel

More information

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory. Pointer C++ Memory Map Once a program is compiled, C++ creates four logically distinct regions of memory: Code Area : Area to hold the compiled program code Data Area : Area to hold global variables Stack

More information

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III Distributed Real-Time Control Systems Lecture 14 Intro to C++ Part III 1 Class Hierarchies The human brain is very efficient in finding common properties to different entities and classify them according

More information

2 nd Week Lecture Notes

2 nd Week Lecture Notes 2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous

More information

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples: Unit IV Pointers and Polymorphism in C++ Concepts of Pointer: A pointer is a variable that holds a memory address of another variable where a value lives. A pointer is declared using the * operator before

More information

a. a * c - 10 = b. a % b + (a * d) + 7 =

a. a * c - 10 = b. a % b + (a * d) + 7 = Exam #2 CISC1110, MW 10:35-12:40pm Fall 2011 Name 1 Evaluate each expression according to C++ rules (8 pts) Given: Integers a = 3, b = 2, c = 5, and float d = 40 a a * c - 10 = b a % b + (a * d) + 7 =

More information

Polymorphism. Arizona State University 1

Polymorphism. Arizona State University 1 Polymorphism CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 15 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT Two Mark Questions UNIT - I 1. DEFINE ENCAPSULATION. Encapsulation is the process of combining data and functions

More information

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1 Polymorphism Part 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid adult

More information

Increases Program Structure which results in greater reliability. Polymorphism

Increases Program Structure which results in greater reliability. Polymorphism UNIT 4 C++ Inheritance What is Inheritance? Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the

More information

Introduction Of Classes ( OOPS )

Introduction Of Classes ( OOPS ) Introduction Of Classes ( OOPS ) Classes (I) A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class.

More information

IUE Faculty of Engineering and Computer Sciences Spring Semester

IUE Faculty of Engineering and Computer Sciences Spring Semester IUE Faculty of Engineering and Computer Sciences 2010-2011 Spring Semester CS116 Introduction to Programming II Midterm Exam II (May 11 th, 2011) This exam document has 5 pages and 4 questions. The exam

More information

Inheritance (continued) Inheritance

Inheritance (continued) Inheritance Objectives Chapter 11 Inheritance and Polymorphism Learn about inheritance Learn about subclasses and superclasses Explore how to override the methods of a superclass Examine how constructors of superclasses

More information

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year Object Oriented Programming Assistant Lecture Omar Al Khayat 2 nd Year Syllabus Overview of C++ Program Principles of object oriented programming including classes Introduction to Object-Oriented Paradigm:Structures

More information

CSC330 Object Oriented Programming. Inheritance

CSC330 Object Oriented Programming. Inheritance CSC330 Object Oriented Programming Inheritance Software Engineering with Inheritance Can customize derived classes to meet needs by: Creating new member variables Creating new methods Override base-class

More information

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

More information

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I.

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I. y UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I Lab Module 7 CLASSES, INHERITANCE AND POLYMORPHISM Department of Media Interactive

More information

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes 1 Objectives Classes & Objects ( 9.2). UML ( 9.2). Constructors ( 9.3). How to declare a class & create an object ( 9.4). Separate a class declaration from a class implementation

More information

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance. Class : BCA 3rd Semester Course Code: BCA-S3-03 Course Title: Object Oriented Programming Concepts in C++ Unit III Inheritance The mechanism that allows us to extend the definition of a class without making

More information

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1 CS250 Intro to CS II Spring 2017 CS250 - Intro to CS II 1 Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining

More information

Distributed Real-Time Control Systems. Chapter 13 C++ Class Hierarchies

Distributed Real-Time Control Systems. Chapter 13 C++ Class Hierarchies Distributed Real-Time Control Systems Chapter 13 C++ Class Hierarchies 1 Class Hierarchies The human brain is very efficient in finding common properties to different entities and classify them according

More information

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS 162, Lecture 25: Exam II Review. 30 May 2018 CS 162, Lecture 25: Exam II Review 30 May 2018 True or False Pointers to a base class may be assigned the address of a derived class object. In C++ polymorphism is very difficult to achieve unless you

More information

INHERITANCE PART 2. Constructors and Destructors under. Multiple Inheritance. Common Programming Errors. CSC 330 OO Software Design 1

INHERITANCE PART 2. Constructors and Destructors under. Multiple Inheritance. Common Programming Errors. CSC 330 OO Software Design 1 INHERITANCE PART 2 Constructors and Destructors under Inheritance Multiple Inheritance private and protected Inheritance Common Programming Errors CSC 330 OO Software Design 1 What cannot be inherited?

More information

SSE2034: System Software Experiment 3 Spring 2016

SSE2034: System Software Experiment 3 Spring 2016 SSE2034: System Software Experiment 3 Spring 2016 Jinkyu Jeong ( jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Object Initialization class Rectangle { private:

More information

Comp151. Inheritance: Initialization & Substitution Principle

Comp151. Inheritance: Initialization & Substitution Principle Comp151 Inheritance: Initialization & Substitution Principle Initializing Base Class Objects If class C is derived from class B which is in turn derived from class A, then C will contain data members of

More information

AN OVERVIEW OF C++ 1

AN OVERVIEW OF C++ 1 AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function

More information

CSC1322 Object-Oriented Programming Concepts

CSC1322 Object-Oriented Programming Concepts CSC1322 Object-Oriented Programming Concepts Instructor: Yukong Zhang February 18, 2016 Fundamental Concepts: The following is a summary of the fundamental concepts of object-oriented programming in C++.

More information

COEN244: Polymorphism

COEN244: Polymorphism COEN244: Polymorphism Aishy Amer Electrical & Computer Engineering Polymorphism means variable behavior / ability to appear in many forms Outline Casting between objects Using pointers to access objects

More information

Inheritance. Software Engineering with Inheritance. CSC330 Object Oriented Programming. Base Classes and Derived Classes. Class Relationships I

Inheritance. Software Engineering with Inheritance. CSC330 Object Oriented Programming. Base Classes and Derived Classes. Class Relationships I CSC0 Object Oriented Programming Inheritance Software Engineering with Inheritance Can customize derived classes to meet needs by: Creating new member variables Creating new methods Override base-class

More information

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles, Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

OOP. Unit:3.3 Inheritance

OOP. Unit:3.3 Inheritance Unit:3.3 Inheritance Inheritance is like a child inheriting the features of its parents. It is a technique of organizing information in a hierarchical (tree) form. Inheritance allows new classes to be

More information

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018) Lesson Plan Name of the Faculty Discipline Semester :Mrs. Reena Rani : Computer Engineering : IV Subject: OBJECT ORIENTED PROGRAMMING USING C++ Lesson Plan Duration :15 weeks (From January, 2018 to April,2018)

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

CSE 143. "Class Relationships" Class Relationships and Inheritance. A Different Relationship. Has-a vs. Is-a. Hierarchies of Organization

CSE 143. Class Relationships Class Relationships and Inheritance. A Different Relationship. Has-a vs. Is-a. Hierarchies of Organization Class Relationships and Inheritance [Chapter 8, pp.343-354] "Class Relationships" is the title of Chapter 8 One class may include another as a member variable Date" used inside "Performance" Called a "has-a"

More information

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

The Notion of a Class and Some Other Key Ideas (contd.) Questions: The Notion of a Class and Some Other Key Ideas (contd.) Questions: 1 1. WHO IS BIGGER? MR. BIGGER OR MR. BIGGER S LITTLE BABY? Which is bigger? A class or a class s little baby (meaning its subclass)?

More information

Inheritance and Overloading. Week 11

Inheritance and Overloading. Week 11 Inheritance and Overloading Week 11 1 Inheritance Objects are often defined in terms of hierarchical classes with a base class and one or more levels of classes that inherit from the classes that are above

More information

Polymorphism CSCI 201 Principles of Software Development

Polymorphism CSCI 201 Principles of Software Development Polymorphism CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu Program Outline USC CSCI 201L Polymorphism Based on the inheritance hierarchy, an object with a compile-time

More information

Object Orientated Analysis and Design. Benjamin Kenwright

Object Orientated Analysis and Design. Benjamin Kenwright Notation Part 2 Object Orientated Analysis and Design Benjamin Kenwright Outline Review What do we mean by Notation and UML? Types of UML View Continue UML Diagram Types Conclusion and Discussion Summary

More information

Review Questions for Final Exam

Review Questions for Final Exam CS 102 / ECE 206 Spring 11 Review Questions for Final Exam The following review questions are similar to the kinds of questions you will be expected to answer on the Final Exam, which will cover LCR, chs.

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Inheritance Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

Syllabus for Bachelor of Technology. Computer Engineering. Subject Code: 01CE1303. B.Tech. Year - II

Syllabus for Bachelor of Technology. Computer Engineering. Subject Code: 01CE1303. B.Tech. Year - II Subject Code: 01CE1303 Subject Name: Object Oriented Design and Programming B.Tech. Year - II Objective: The objectives of the course are to have students identify and practice the object-oriented programming

More information

COMPUTER PROGRAMMING (ECE 431) TUTORIAL 9

COMPUTER PROGRAMMING (ECE 431) TUTORIAL 9 COMPUTER PROGRAMMING (ECE 431) TUTORIAL 9 1. What is object oriented programming (OOP)? How is it differs from the traditional programming? 2. What is a class? How a class is different from a structure?

More information

CMSC 202 Midterm Exam 1 Fall 2015

CMSC 202 Midterm Exam 1 Fall 2015 1. (15 points) There are six logic or syntax errors in the following program; find five of them. Circle each of the five errors you find and write the line number and correction in the space provided below.

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Inheritance Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini 22. Inheritance Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Inheritance Object-oriented programming

More information

Namespaces and Class Hierarchies

Namespaces and Class Hierarchies and 1 2 3 MCS 360 Lecture 9 Introduction to Data Structures Jan Verschelde, 13 September 2010 and 1 2 3 Suppose we need to store a point: 1 data: integer coordinates; 2 functions: get values for the coordinates

More information

Computer Science II (20073) Week 1: Review and Inheritance

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

Previously, on Lesson Night... From Intermediate Programming, Part 1

Previously, on Lesson Night... From Intermediate Programming, Part 1 Previously, on Lesson Night... From Intermediate Programming, Part 1 Struct A way to define a new variable type. Structs contains a list of member variables and functions, referenced by their name. public

More information

Lecture 6. Inheritance

Lecture 6. Inheritance Inheritance Lecture 6 A key feature of C++ classes is inheritance. Inheritance allows to create classes which are derived from other classes, so that they automatically include some of its "parent's" members,

More information

Abstract Data Types (ADT) and C++ Classes

Abstract Data Types (ADT) and C++ Classes Abstract Data Types (ADT) and C++ Classes 1-15-2013 Abstract Data Types (ADT) & UML C++ Class definition & implementation constructors, accessors & modifiers overloading operators friend functions HW#1

More information

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming COP3330 / CGS5409 Object Oriented Programming COP3330 / CGS5409 Classes & Objects DDU Design Constructors Member Functions & Data Friends and member functions Const modifier Destructors Object -- an encapsulation of data

More information