Unit 4 Inheritance: Tanima Thakur UID-23532
Unit 4 Inheritance: Tanima Thakur UID-23532
Unit 4 Inheritance: Tanima Thakur UID-23532
Inheritance
Tanima Thakur
UID- 23532
C++ Inheritance
One of the most important concepts in object-oriented
programming is that of inheritance. Inheritance allows us to
define a class in terms of another class, which makes it easier to
create and maintain an application. This also provides an
opportunity to reuse the code functionality and fast
implementation time.
The derived classes have all the features of the base class and
the programmer can choose to add new features specific to the
newly created derived class.
Features or Advantages of Inheritance:
Reusability:
Inheritance helps the code to be reused in many situations.
For example, if the base class is MyClass and the derived class is
sample it is specified as:
The above makes sample have access to both public and protected
variables of base class MyClass
Reminder about public, private and protected access specifiers:
class MyClass
{ public:
MyClass(void) { x=0; }
void f(int n1)
{ x= n1*5;}
void output(void) { cout<<x; }
private:
int x;
};
Inheritance Example:
int main(void)
{ sample s;
s.f(10);
s.output();
s.f1(20);
s.output();
}
Class A
Class A
Class B Class C
Class D