CS1201: Programming Language 2

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Chapter 12: Inheritance and Composition
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
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.
C++ Classes & Data Abstraction
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
CS 222 Object Oriented Programming Using C++ Inheritance.
Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed.
Inheritance in C++ Multiple Base Classes Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
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 12: Adding Functionality to Your Classes.
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
Pointer Data Type and Pointer Variables II By: Nouf Aljaffan Edited by : Nouf Almunyif.
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 One of the most powerful features of C++
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
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
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)
1 Chapter 4: Another way to define a class Inheritance..!!
EEL 3801 Part VII Fundamentals of C and C++ Programming Inheritance.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
1.  The following class, called road_vehicle, very broadly defines vehicles, that travel on the road. It stores the number of wheels a vehicle has and.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Procedural and Object-Oriented Programming
Access Functions and Friend Functions
Inheritance ITI1121 Nour El Kadri.
Chapter 11: Inheritance and Polymorphism
Chapter 5 Classes.
CS1201: Programming Language 2
Week 4 Object-Oriented Programming (1): Inheritance
Chapter 15: Overloading and Templates
UML Class Diagram: class Rectangle
Pointer Data Type and Pointer Variables
Andy Wang Object Oriented Programming in C++ COP 3330
CS1201: Programming Language 2
CS1201: Programming Language 2
MSIS 670 Object-Oriented Software Engineering
CS1201: Programming Language 2
Inheritance.
Virtual Base Classes By:Nouf Aljaffan Edited by : Nouf Almunyif.
CS1201: Programming Language 2
CS1201: Programming Language 2
Inheritance CT1513.
Multiple Base Classes Inheritance
CS1201: Programming Language 2
CS1201: Programming Language 2
Chapter 11: Inheritance and Composition
CS1201: Programming Language 2
Inheriting Multiple Base Classes
CS1201: Programming Language 2
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
CS1201: Programming Language 2
Computer Science II for Majors
Presentation transcript:

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

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

Hierarchical Inheritance

Hierarchical Inheritance vehicle is-a Land vehicle Water vehicle is-a is-a car boat submarine 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, 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, (default) 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.

class A { public: int x; protected: int y; private: int z; }; class B : public A // x is public // y is protected // z is not accessible from B class C : protected A // x is protected // z is not accessible from C class D : private A // 'private' is default for classes // x is private // y is private // z is not accessible from D

Inheritance and accessibility Access Specifier Accessible from own class Accessible from Derived Class Accessible from Objects outside class Public Yes Protected No Private

Example ( public access) #include <iostream> 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; else width = 0; } 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 << " Area of myRectangle1: " << myRectangle1.area() << endl; cout << "\n myRectangle2: "; myRectangle2.print(); cout << endl; cout << " Area of myRectangle2: " << myRectangle2.area() << endl; myBox1.print(); cout<<"surface area of Mybox" <<myBox1.area(); cout<<"volume of mybox1 is " <<myBox1.volumn(); myBox2.print(); cout<<"surface area of Mybox" <<myBox2.area(); cout<<"volume of mybox1 is " <<myBox2.volumn(); }

Cont. Example

Over-written Functions These functions are NOT overloaded, since they have exactly the same prototype (and header), and they are not in the same class. They are over-written functions. The over-written function that is closest to the object defined takes precedence.

public, its public members become public members of the derived class. its protected members become protected members of the derived class. Example : base class inherited as public

#include <iostream> using namespace std; class base // base class {int pri; //private by default protected: int prot; public: int pub; void set(int b) { pri=b;} void setprot(int p) {prot=p;} void show(){ cout<<"in base pri :"<<pri<<"\n";} }; class drived: public base // drivedclass { int k; public: drived( int x) {k =x; } void showK(){ cout<<" in derived k : "<< k << "\n"; cout<<" in deraived prot from base : "<<prot<<endl; //cout << pri; this is error } } ;//end of class void main(){ drived ob(3); ob.set(5); // access member of base ob.show(); // access member of base ob.showK(); // access member of drived class //ob.prot=5;error

protected Example : using protected for inheritance of base class its public and protected members become protected members of the derived class. Example : using protected for inheritance of base class

Private ,(default) 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.

using protected member READ

using protected member

using protected member

using protected member

using protected member