Question #186607

Write a program which has an abstract class called Number having an integer data member.


The class contains a pure virtual function called operation. A class called Armstrong is derived from


class called Number. Another class called Palindrome is derived from class Number. Use


appropriate constructors and redefine the function called operation to display if the number is


Armstrong number as well as Palindrome in case it falls in this category. You may make use of


other data members and member functions if needed


Expert's answer

#include <iostream>

class Number
{
protected:
	int m_number{ 0 };
public:
	Number(){};
	Number(int n)
	{
		m_number = n;
	}
	virtual void operation() = 0;
	
	void set_number(int n)
	{
		m_number = n;
	}
	int get_number() 
	{
		return m_number;
	}
};
class Palindrom :public Number
{
public:


	Palindrom() {}
	Palindrom(int n)
	{
		m_number = n;
	}
	void operation()
	{
		int digit{ 0 };
		int reverse{ 0 };
		int num{ m_number };
		do
		{
			digit = num % 10;
			reverse = (reverse * 10) + digit;
			num /= 10;
		} while (num != 0);
		if (reverse == m_number)
		{
			std::cout << m_number << " is Palindrom" << std::endl;
		}


	}
private:


};
class Armstrong:public Number
{
public:


	Armstrong(){}
	Armstrong(int n)
	{
		m_number = n;
	}
	void operation() 
	{
		int digit{ 0 };
		int arm { 0 };
		int num{ m_number };
		while(num !=0)
		{
			digit = num % 10;
			arm += pow(digit,3);
			num /=10;
		} 		
		if (arm == m_number) 
		{
			std::cout << m_number<<" is Armstrong number"<<std::endl;
		}
		
	}
private:


};


int main()
{
	// example work program
	Armstrong example;
	example.set_number(153);
	example.operation();
	Palindrom example_1(1221);
	example_1.operation();
	system("pause");
	return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS