It shouldn't be very difficult. You should have different paths while "cmaking" it. For example, you should specify different install paths using -DCMAKE_INSTALL_PREFIX= flag.
For example,
for opencv 2.4.9,
-DCMAKE_INSTALL_PREFIX=/path/to/opencv2.4.9/
while
for opencv 3.0.0
-DCMAKE_INSTALL_PREFIX=/path/to/opencv3.0.0
Do not confuse this path with source path.
And then real "magic" is in the makefile to compile your own code. Linking path and include paths have to be specified as per the opencv version you want.
For example : You have opencv 2.4.9 installed in /path/to/opencv2.4.9/lib/ and opencv 3.0.0 install in /path/to/opencv3.0.0/lib/ as per mentioned in that link, you will have makefile as per below:
Makefile for opencv 2.4.9
CPP = g++
# OpenCV 2.4.9
CPPFLAGS = -L/path/to/opencv2.4.9/lib \
-I/path/to/opencv2.4.9/include
all: test
test: main.cpp
$(CPP) $(CPPFLAGS) $^ -o $@
and Makefile for opencv 3.0.0
CPP = g++
# OpenCV 2.4.9
CPPFLAGS = -L/path/to/opencv3.0.0/lib \
-I/path/to/opencv3.0.0/include
all: test
test: main.cpp
$(CPP) $(CPPFLAGS) $^ -o $@
You might be confused as in that link, there are two CPPFLAGS defined in the same file.
Hope it clears your doubt.