Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,562 questions

52,727 answers

573 users

How to use class to calculate power in C++

1 Answer

0 votes
#include <iostream>

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

class Power {
	double base;
	int exp;
	double result;
public:
	Power(double _base, int _exp);
	double pow() {
		return result;
	}
};

Power::Power(double _base, int _exp)
{
	base = _base;
	exp = _exp;
	if (exp == 0)
		return;
	for (result = 1; exp > 0; exp--)
		result = result * base;
}

int main()
{
	Power o1(2, 4);
	cout << o1.pow() << endl;

	Power o2(3, 5);
	cout << o2.pow() << endl;

	return 0;
}




/*
run:

16
243

*/

 



answered Mar 27, 2018 by avibootz

Related questions

2 answers 61 views
1 answer 83 views
1 answer 111 views
111 views asked Jul 23, 2021 by avibootz
1 answer 166 views
1 answer 137 views
...