Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Chapter No-03.Extending Classes Using Inheritance

The document discusses inheritance in object-oriented programming, explaining the concept of derived classes and the visibility modes (private, protected, public) that affect member access. It outlines different types of inheritance including single, multiple, hierarchical, multilevel, and hybrid inheritance, along with examples in C++. Additionally, it touches on the concept of virtual base classes to avoid duplication of inherited members in hybrid inheritance scenarios.

Uploaded by

vrushalikabade36
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter No-03.Extending Classes Using Inheritance

The document discusses inheritance in object-oriented programming, explaining the concept of derived classes and the visibility modes (private, protected, public) that affect member access. It outlines different types of inheritance including single, multiple, hierarchical, multilevel, and hybrid inheritance, along with examples in C++. Additionally, it touches on the concept of virtual base classes to avoid duplication of inherited members in hybrid inheritance scenarios.

Uploaded by

vrushalikabade36
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 18
fe O Chaplee No-3 Extending classes USi ~ Capa of dass te Fa _land chaeadea{sticS from anothet 1 A ]__base class fsapet ass + [ patent el Ee + - a Peeived dass [sub day” E ; ie [chides [Define _Detived class - ? i The class whith Inheeites {he membee of anotheé class }S cated as devived class @ scanned with OKEN Scanner felusy Aeviyed lass pune /oiblilyinede hyse day } name body of derpved elnes le ; | Heliieny frigdes The nisihy modes spaifies auhelhee® [he feahaees Bh the base classy ope pabtlenty dohedted, ae Velysbebe Teheeibed “14 can ie Pobive oP pelyte 4 lal agg pp 5 | | Vabiie yoid dleplay | | ' | | a logs B Public : Mbiig 1 wig | nyew ©) 1 4 a when metre defining Theohed So thal obgee fary access foth dass ll @ scanned with OKEN Scanner Q E State and explain the visibility modes used in inheritance. Visibility mo © private © protected public Base class Derived class visibility _ - vistilty [Protected | Public Not Not Not Inherited | Inherited _| Inherited. Protected | Private | Protected ected - Public | Private | Protected _| 1 + Privates © When a base class is privately inherited by a detived class, ‘public members" and ‘protected members’ of the base class become ‘private members’ of the derived lass. © Therefore, the public andl protected members of the base class can only be accessed by the member functions of derived class | bout, cannot be accessed by the objects of the derived class, | aan ~ Syntax: | class derived: private hase | { | JiMembers of derived class; }e Public: —— = | 0 When aba ublicly inherited by a derived class then | ‘Protected members’ of base class becomes. ‘protected | members’ and ‘public members" of the base class become ‘public members” of the derived elass, © Therefore the public members of the base class can be | ——— ‘uecessed by both the member functions of derived class as well 4s the objects of the derived cla: Syntax: class derived: public base { “Members of derived elass; hk © Protected: © When a base class is protetedly inherited by a derived cass, ‘Public and protected members’ of the base class become ‘protected members’ of the derived class, © Therefore the pul can be accessed by the member functions of derived clase ag Well as the member functions of immediate derived class of it but they cannot be accessed by the objects of derived clase Symtac: lass derived: protected base { “Members of derived class; K @ scanned with OKEN Scanner Pints, pe 6 | Sompre Program . Gindude CFostream hy painciude éconto. hy _clasS Sempre 4 Pubic: Veid dispey ¢ y Coup < Ob: disriory cy ~“gedehoos 2 oo en @ scanned with OKEN Scanner Tpratifie Takeelianre class whith Contains mote then bo s¢ Class jondonly one detived class | cus muskiPle cy inheettaunce - rien : i oo Lelass bose t a 3 ‘ = = _ - " Membet of dass base L areas Jeloss paseo i sane a 7 Mmembet Of dassbayo aT ss deeived + pub @ scanned with OKEN Scanner Pe —" co ay Mowing THREE of the fol —s | pt any sram to implement inheritance shown in Atte! write # CH prog! followin figure: ; ; 8 Ties Teacher | | Class Ans_| #include class Teacher { protected: char Name[20]; int empid; B class Student a { protected: char sname[20]; a int rollno; }; - a Info:public Teacher,public Student a public: void acceptT() u Ps cout<<"\nEnter data for teacher:"': cout<<"\nName:"; , cin>>Name; cout<<"\nEmployee id:"; ! cin>>empid; : @ scanned with OKEN Scanner void displayT() : t . cout<<"\nTeacher's data is:"; cout<<"\nName:"<>sname: cout<<"\nRoll no:"; cin>>rollno; } void displayS() { cout<<"\nStudent's data is:"; cout<<"\nName:"< using namespace std; classA //superclass A public: void show A() { cout<<"class A"< W | P PB a ; c a }} } ——~ consider @ hybrid inheritance as shown in the above diagram. The | itd class has two direct base classes, Parent! and Parent which ‘ themselves have a common base class as Grandparent. The child 7 ——— inherits the members of Grandparent via two separate paths, All the | © public and protected members of Grandparent are inherited into Child 7 — rice, first via Parent] and again via Parent2. This leads to duplicate \c fois ofthe inherited members of Grandparent inside Child class. The 7 ——~ Guplication of inherited members can be avoided by making the ¢ common base class as virtual base class while declaring the direct or £ intermediate base classes as shown below. 4 —— class Grandparent : peat class Parent! virtual public Grandparent ; { —h class Parent2:virtual public Grandparent — { b —— class Child: public Parent! public Parent B “| | Example oa Hinclude | #include ~| am student —lulmo; ae sea : @ scanned with OKEN Scanner | publ void getnumber() | \4 | cout<<"Enter Roll No:", | cin>>mo; ¢ \} | void putnumber() if c | cout<<"inint Roll No:"<>partl; cout<<"Part2:"; cin>>part2; ly 14 ie | void putmarks() { cout<<"\t Marks Obtained\n"; cout<<"\n\t Part] :"<>score; } void putscor @ scanned with OKEN Scanner S cout<<"\n\t Sports Score is:"<. poe ois a eat xc ST arn bo secia 2 hoe? a b sii 33 Ay ob: Show) yo Sf OO _ e geno, er Se —Bypek ———— ee ® scanned with OKEN Scanner

You might also like