adding the multiply example
This commit is contained in:
26
lectures/14_stacks_queues/multiply.cpp
Normal file
26
lectures/14_stacks_queues/multiply.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <iostream>
|
||||
|
||||
// functor class
|
||||
class MultiplyBy {
|
||||
private:
|
||||
int factor;
|
||||
|
||||
public:
|
||||
// constructor
|
||||
MultiplyBy(int factor) : factor(factor) {}
|
||||
|
||||
// overloaded function call operator
|
||||
int operator()(int x) const {
|
||||
return x * factor;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
// create an instance of the functor
|
||||
MultiplyBy multiplyByTwo(2);
|
||||
|
||||
// use the functor as a function
|
||||
std::cout << "Result of multiplying 5 by 2: " << multiplyByTwo(5) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user