PlanetMath (more info)
 Math for the people, by the people.
Encyclopedia | Requests | Forums | Docs | Wiki | Random | RSS  
create new user
forget your password?
Main Menu
C++ (Definition)

C++ is an object-oriented programming language based on C. It was developed by Bjarne Stroustrup at Bell Labs in 1983, the same year that the ANSI C standard was ratified. Working in tandem, the ANSI and ISO ratified standard C++ in 1998.

The following C++ program takes two integers as inputs and outputs their greatest common divisor using Euclid's algorithm.

#include <iostream>

template<typename T>
T gcd(T x, T y) {
while ( y != 0 ) {
T z = x % y;
x = y;
y = z;
return x;

int main() {
int inputX, inputY;

std::cout << "Please enter x: ";
std::cin >> inputX;
std::cout << "Please enter y: ";
std::cin >> inputY;

std::cout << "The GCD of " << inputX
<< " and " << inputY
<< " is " << gcd(inputX,inputY) << std::endl;

return 0;

This program works pretty much the same as the corresponding C example. Instead of C's standard I/O library, the program uses C++'s iostream library. A substantial difference between the two programs is that the C++ version of gcd function is more general: not only can it work with integers of type int, the same code can work 1with any type that has division with remainder defined. The division with remainder corresponds to the operator % in C++.

Note that the algorithm, for integers, may return negative numbers for the greatest common divisor, if an input contains a negative. The algorithm is still correct, however, if the “greatest common divisor” is suitably defined, as in modern abstract algebra, to be unique only up to units in a ring. See [Stepanov] -- which is where the present example comes from -- for an interesting discussion around this point.

We could define another GCD function with the same name but that took three integers as input and returned the GCD of the trio. Or we could create our own complex integer object and then write a GCD function with the same name that took complex integers as input. Furthermore, we could define the operators so as to enable a programmer to write things like w = u + v; with complex integers rather something like w.setVal(complexAddition(u, v));. The programmer's imagination is the limit: the addition operator for a set object could be defined to either add up each element in one set to an element in a second set, or to perform a union of the two sets. Even in our simple GCD example, C++'s iostream library has overloaded the bitwise operators to perform concatenation.


Alexander Stepanov. Foreword to STL Tutorial and Reference Guide: C++ Programming with the Standard Template Library, second ed. Addison-Wesley, 2001.


... work1
Moreover, unlike most other object-oriented languages, this abtraction imposes absolutely no performance penalty at run-time.

Anyone with an account can edit this entry. Please help improve it!

"C++" is owned by PrimeFan. [ full author list (4) ]
(view preamble)

View style:

Other names:  C with Classes

Cross-references: overloaded, simple, union, addition, limit, object, complex, point, ring, units, algebra, contains, negative, operator, remainder, division, code, type, function, Euclid's algorithm, greatest common divisor, integers, C, language
There are 2 references to this entry.

This is version 7 of C++, born on 2007-03-23, modified 2007-03-27.
Object id is 9108, canonical name is C.
Accessed 251 times total.

AMS MSC68N15 (Computer science :: Software :: Programming languages)

Pending Errata and Addenda
[ View all 1 ]
Style: Expand: Order:
forum policy
Thanks for source code display fixes by PrimeFan on 2007-03-24 17:56:37
Thanks to Michael Slone and Steve Cheng for fixing the source code listings in the C and C++ entries. Yesterday I was trying several different things to get the source code to show up and none of it was working. I've been having trouble with the Web lately, I don't know why. Funny thing is, the day before yesterday I got C and C++ programs for GCD to work after just two tries each. The Javascript program took several dozen tries to get it to work, and this after I got C and C++ to work.

Also thanks to Steve Cheng for adding the data type generalization for gcd(x, y) in the C++ example. Of course I had to try compiling it on my computer. The first try had 19 errors (things like calling "cout" not a class object) and I forgot how many warnings, but that was only because I failed to notice that the pound sign had gotten transmogrified into something else along the way. The second try had just 3 errors, so I added some semicolons and lowercased "GCD" in main(). (I read somewhere that in Standard C++ you can omit semicolons for lines right before ending braces, but I can't recall where, but it wouldn't surprise me one bit if Microsoft's compiler wasn't entirely standards-compliant). On the third try, the program compiled successfully and gave all the right results for my customary test pairs, including (42, -21).

For (-21, 42), Steve's GCD program gives -21, which I think is the right answer (I don't have any rigorous mathematical reasoning for that, nor does Mathematica back me up on this: even for (-42, -21), Mathematica gives the answer as (+)21).
[ reply | up ]

rate | post | correct | update request | add derivation | add example | add (any)