Top Banner
Data types Fundamental data types Integer, floating point, character Derived data types – Arrays – Strings – Structures
16

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

Jan 16, 2016

Download

Documents

Willis Young
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

Data types

• Fundamental data types– Integer, floating point, character

• Derived data types– Arrays

– Strings

– Structures

Page 2: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

Fundamental data types

• Integer types– short, int, long

• Character type– char

• Boolian type– bool

• Floating-point types– float, double, long double

Page 3: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

Integer limits

Page 4: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

Integer limits

// limits.cpp -- some integer limits#include <iostream>using namespace std;#include <climits> // use limits.h for older systemsint 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;}

Page 5: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

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 valueint 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}

Page 6: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

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;}

Page 7: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

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;

}

Page 8: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

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;}

Page 9: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

Derived data types

• Arrays– static, dynamic

• Strings• Structures

Page 10: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

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;}

Page 11: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

Strings

// strings.cpp _ storing strings in an array#include <iostream>#include <cstring> // for the strlen() functionusing 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;}

Page 12: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

Strings

Page 13: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

Structures

Page 14: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

Structures// 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;

}

Page 15: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

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;}

Page 16: Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.

Local and external declarations