CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.

Slides:



Advertisements
Similar presentations
Chapter 12: Inheritance and Composition
Advertisements

Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Exercise 2.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
OOP: Inheritance By: Lamiaa Said.
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Inheritance in C++ Multiple Base Classes Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
CS-2135 Object Oriented Programming
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 12: Inheritance and Composition.
UML Class Diagram: class Rectangle
Chapter 10: Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 12: Adding Functionality to Your Classes.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Intro to OOP with Java, C. Thomas Wu
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
CE Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.
Pointer Data Type and Pointer Variables II By: Nouf Aljaffan Edited by : Nouf Almunyif.
Chapter 13: Inheritance and Composition
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Access Control Under Inheritance tMyn1 Access Control Under Inheritance The private data members of a base class are also members of the derived class,
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
Chapter 10 Inheritance and Polymorphism
INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.
Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
1 Chapter 4: Another way to define a class Inheritance..!!
1 Inheritance and Polymorphism Chapter 11 Spring 2007 CS 101 Aaron Bloomfield.
EEL 3801 Part VII Fundamentals of C and C++ Programming Inheritance.
1.  Inheritance Inheritance  Define a Class Hierarchy Define a Class Hierarchy  Visibility Mode Visibility Mode  Access Rights of Derived Classes.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance ITI1121 Nour El Kadri.
Chapter 11: Inheritance and Polymorphism
Chapter 5 Classes.
CS1201: Programming Language 2
Week 4 Object-Oriented Programming (1): Inheritance
UML Class Diagram: class Rectangle
Andy Wang Object Oriented Programming in C++ COP 3330
CS1201: Programming Language 2
MSIS 670 Object-Oriented Software Engineering
CS1201: Programming Language 2
CS1201: Programming Language 2
CS1201: Programming Language 2
Inheritance CT1513.
Multiple Base Classes Inheritance
Chapter 11: Inheritance and Composition
CS1201: Programming Language 2
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif

Inheritance Inheritance and composition are meaningful ways to relate two or more classes. Inheritance implements an is-a relationship: For example - a mustang is-a car. Inheritance lets us create new classes from existing classes. ▫The new classes that we create from the existing classes are called the derived classes;. Is also referred to as the subclass. ▫the existing classes are called the base classes. Is also called the superclass. ▫ The derived classes inherit the properties of the base classes. Each derived class, in turn, becomes a base class for a future derived class. A derived class can redefine the member functions of a base class, but this redefinition applies only to the objects of the derived class.

Hierarchical Inheritance Inheritance is hierarchical: ▫A derived class can also act as a base class to a lower-level derived class. ▫ The higher the class in the hierarchy, the more general information it contains. ▫The lower the class in the hierarchy, the more specific information it contains. Attributes in a derived class overwrite the same ones in a base class.

Hierarchical Inheritance

vehicle Water vehicle boat Land vehicle carsubmarine is-a bicycle

class inheritance definition: class Derived_Class_name: accessId Base_Class_name { DClassMembersList }; accessId define how can the derived class access the Base class members. Access identifier can be either public, protected and privet.

Reviewing public, protected, and private When a class member is declared : ▫as public, it can be accessed by any other part of a program. ▫as private, it can be accesses only by members of its class.  Further, derived classes do not have access to private base class members. ▫as protected, it can be accessed only by members of its class.  However, derived classes also have access to protected base class members.  Thus, protected allows a member to be inherited, but to remain private within a class hierarchy.

Cont.. When a base class is inherited by use of (accessId ) ▫ public, (default)  its public members become public members of the derived class.  its protected members become protected members of the derived class. ▫protected, its public and protected members become protected members of the derived class. ▫private, its public and protected members become private members of the derived class. In all cases, private members of a base class remain private to that base class.

Inheritance and accessibility Access Specifier Accessible from own class Accessible from Derived Class Accessible from Objects outside class PublicYes ProtectedYes No PrivateYesNo

Example ( public access) #include using namespace std; class rectangleType // base class { protected: double length; double width; public: rectangleType() {length = 0; width = 0;} rectangleType( double L, double w) {setDimension( L, w); } void setDimension ( double L, double w) {if ( L >= 0 ) length = L; else length = 0; if ( w >= 0 )width= w; elsewidth = 0; } 11 double getLength() {return length;} double getWidth() {return width;} double area() {return length * width;} double perimeter() {return 2 * ( length + width );} void print(){ cout<<"Length = "<< length << " ; Width = " << width; } };

class boxType: public rectangleType { private: double height; public: boxType() {height = 0 ;} boxType( double L, double w, double h) {setDimension( L, w, h); } ~boxType(){} void setDimension ( double L, double w, double h) {rectangleType::setDimension( L, w ); if ( h >= 0) ‏ height = h; else height = 0;} double getHeight() {return height; } double area() {return 2 * ( length * width + length * height + width * height ); } double volume() {return rectangleType::area() * height; } void print() {rectangleType::print(); cout << " ; Height = " << height;} };

Cont. Example Void main() ‏ { rectangleType myRectangle1; rectangleType myRectangle2(8, 6); boxType myBox1; boxType myBox2(10, 7, 3); cout << "\n myRectangle1: "; myRectangle1.print(); cout << endl; cout << " Area of myRectangle1: " << myRectangle1.area() << endl; cout << "\n myRectangle2: "; myRectangle2.print(); cout << endl; cout << " Area of myRectangle2: " << myRectangle2.area() << endl; }