Inheritance

Published on May 2016 | Categories: Documents | Downloads: 85 | Comments: 0 | Views: 571
of 19
Download PDF   Embed   Report

Comments

Content

€ The

mechanism of deriving a new class from an old one is called inhertitance(or derivation) € The old class is refered to as the Base class and new one is called the Derived class or subclass.

€ € €

Reusability: Inheritance helps the code to be reused in many situations. The base class is defined and once it is compiled, it need not be reworked. Using the concept of inheritance, the programmer can create as many derived classes from the base class as needed while adding specific features to each derived class as needed. Saves Time and Effort: The above concept of reusability achieved by inheritance saves the programmer time and effort. Since the main code written can be reused in various situations as needed.

€ €

ƒ

There are five types of inheritance. 1. Single inheritance 2. Multilevel inheritance 3. Multiple inheritance 4. Hierarchical inheritance 5. Hybrid inheritance

Base class
Student Shape

Derived classes
GraduateStudent UndergraduateStudent Circle Triangle Rectangle CarLoan HomeImprovementLoan MortgageLoan FacultyMember StaffMember CheckingAccount SavingsAccount

Loan

Employee Account

€a

derived class is any class that inherits from any other derived class or base class. € Syntax:
€ Class

derived-class-name : visibility-mode base-class-name €{ }

Class ABC:private XYZ { Members of abc }; Class ABC:public XYZ { Members of abc }; Class ABC: XYZ { Members of abc };



Class inherits from one base class

A

B

using namespace std; Class B { Int a; Public: Int b; Void get_ab(); Int get_A(void); Void show_a(void); }; Class D:public B { Int c; Public: Void mul(void); Void display(void); }; Void B::get_ab(void) { A=5;b=10; } Int B::get_a() { Returrn a; }
Void D:: mul() {c=b*get_a(); } Void D::display() { Cout<<´a =´<<get_a()<<´nµ; Cout<<´b =´<<b<<´nµ; Cout<<´c =´<<c<<´nµ; } Int main() { D d; d.get_ab(); d.mul(); d.show_a() d.display(); d.b=20; d.mul(); d.display(); Return 0; }

€ A=5 € A=5 € B=10 € C=50

€ A=5 € B=20 € C=100

€A

third visibility modifier,protected,which serve a limited purpose in inheritance. € A member declared as protected is accessible by the member function within its class and any class immediately dervied from it. € It cannot be acesses by the functions outside two classes

Class alpha { Private: Protected: Public: }

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close