1 / 16

Data types

Fundamental data types Integer, floating point, character Derived data types Arrays Strings Structures. Data types. Integer types short, int, long Character type char Boolian type bool Floating-point types float, double, long double. Fundamental data types. Integer limits.

channer
Download Presentation

Data types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fundamental data types Integer, floating point, character Derived data types Arrays Strings Structures Data types

  2. Integer types short, int, long Character type char Boolian type bool Floating-point types float, double, long double Fundamental data types

  3. Integer limits

  4. Integer limits // limits.cpp -- some integer limits #include <iostream> using namespace std; #include <climits> // use limits.h for older systems int main() { int n_int = INT_MAX; // initialize n_int to max int value short n_short = SHRT_MAX; // symbols defined in limits.h file long n_long = LONG_MAX; // sizeof operator yields size of type or of variable cout << "int is " << sizeof (int) << " bytes.\n"; cout << "short is " << sizeof n_short << " bytes.\n"; cout << "long is " << sizeof n_long << " bytes.\n\n"; cout << "Maximum values:\n"; cout << "int: " << n_int << "\n"; cout << "short: " << n_short << "\n"; cout << "long: " << n_long << "\n\n"; cout << "Minimum int value = " << INT_MIN << "\n"; return 0; }

  5. Exceeding integer limits // exceed.cpp -- exceeding some integer limits #include <iostream> using namespace std; #define ZERO 0 // makes ZERO symbol for 0 value #include <climits> // defines INT_MAX as largest int value int main() { short sam = SHRT_MAX; // initialize a variable to max value unsigned short sue = sam;// okay if variable sam already defined cout << "Sam has " << sam << " dollars and Sue has " << sue; cout << " dollars deposited.\nAdd $1 to each account.\nNow "; sam = sam + 1; sue = sue + 1; cout << "Sam has " << sam << " dollars and Sue has " << sue; cout << " dollars deposited.\nPoor Sam!\n"; sam = ZERO; sue = ZERO; cout << "Sam has " << sam << " dollars and Sue has " << sue; cout << " dollars deposited.\n"; cout << "Take $1 from each account.\nNow "; sam = sam - 1; sue = sue - 1; cout << "Sam has " << sam << " dollars and Sue has " << sue; cout << " dollars deposited.\nLucky Sue!\n"; return 0 }

  6. Arithmetic // arith.cpp -- some C++ arithmetic #include <iostream> using namespace std; int main() { cout.setf(ios_base::fixed, ios_base::floatfield); // fixed-point float hats, heads; cout << "Enter a number: "; cin >> hats; cout << "Enter another number: "; cin >> heads; cout << "hats = " << hats << "; heads = " << heads << "\n"; cout << "hats + heads = " << hats + heads << "\n"; cout << "hats - heads = " << hats - heads << "\n"; cout << "hats * heads = " << hats * heads << "\n"; cout << "hats / heads = " << hats / heads << "\n"; return 0; }

  7. Character variables // chartype.cpp -- the char type #include <iostream> using namespace std; int main( ) { char ch; // declare a char variable cout << "Enter a character:\n"; cin >> ch; cout << "Holla! "; cout << "Thank you for the " << ch << " character.\n"; return 0; }

  8. Type cast // typecast.cpp -- forcing type changes #include <iostream> using namespace std; int main() { int auks, bats, coots; // the following statement adds the values as double, // then converts the result to int auks = 19.99 + 11.99; // these statements add values as int bats = (int) 19.99 + (int) 11.99; // old C syntax coots = int (19.99) + int (11.99); // new C++ syntax cout << "auks = " << auks << ", bats = " << bats; cout << ", coots = " << coots << '\n'; char ch = 'Z'; cout << "The code for " << ch << " is "; // print as char cout << int(ch) << '\n'; // print as int return 0; }

  9. Arrays static, dynamic Strings Structures Derived data types

  10. Arrays // arrayone.cpp _ small arrays of integers #include <iostream> using namespace std; int main() { int yams[3]; // creates array with three elements yams[0] = 7; // assign value to first element yams[1] = 8; yams[2] = 6; int yamcosts[3] = {20, 30, 5}; // create, initialize array // NOTE: If your C++ compiler or translator can't initialize // this array, use static int yamcosts[3] instead of // int yamcosts[3] cout << "Total yams = "; cout << yams[0] + yams[1] + yams[2] << "\n"; cout << "The package with " << yams[1] << " yams costs "; cout << yamcosts[1] << " cents per yam.\n"; int total = yams[0] * yamcosts[0] + yams[1] * yamcosts[1]; total = total + yams[2] * yamcosts[2]; cout << "The total yam expense is " << total << " cents.\n"; cout << "\nSize of yams array = " << sizeof yams; cout << " bytes.\n"; cout << "Size of one element = " << sizeof yams[0]; cout << " bytes.\n"; return 0; }

  11. Strings // strings.cpp _ storing strings in an array #include <iostream> #include <cstring> // for the strlen() function using namespace std; int main() { const int Size = 15; char name1[Size]; // empty array char name2[Size] = "C++owboy"; // initialized array // NOTE: some implementations may require the static keyword // to initialize the array name2 cout << "Howdy! I'm " << name2; cout << "! What's your name?\n"; cin >> name1; cout << "Well, " << name1 << ", your name has "; cout << strlen(name1) << " letters and is stored\n"; cout << "in an array of " << sizeof name1 << " bytes.\n"; cout << "Your initial is " << name1[0] << ".\n"; name2[3] = '\0'; // null character cout << "Here are the first 3 characters of my name: "; cout << name2 << "\n"; return 0; }

  12. Strings

  13. Structures

  14. // structur.cpp -- a simple structure #include <iostream> using namespace std; struct inflatable // structure template { char name[20]; float volume; double price; }; int main() { inflatable guest = { "Glorious Gloria", // name value 1.88, // volume value 29.99 // price value }; // guest is a structure variable of type inflatable // It's initialized to the indicated values inflatable pal = { "Audacious Arthur", 3.12, 32.99 }; // pal is a second variable of type inflatable // NOTE: some implementations require using // static inflatable guest = cout << "Expand your guest list with " <<guest.name; cout << " and " << pal.name << "!\n"; // pal.name is the name member of the pal variable cout << "You can have both for $"; cout << guest.price + pal.price << "!\n"; return 0; } Structures

  15. Assigning structures // assgn_st.cpp -- assigning structures #include <iostream> using namespace std; struct inflatable { char name[20]; float volume; double price; }; int main() { inflatable bouquet = { "sunflowers", 0.20, 12.49 }; inflatable choice; cout << "bouquet: " << bouquet.name << " for $"; cout << bouquet.price << "\n"; choice = bouquet; // assign one structure to another cout << "choice: " << choice.name << " for $"; cout << choice.price << "\n"; return 0; }

  16. Local and external declarations

More Related