Inheritance

786 views

Published on

this ppt will explain u all in detail what is Inheritance n access specifiers of c++...:)all the best..:)

Published in: Technology, Sports
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total views
786
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
38
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

Inheritance

  1. 1. Welcome,Let’s gear up for the basics of Inheritance andelements of its architecture.
  2. 2. Agenda of the presentation• Basics of inheritance• Private v/s public inheritance• Glimpse at an example
  3. 3. InheritanceIn Object Oriented Programming, Inheritance is the process by which objects ofone class acquire the properties and functionality of objects of another class.It supports the concept of hierarchical classification.
  4. 4. Example at a glance Parent Class Child Classes
  5. 5. What in the need of inheritance in c++?One reason to use inheritance is that it allows you to reuse code from a previousproject but gives you the flexibility to slightly modify it if the old code doesn’t doexactly what you need for the new project.It doesn’t make sense to start every new project from scratch since some code willcertainly be repeated in several programs and you should strive to build on what you didpreviously.Moreover, it is easy to make an error if we try to modify the original class, but we areless likely to make an error if we leave the original alone and only add to it. Another reason for using inheritance is if the project requires the use of several classes which are very similar but slightly different.
  6. 6. What is a class?A class is an expanded concept of a data structure: instead of holding only data, itcan hold both data and functions.Base ClassA base class is a class that is created with the intention of deriving otherclasses from it.Parent ClassA parent class is the closest class that we derived from to create the one we are referencingas the child class.Child ClassA child class is a class that was derived from another, that will now be the parent class to it.
  7. 7. Example of a Parent ClassAs an example, suppose you are creating a game, something using different cars,and you need specific type of car for the policemen and another type for theplayer(s). Both car types share similar properties. The major difference (on thisexample case) would be that the policemen type would have sirens ontop of their cars and the players cars will not.
  8. 8. What is a private class?Private members of a class are accessible only from within other members of thesame class or from their friends.What is a protected class?Protected members are accessible from members of their same class and fromtheir friends, but also from members of their derived classes.What is a public class?Finally, public members are accessible from anywhere the object is visible.
  9. 9. This is our base class or parent class whatever you preferClass A{public: int apub;private: int aprv; // this wont be accessible to child/sub classesprotected: int apro;}Basic thumb rule in inheritance. You cannot inherit private members.
  10. 10. Public visibility mode during InheritancePublic inheritance will inherit protected variables & functions.Class B : public A // this is inheritance with private specifier{ public: int bpub; // also has access to aprv private: int bprv; // no access to apub protected: int bpro; // also has access to apro}here in class B you can use apub, bpub, apro, bpro but you cannotaccess aprv because aprv is private variable of class A.
  11. 11. Private visibility mode during Inheritanceprivate members of class are are not accessible but protected & public willbecome private members of class BSo anything that is public or protected in Class A will become private in classBClass B : private A // this is inheritance with private specifier{public:int bpub;private:int bprv; // also has access to apub,apro but now apub and apro are privatememebers of this classprotected:int bpro;}
  12. 12. Protected visibility mode during Inheritanceprotected inheritance will inherit protected as well as public variables& methods of base class (class A) with visibilty modifier as protectedClass B : protected A{public:int bpub;private:int bpriprotected:int bpro; // also has access to apub,apro}
  13. 13. Feel free to ask if there are any questions or queries Thank you Prepared by : Pooja K. Doshi

×