Download code

Jump to: navigation, search

Back to Basic_constructs_(C_Plus_Plus)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

variables.cc

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Basic_constructs_(C_Plus_Plus)?oldid=16513
14 */
15 
16 #include <complex>
17 #include <iostream>
18 
19 // variables at global scope
20 int v(3);
21 int v2 = 3;
22 int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
23 
24 void f()
25 {
26   // variables at function scope
27   std::complex<double> z(2,3);
28   int a[] = { 1, 2, 3, 4, 5 };
29 }
30 int remember(int init)
31 {
32   static int var = init;
33   return var;
34 }
35 int main()
36 {
37   std::cout << remember(42) << "\n";
38   std::cout << remember(39) << "\n"; // outputs 42 again
39 }


hijacker
hijacker
hijacker
hijacker

do_nothing.cc

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Basic_constructs_(C_Plus_Plus)?oldid=16513
14 */
15 
16 int main(int argc, char* argv[])
17 {
18   return 0;
19 }
20 


hijacker
hijacker
hijacker
hijacker

failure.h

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Basic_constructs_(C_Plus_Plus)?oldid=16513
14 */
15 
16 #ifndef FAILURE_H_INCLUDED
17 #define FAILURE_H_INCLUDED
18 
19 int failure();
20 
21 #endif


hijacker
hijacker
hijacker
hijacker

io.cc

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Basic_constructs_(C_Plus_Plus)?oldid=16513
14 */
15 
16 #include <iostream>
17 
18 int main()
19 {
20   std::cout << "Please enter an integer value: ";
21   int value;
22   std::cin >> value;
23   std::cout << "The number you typed was " << value << std::endl;
24 }


hijacker
hijacker
hijacker
hijacker

hello.cc

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Basic_constructs_(C_Plus_Plus)?oldid=16513
14 */
15 
16 #include <iostream>
17 
18 int main()
19 {
20   std::cout << "Hello world!" << std::endl;
21 }


hijacker
hijacker
hijacker
hijacker

failure.cc

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Basic_constructs_(C_Plus_Plus)?oldid=16513
14 */
15 
16 #include "failure.h"
17 #include <cstdlib>
18 
19 int failure()
20 {
21   return EXIT_FAILURE;
22 }


hijacker
hijacker
hijacker
hijacker

build.log

1 /tmp/litprog8077409/variables.cc: In function 'void f()':
2 /tmp/litprog8077409/variables.cc:28:7: warning: unused variable 'a' [-Wunused-variable]


example2.cc

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Basic_constructs_(C_Plus_Plus)?oldid=16513
14 */
15 
16 #include <cstdlib>
17 
18 int main()
19 {
20   return EXIT_FAILURE;
21 }


hijacker
hijacker
hijacker
hijacker

vars

node some_node;
struct node some_other_node;
node initialized_node = { 7, &some_node };
vector3d diagonal = { 1, 1, 1 };
double diagonal_length = diagonal.length(); // sqrt(3)


types

char c = 'a';  // character type, guaranteed to have size of one byte
char c2 = 48;  // the type char actually is a numeric type (on systems using ASCII, this initializes c2 with '0'
signed char sc = -3; // one byte, signed (i.e. can take negative values)
unsigned char uc = 3; // one byte, unsigned (i.e. all values are non-negative)
// note that the type char has the same representation and value range as either signed char or unsigned char,
// but is a distinct type from both

wchar_t wc = L'a'; // wide character type

short s = -300s; // signed short integer; same type as signed short, short int, signed short int
unsigned short us = 300us; // unsigned short integer; same type as unsigned short int

int i = -1000; // signed integer; same type as signed int, signed
unsigned u = 1000u; // unsigned integer; same type as unsigned int

long l = -100000L; // long signed integer; same type as long int, signed long, signed long int
unsigned long ul = 100000UL; // long unsigned integer; same type as unsigned long int

bool b = false; // boolean variable; can only take the values false and true (but implicit conversion from/to int exists)

float f = 1.0f; // floating point variable
double d = 1.0; // floating point variable (usually double precision)
long double d = 1.0L; // floating point value (at least same precision as double, sometimes more)

// the following types are not in the current C++ standard, but a very common extension,
// part of C99, and will be part of next version of the C++ standard
long long ll = -1000000000LL; // very long integer, same as long long int, signed long long, signed long long int
unsigned long long ull = 1000000000ULL; // unsigned version of long long
int const ci = 3; // compile time constant
int const ci2 = some_function(); // constant, but initialized at run time
int volatile vi; // This may be visible from outside the program
int const volatile cvi; // The program may not change it, but external processes might
int* pi = &i; // pointer to int, initialized to point to i
int const* pci = &i; // pointer to constant int, initialized to i; cannot be used to change i
int* const cpi = &i; // constant pointer to int, initialized i; can be used to change i,
                     // but not reset to point somewhere else
int const* const cpci = &i; // constant pointer to constant int, pointing to i
int const* cpi2 = &ci; // pointer to constant int can (but need not) point to actually constant int
void* pv = pi; // pointer to void pointing to i; stores address, but loses type information
void const* pcv = pci; // constness matters also for pointer to void
int ai[3] = { 1, 2, 3 }; // array of 3 ints
int* api[3]; // array of 3 pointers to int
int (*pai)[3]; // pointer to array of 3 ints
int* (*pai)[5]; // pointer to array of 5 pointers to int
int a2[3][4]; // two-dimensional array; actually an array of 3 arrays of 4 ints each
int& ri = i; // gives a new name to i
int const& rci = ai[1]; // gives a name to the second member of the array ai;
                        // the name cannot be used to modify ai[1].
void foo(int);          // not a variable, but a function declaration
void (*pf)(int) = foo;  // a function pointer pointing to foo
void (**pf)(int) = &pf; // a pointer to pointer to function, pointing to pf
void (&rf)(int) = foo;  // a function reference to foo; seldom used,
                        // works identical to a const pointer
enum color { red, orange, yellow, green, blue, violet }; // defines type "color"
color background = green; // a variable of type color
enum flags { flag0 = 1, flag1 = 2, flag3 = 4, flag4 = 8, flag5 = 16 };
struct node
{
  int value;
  node* next;
};
struct node;
struct vector3d
{
  double x, y, z;
  double length();
};

double vector3d::length()
{
  return std::sqrt(x*x+y*y+z*z);
}

struct example_class
{
  example_class();
  int i;
};

example_class::example_class():
  i(3)
{
  std::cout << "default-initialized an object. i = " << i << "\n";
}


hijacker
hijacker
hijacker
hijacker

example3.cc

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Basic_constructs_(C_Plus_Plus)?oldid=16513
14 */
15 
16 #include "failure.h"
17 
18 int main()
19 {
20   return failure();
21 }


hijacker
hijacker
hijacker
hijacker