Home » C++ programming » C++ find output programs

# C++ Inheritance | Find output programs | Set 5

This section contains the **C++ find output programs with their explanations on C++ Inheritance (set 5)**.

Submitted by Nidhi, on July 05, 2020

**Program 1:**

#include <iostream> #include <string.h> using namespace std; class Fact { public: int factorial(int num) { int f = 1; for (int i = 2; i <= num; i++) { f = f * i; } return f; } }; class Power { public: int pow(int num, int p) { int c = 1; for (int i = 1; i <= p; i++) { c = c * num; } return c; } }; class Sample : Fact, Power { public: void calculate() { int A = 2, B = 3, NUM = 5; int RES = 0; RES = pow(A, B) * factorial(NUM); cout << RES << endl; } }; int main() { Sample S; S.calculate(); return 0; }

**Output:**

960

**Explanation:**

Here, we created three classes *Fact*, *Power*, and *Sample*. The Fact class contains a member function to calculate factorial of specified number; Power class contains a member function to calculate the power of a number.

Both *Fact* and *Power* class is inherited by *Sample* class and consumed the member functions of Both parent class.

In the *main()* function, we created the object of *Sample* class and call *calculate()* function that solves the mathematical expression using *factorial()* and *power()* function and prints **"960"** on the console screen.

**Program 2:**

#include <iostream> #include <string.h> using namespace std; class Fact { public: int factorial(int num) { int f = 1; for (int i = 2; i <= num; i++) { f = f * i; } return f; } }; class Power { public: int pow(int num, int p) { int c = 1; for (int i = 1; i <= p; i++) { c = c * num; } return c; } }; class Sample : Fact, Power { public: Sample() { cout << "Result: "; } void calculate() { int A = 2, B = 3, NUM = 5; int RES = 0; RES = pow(A, B) * factorial(NUM); cout << RES << endl; } }; int main() { Sample* S; S->calculate(); return 0; }

**Output:**

960

**Explanation:**

Here, we created three classes *Fact*, *Power*, and *Sample*. The *Fact* class contains a member function to calculate factorial of specified number; *Power* class contains a member function to calculate the power of a number.

Both *Fact* and *Power* class is inherited by *Sample* class and consumed the member functions of Both parent class.

In the *main()* function, we created the pointer of *Sample* class and call *calculate()* function that solves the mathematical expression using *factorial()* and *power()* function and prints **"960"** on the console screen.

Here, we used pointer without initializing it, so it will not call the constructor of the class, and also it is not the proper way. We must assign the address of the object to the pointer.

**Program 3:**

#include <iostream> #include <string.h> using namespace std; class Fact { public: int factorial(int num) { int f = 1; for (int i = 2; i <= num; i++) { f = f * i; } return f; } }; class Power { public: int pow(int num, int p) { int c = 1; for (int i = 1; i <= p; i++) { c = c * num; } return c; } }; class Sample { public: void calculate() { int A = 2, B = 3, NUM = 5; int RES = 0; Fact F; Power P; RES = P.pow(A, B) * F.factorial(NUM); cout << RES << endl; } }; int main() { Sample S; S.calculate(); return 0; }

**Output:**

960

**Explanation:**

Here, we created three classes *Fact*, *Power*, and *Sample*. The *Fact* class contains a member function to calculate factorial of specified number; *Power* class contains a member function to calculate the power of a number.

Here, we created the object of class *Fact* and *Power* inside the *calculate()* member function and calculate the mathematical expression and print the result on the console screen.

In the *main()* function, we created the object of *Sample* class and call *calculate()* function that print **"960"** on the console screen.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.