Language: C++

#include <iostream>
#include <vector>
#include <memory>

using namespace std;

/***インターフェースクラス***/
class ICalc{
public:
	/***計算***/
	virtual void compute() = 0;
	/***表示***/
	virtual void print() = 0;
	virtual ~ICalc(){};
};

/***表示***/
class Print{
public:
	void print_all(vector<ICalc*> calc){
		for(auto& cal : calc){
			cal->print();
		}
		cout << endl;
	}
};

/***加算***/
class Add: public ICalc{
private:
	double a, b, ans;
public:
	Add(double a, double b): a(a), b(b), ans(0){};
	void compute( ){
		ans = a + b;
	}
	void print(){
		cout << ans << endl;
	}
};

/***減算***/
class Sub: public ICalc{
private:
	double a, b, ans;
public:
	Sub(double a, double b): a(a), b(b), ans(0){};
	void compute( ){
		ans = a - b;
	}
	void print(){
		cout << ans<< endl;
	}
};

/***乗算***/
class Mul: public ICalc{
private:
	double a, b, ans;
public:
	Mul(double a, double b): a(a), b(b), ans(0){};
	void compute( ){
		ans = a * b;
	}
	void print(){
		cout << ans<< endl;
	}
};

/***除算***/
class Div: public ICalc{
private:
	double a, b, ans;
public:
	Div(double a, double b): a(a), b(b), ans(0){};
	void compute( ){
		if(b != 0){
			ans = a / b;
		}
	}
	void print(){
		cout << ans << endl;
	}
};


int main()
{
	/***初期化***/
	vector<ICalc*> calc;
	ICalc* calc_p;
//	unique_ptr<ICalc> calc1;
	Print pt;

//	calc1 = make_unique<Add>(5,5);
//	calc1->compute();
//	calc1.reset(new Sub(5,5));
//	calc1->compute();
//	calc1.reset(new Mul(5,5));
//	calc1->compute();
//	calc1.reset(new Div(5,5));
//	calc1->compute();

	Add a1(5,5);
	Sub s1(5,5);
	Mul m1(5,5);
	Div d1(5,5);

	/***計算***/
	calc_p = &a1;
	calc_p->compute();
	calc_p = &s1;
	calc_p->compute();
	calc_p = &m1;
	calc_p->compute();
	calc_p = &d1;
	calc_p->compute();

	/***格納***/
	calc.push_back(&a1);
	calc.push_back(&s1);
	calc.push_back(&m1);
	calc.push_back(&d1);

	/***表示***/
	pt.print_all(calc);
}
//////////////
#include<stdio.h>

int add(int x , int y){
	return x + y;
}
int sub(int x , int y){
	return x - y;
}
int mul(int x , int y){
	return x * y;
}
int div(int x , int y){
	if(y != 0){
		return x / y;
	}
	return 0;
}


enum {ADD=0,SUB,MUL,DIV};

typedef int(*CALC[])(int,int);

void tmp(CALC calc){
	printf("%d",calc[2](1,5));
}
//void tmp(int(*calc[])(int,int)){
//	printf("%d",calc[2](1,5));
//}

int main(void){
	int x, y;
	x = 5;
	y = 5;


	/*関数ポインタ作成*/
//	int (*funcptr[])(int,int) = {add, sub ,mul, div};
//	tmp(funcptr);
	CALC cal = {add, sub ,mul, div};
	tmp(cal);

//	int ans = 0;
//	ans = funcptr[ADD](x,y);
//	printf("%d\n",ans);
//	ans = funcptr[SUB](x,y);
//	printf("%d\n",ans);
//	ans = funcptr[MUL](x,y);
//	printf("%d\n",ans);
//	ans = funcptr[DIV](x,y);
//	printf("%d\n",ans);

	int num;
	char* func_name[] = {"add","sub","mul","div"};
	int func_num = sizeof(func_name) / sizeof(func_name[0]);
	do{
		printf("pleas input num 0~3\n");
		printf("0:add 1:sub 2:mul 3:div\n");
		scanf("%d",&num);
	}while(num >= func_num || num < 0);

//	printf("%s:%d\n",func_name[num], funcptr[num](x,y));
	printf("%s:%d\n",func_name[num], cal[num](x,y));


	return 0;
}


Compiler output:
a.cc:140:5: error: ambiguating new declaration of 'int div(int, int)'
  140 | int div(int x , int y){
      |     ^~~
In file included from /usr/include/c++/14/cstdlib:79,
                 from /usr/include/c++/14/ext/string_conversions.h:43,
                 from /usr/include/c++/14/bits/basic_string.h:4154,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/stdlib.h:992:14: note: old declaration 'div_t div(int, int)'
  992 | extern div_t div (int __numer, int __denom)
      |              ^~~
a.cc:159:5: error: redefinition of 'int main()'
  159 | int main(void){
      |     ^~~~
a.cc:87:5: note: 'int main()' previously defined here
   87 | int main()
      |     ^~~~
a.cc: In function 'int main()':
a.cc:168:39: error: no matches converting function 'div' to type 'int (*)(int, int)'
  168 |         CALC cal = {add, sub ,mul, div};
      |                                       ^
/usr/include/c++/14/cstdlib:181:3: note: candidates are: 'ldiv_t std::div(long int, long int)'
  181 |   div(long __i, long __j) _GLIBCXX_NOTHROW { return ldiv(__i, __j); }
      |   ^~~
/usr/include/c++/14/cstdlib:219:3: note:                 'lldiv_t __gnu_cxx::div(long long int, long long int)'
  219 |   div(long long __n, long long __d)
      |   ^~~
/usr/include/stdlib.h:992:14: note:                 'div_t div(int, int)'
  992 | extern div_t div (int __numer, int __denom)
      |              ^~~
a.cc:182:30: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
  182 |         char* func_name[] = {"add","sub","mul","div"};
      |                              ^~~~~
a.cc:182:36: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
  182 |         char* func_name[] = {"add","sub","mul","div"};
      |                                    ^~~~~
a.cc:182:42: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
  182 |         char* func_name[] = {"add","sub","mul","div"};
      |                                          ^~~~~
a.cc:182:48: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
  182 |         char* func_name[] = {"add","sub","mul","div"};
      |                                                ^~~~~