Inheritance Part 1

 

Inheritance is the Capability of one class to inherit properties from another class.

Base class is a class whose properties are inherited by another class. It is also called as super class.

Derived Class is a class that inherits properties from base class. It is also called as sub class.

An abstract class is one that is not used to create objects and that act as a base class.

Advantages of inheritance:

·       Code reusability

·       Testing and debugging is easy

·       Coding is easy because similar type of codes and logics are used in inherited classes

·       Objects can be represented in different levels. i.e. hierarchical arrangement of objects

·       Efficient memory utilization

·       Faster development of codes and easy to extend

·       Easy maintenance

Defining Derived Classes:

Syntax:

class derived_class_name: visibility_mode base_class_name

{

            // member of derived class

};

Where class is a keyword, derived_class_name is the name of derived class, visibility_mode specifies the type of derivation, base_class_name is the name of base class.

Visibility mode:

Visibility modes are private, public, protected.

The visibility mode in the definition of the derived class specifies whether features of the base class are privately derived or publicly derived or protectedly derived.

If no visibility mode is specified, then by default the visibility mode is private.

Public inheritance:

In this, public member of base class becomes public member of derived class; private member of base class cannot be inherited to the derived class, protected member of base class stay protected in a derived class.

Ex:

class school

{

            private:

                        char name[20];

                        char address[20];

            public:

                        viod getdata();

                        void putdata();

};

class college:public school

{

            private:

                        char name[20];

                        char address[20];

            public:

                        viod getdata();

                        void putdata();

};

Private inheritance:

In this, public and protected members of base class becomes private member of derived class, private member of base class cannot be inherited to the derived class.

Ex:

class school

{

            private:

                        char name[20];

                        char address[20];

            public:

                        viod getdata();

                        void putdata();

};

class college:private school

{

            private:

                        char name[20];

                        char address[20];

            public:

                        viod getdata();

                        void putdata();

};

Protected inheritance:

In this, public and protected members of base class becomes protected member of derived class, private member of base class cannot be inherited to the derived class.

Ex:

class school

{

            private:

                        char name[20];

                        char address[20];

            public:

                        viod getdata();

                        void putdata();

};

class college:protected school

{

            private:

                        char name[20];

                        char address[20];

            public:

                        viod getdata();

                        void putdata();

};

Comments