types - } // Enumerated Types void example2() { string...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
// types.cpp - demonstrate some basic C++ types / // modified 1/13/05 / #include <iostream> #include <string> using namespace std; u void example1() { bool ok = true; // true=1, false=0 char x = 'A'; // one byte unsigned char y = 255; // range 0 - 255 wchar_t ch = 0x0041; // 16-bit unicode (wide) character / int a = -2000000000; // signed, usually 32 bits unsigned int ua = 4200000000; // unsigned 32 bits long int b = -2000000000; // signed, size >= int unsigned long ub = 4200000000; // unsigned / float c = -2.4E+05; // typically 32 bits double d = 17.5; // typically 64 bits long double e = 1.2; // typically 80 bits / x = 127; // signed char accepts integer x++; // exceeds range of signed char, no warning w ok = 1; // no warning ok = 0; // no warning //ok = 36; // truncation warning / // a = c; // float to int truncation warning a = (int) c; // traditional cast a = int(c); // function-style cast a = static_cast<int>( c ); a //ok = c; // int to bool truncation warning
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } // Enumerated Types void example2() { string FruitNames = { &quot;orange&quot;, &quot;strawberry&quot;, &quot;apple&quot; }; enum Fruit { orange, strawberry, apple }; e Fruit F; F = apple; cout &lt;&lt; F &lt;&lt; endl; //2 cout &lt;&lt; FruitNames[ apple ] &lt;&lt; endl; // &quot;apple&quot; / //F = 2; // conversion error! / F = static_cast&lt;Fruit&gt;( 1 ); // force the conversion F = (Fruit) 1; // ok also F = Fruit( 1 ); // function-style cast / if( F == orange ) // direct comparison is OK cout &lt;&lt; &quot;F is orange&quot; &lt;&lt; endl; c int n = F + 1; // ok: auto promotion to int } //******** PASING ENUMS TO FUNCTIONS ********************** / enum Printer { scribe=101, fountain=56, appleJet=22 }; e void writeTo( Printer p, string msg ) { cout &lt;&lt; &quot;Writing &quot; &lt;&lt; msg &lt;&lt; &quot; to printer &quot; &lt;&lt; p &lt;&lt; endl; } void example3() { Printer G = fountain; writeTo( G, &quot;printed message&quot; ); } void types() { example1(); example2(); example3(); }...
View Full Document

This note was uploaded on 10/17/2010 for the course SCIS COP 4338 taught by Professor Kipirvine during the Spring '10 term at FIU.

Page1 / 2

types - } // Enumerated Types void example2() { string...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online