×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

C++ Inheritance | Find output programs | Set 4

This section contains the C++ find output programs with their explanations on C++ Inheritance (set 4).
Submitted by Nidhi, on July 05, 2020

Program 1:

#include <iostream>
#include <string.h>
using namespace std;

class Class1 {
protected:
    void fun1()
    {
        cout << "Fun1() called" << endl;
    }
};

class Class2 : protected Class1 {
protected:
    void fun2()
    {
        cout << "Fun2() called" << endl;
    }
};

int main()
{
    Class2 OB;
    OB.fun1();
    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:24:13: error: ‘void Class1::fun1()’ is protected within this context
     OB.fun1();
             ^
main.cpp:7:10: note: declared protected here
     void fun1()
          ^~~~
main.cpp:24:13: error: ‘Class1’ is not an accessible base of ‘Class2’
     OB.fun1();
             ^

Explanation:

It will generate an error because we are trying to access the protected member of Class1 using the object of Class2. We can access protected members inside the child or derived class but we cannot access the protected member outside the class.

Program 2:

#include <iostream>
#include <string.h>
using namespace std;

class Class1 {
protected:
    void fun1()
    {
        cout << "Fun1() called" << endl;
    }
};

class Class2 : protected Class1 {
public:
    void fun2()
    {
        fun1();
        cout << "Fun2() called" << endl;
    }
};

int main()
{
    Class2 OB;
    OB.fun2();
    return 0;
}

Output:

Fun1() called
Fun2() called

Explanation:

Here, we created two classes Class1 and Class2. Here, protected members of Class1 inherited by Class2, and we called protected member function fun1() inside the fun2() function of Class2.

In the main() function, we created the object of class Class2 and call fun2() that is a public function, that’s why it can be accessed outside the class, and it called fun1() inside it. Then the above output will be printed on the console screen.

Program 3:

#include <iostream>
#include <string.h>
using namespace std;

class Math {
public:
    int add(int A, int B)
    {
        return (A + B);
    }
    int factorial(int num)
    {
        int f = 1;

        for (int i = 2; i <= num; i++) {
            f = f * i;
        }
        return f;
    }
};

class Sample : Math {
public:
    void calculate()
    {
        int A = 2, B = 3, NUM = 5;
        int RES = 0;

        RES = add(A, B) * factorial(NUM);

        cout << RES << endl;
    }
};

int main()
{
    Sample S;
    S.calculate();
    return 0;
}

Output:

600

Explanation:

Here, we created two classes Math and Sample; Math class is inherited by Sample class.

The Math class contains two member functions add() and factorial. The add() function is used to add two integer numbers and factorial() function is used to calculate the factorial of the specified number.

The Sample class contains a member function called calculate(), this member function calculates a mathematical expression using inherited functions and print the result using cout statement.

In the main() function, we created the object of Sample class and call calculate() function that solves the mathematical expression using add() and factorial() function and print "600" on the console screen.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.