Data Structure and Algorithms based on the Object-oriented programming (OOP) where some are the base of OOP for instant inheritance, polymorphism, Encapsulation and Abstraction etc. we in the initial labs trying to get all important information about these mentioned concepts because this will help us in achieving out future goal while studying this Course.



Today’s Concept:

Inheritance using C++

 

Prerequisite of this Lab is all those students who have basic concepts on C++ Programming such as Syntax of Language, concept of functions, pointers, datatypes etc. Visual Studio 2012 and onward version will be used for implementation and programming tasks assigned in the Lab.

Inheritance is one of the major concepts in OOP in C++ and other programming languages as well. It enables us to create sub classes from an existing class we called it Based class and all sub classes we call them as Derived Class. All sub classes can inherit the functionalities from the Parent class (Base class) and can have additional features or functions to its own.


Syntax of Inheritance in C++.

class Library {   

    // issue_books_Date() function

    // Return_books_Date() function

};

 

class Finance : public Library {

    // fine_on_Students() function

};

In the above example we have created the base class named as Library and base class named as Finance which publicly inherits Library class. We can also use other keywords such as private and protected instead of public.

Inheritance is an Is-A relationship we always use inheritance where relation is same as we have mentioned earlier some are examples below:

Mango is a fruit.

Car is a vehicle.

A horse is a animal.

Inheritance using different access modifiers and their meanings

There is various way we can derived classes from the base class all ways can be possible using some access modes. All these modes have different effects when we implement inheritance using these modes:

Public: if a sub class is declared in public mode then the data member of parent class is inherited by the derived class just as they are.

Private: In this case all data members in the base class or parent class become private in the sub class or child class

Protected: the public members of the base class become protected members in the sub class. The private members of the base class are always private in the derived class.

Protected access modifier is especially relevant when we talk about inheritance in C++ programming language. When we use private access modifier we can not access data members from outside the class keeping that same in our mind for protected members too because it hold the same logic when we use protected data members will be inaccessible outside the class but they can be accessed by the derived classes and friend classes/functions.

Example using C++

#include <iostream>

using namespace std;

 

class A

{

    private:

        int one;

    protected:

        int two;  //can access by the derived class

    public:

        void setVal(int v)

        {

            two=v;   

        }

};

 

class B:private A

{

    public:

        void printVal(void)

        {

            setVal(10); //accessing public member function here

              //protected data member direct access here

            cout << "value of two: " << two << endl;

        }

};

 

int main()

{

        B objB; //derived class creation

        objB.printVal();

        return 0;

}

 


Lab Activity:

  1. Create a Program in C++ to implement inheritance using all information provided to you in the Lab manual.
  2. Create a program in C++ to implement inheritance between Students and person classes you are instructed to create two classes. Make person class as base class and student class derived class print all information of student in the main.
  3. Create a program in C++ to implement inheritance using Private access modifier please see the example about you can use the same code for your example.
  4. Using your understanding of relationship and inheritance create ATM system using C++. You are free to create as many classes as you want.

 Submission for the Lab Tasks:

                                       Click for Submission 




Post a Comment

Previous Post Next Post