How to create class constructor with different parameter type in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

class Test {
	int n;
public:
	Test(int x) {
		n = x;
	}
	Test(char *s) {
		n = atoi(s);
	}
	int f() {
		return n;
	}
};

int main()
{
	Test o1 = 43;
	Test o2 = "982";

	cout << o1.f() << endl;
	cout << o2.f() << endl;

	return (0);
}


/*
run:

43
982

*/

 



answered Feb 23, 2018 by avibootz

Related questions

2 answers 219 views
2 answers 185 views
1 answer 125 views
2 answers 156 views
156 views asked Jan 29, 2023 by avibootz
1 answer 145 views
...