Access Control Under Inheritance tMyn1 Access Control Under Inheritance The private data members of a base class are also members of the derived class,

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

4/14/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23m2 OOP Friend Functions Ref: Sebesta, Chapter 12; Lafore, Chapter 11.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
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.
Inheritance Math 130 B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours B Smith: Consider using the example in SAMS Teach Yourself.
Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
1 Class Constructor Is a specific method Used to initialize the class’s fields Having the same name as the declaring classclass Doesn’t have return type(even.
Chapter 10: Introduction to Inheritance
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class.
CS-2135 Object Oriented Programming
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
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.
Intro to OOP with Java, C. Thomas Wu
Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
Inheritance One of the most powerful features of C++
Overview of Previous Lesson(s) Over View .NET Framework is a software framework developed by Microsoft that runs primarily on Microsoft Windows.  It.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
Understanding Polymorphism tMyn1 Understanding Polymorphism Polymorphism requires the use of derived classes. It always involves the use of a pointer to.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
CITA 342 Section 1 Object Oriented Programming (OOP)
Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-1 Concordia University Department of Computer Science and Software Engineering COMP345.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
Access Control to Class Members tMyn1 Access Control to Class Members In its support for encapsulation, the class provides two major benefits. First, it.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Topics Instance variables, set and get methods Encapsulation
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
Constructor Operation tMyn1 Constructor Operation in a Derived Class So far the default base class constructor has been called automatically. We can arrange.
Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user.
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.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
1 More About Derived Classes and Inheritance Chapter 9.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Friend functions.
Templates.
Encapsulation, Data Hiding and Static Data Members
Chapter 5 Classes.
Subject Name: PROGRAMMING IN C++ Subject Code: 10EC665
Method Mark and Lyubo.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
CS1201: Programming Language 2
CS1201: Programming Language 2
CS1201: Programming Language 2
CLASSES AND OBJECTS.
What about multi-dimensional arrays?
Object oriented programming (OOP) Lecture No. 7
By Rajanikanth B OOP Concepts By Rajanikanth B
Object Oriented Programming
ENERGY 211 / CME 211 Lecture 22 November 10, 2008.
Inheritance in C++ Inheritance Protected Section
Inheritance Lakshmish Ramaswamy.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Computer Science II for Majors
Presentation transcript:

Access Control Under Inheritance tMyn1 Access Control Under Inheritance The private data members of a base class are also members of the derived class, but they remain private to the base class within the derived class. This means that the inherited data members are accessible to the inherited function members from the base class, but they are not accessible to the member functions declared within the derived class definition. So this does not work:

Access Control Under Inheritance tMyn2 … class Box { public: Box(); ~Box(); private: double length; double breadth; double height; }; …

Access Control Under Inheritance tMyn3 … class Carton:public Box { public: Carton(); ~Carton(); double volume(); private: double weight; }; …

Access Control Under Inheritance tMyn4 … double Carton::volume() { return length*breadth*height; } … 'Box::length' : cannot access private member declared in class 'Box' 'Box::breadth' : cannot access private member declared in class 'Box' 'Box::height' : cannot access private member declared in class 'Box'

Access Control Under Inheritance tMyn5 However, it is legal to use the volume() function if it is a base class member:

Access Control Under Inheritance tMyn6 … class Box { public: Box(); ~Box(); double volume(); private: double length; double breadth; double height; }; … double Box::volume() { return length*breadth*height; } …

Access Control Under Inheritance tMyn7 … class Carton:public Box { public: Carton(); ~Carton(); private: double weight; }; …

Access Control Under Inheritance tMyn8 … int main(array ^args) { Box first=Box(); cout<<"Volume is "<<first.volume()<<endl; Carton second=Carton(); cout<<"Volume is "<<second.volume()<<endl; return 0; } The volume() function defined in the Box class works equally well for objects of the Carton class.

Access Control Under Inheritance tMyn9

Access Control Under Inheritance tMyn10 The private members of a base class are only accessible to function members of the base class, but this isn’t always convenient. There will doubtless be many occasions when we want the members of a base class to be accessible from within the derived class, but nonetheless protected from outside interference. In addition to the public and private access specifiers for members of a class, you can also declare members of a class as protected. Within the class, the keyword protected has exactly the same effect as the keyword private.

Access Control Under Inheritance tMyn11 Members of a class that are protected can only be accessed by member functions of the class (...friend classes and friend functions of the class). These protected class members can’t be accessed from outside the class, so they behave like private class members. The difference between protected and private members only becomes apparent in a derived class. Members of a base class that are declared as protected are freely accessible in function members of a derived class, whereas the private members of the base class are not.

Access Control Under Inheritance tMyn12 class Box { public: Box(); ~Box(); protected: double length; double breadth; double height; };

Access Control Under Inheritance tMyn13 … class Carton:public Box { public: Carton(); ~Carton(); double volume(); private: double weight; }; … double Carton::volume() { return length*breadth*height; } … Those came as being protected from the base class

Access Control Under Inheritance tMyn14 int main(array ^args) { Box first=Box(); Carton second=Carton(); cout<<"Volume is "<<second.volume()<<endl; return 0; }

Access Control Under Inheritance tMyn15

Access Control Under Inheritance tMyn16 Notice: you are not able to have the line cout<<first.volume(); because the Box class now has no function volume() defined. However, this works well: cout<<second.volume(); This is because the function volume() accesses the inherited members length, breadth and height to produce the result. These members were declared as protected in the base class and remain protected in the derived class.

Access Control Under Inheritance tMyn17 You can’t have the line second.length=5.5; because the protected members of the base class remain protected in the derived class.