请输入您要查询的字词:

 

单词 C
释义

C++


++ is a compiled multi-paradigm programming language based on C that supportsMathworldPlanetmath procedural, object-oriented and generic programming paradigms. 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 divisorMathworldPlanetmath using Euclid’s algorithmMathworldPlanetmath.

#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 asthe corresponding C example (http://planetmath.org/CProgrammingLanguage).Instead of C’s standard I/O library, the program usesC++’s iostream library.A substantial differencePlanetmathPlanetmath between the two programsis that the C++ version of gcd function is more general:not only can it work with integers of type int,the same code can work11Moreover, unlike most other object-oriented languagesPlanetmathPlanetmath,this abtraction imposes absolutely no performance penalty at run-time.with any type that hasdivision with remainder defined.The division with remainder corresponds to the operator % in C++.

Note that the algorithm, for integers, may return negativenumbers 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 additionPlanetmathPlanetmath 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 concatenationMathworldPlanetmath.

References

  • Stepanov Alexander Stepanov. http://www.stepanovpapers.com/MusserForeward.pdfForeword to STL Tutorial and Reference Guide: C++ Programming with the Standard Template Library, second ed. Addison-Wesley, 2001.
随便看

 

数学辞典收录了18232条数学词条,基本涵盖了常用数学知识及数学英语单词词组的翻译及用法,是数学学习的有利工具。

 

Copyright © 2000-2023 Newdu.com.com All Rights Reserved
更新时间:2025/5/4 12:05:55