Derived Types |
|---|
// arrayone.cpp _ small arrays of integers
#include <iostream.h>
int main(void)
{
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;
}
// strings.cpp _ storing strings in an array
#include <iostream.h>
#include <string.h> // for the strlen() function
int main(void)
{
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;
}
// instr1.cpp _ reading more than one string
#include <iostream.h>
const int ArSize = 20;
int main(void)
{
char name[ArSize];
char dessert[ArSize];
cout << "Enter your name:\n";
cin >> name;
cout << "Enter your favorite dessert:\n";
cin >> dessert;
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
// instr2.cpp _ reading more than one word with getline
#include <iostream.h>
int main(void)
{
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
cout << "Enter your name:\n";
cin.getline(name, ArSize); // reads through newline
cout << "Enter your favorite dessert:\n";
cin.getline(dessert, ArSize);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
// instr3.cpp _ reading more than one word with get
#include <iostream.h>
int main(void)
{
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
cout << "Enter your name:\n";
cin.get(name, ArSize); // reads to newline
cout << "Enter your favorite dessert:\n";
cin.get(dessert, ArSize);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
// instr4.cpp _ reading more than one word with get() & get()
#include <iostream.h>
int main(void)
{
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
cout << "Enter your name:\n";
cin.get(name, ArSize).get(); // read string, newline
cout << "Enter your favorite dessert:\n";
cin.get(dessert, ArSize);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
// numstr.cpp -- following number input with line input
#include <iostream.h>
int main(void)
{
cout << "What year was your house built?\n";
int year;
cin >> year;
cout << "What is its street address?\n";
char address[80];
cin.getline(address, 80);
cout << "Year built: " << year << "\n";
cout << "Address: " << address << "\n";
return 0;
}
// structur.cpp -- a simple structure
#include <iostream.h>
struct inflatable // structure template
{
char name[20];
float volume;
double price;
};
int main(void)
{
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;
}
// assgn_st.cpp -- assigning structures
#include <iostream.h>
struct inflatable
{
char name[20];
float volume;
double price;
};
int main(void)
{
inflatable bouquet =
{
"sunflowers",
0.20,
12.49
};
// NOTE: some implementations may require using
// static inflatable bouquet =
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;
}
// address.cpp _ using the & operator to find addresses
#include <iostream.h>
int main(void)
{
int donuts = 6;
double cups = 4.5;
cout << "donuts value = " << donuts;
cout << " and donuts address = " << &donuts << "\n";
// NOTE: you may need to use unsigned (&donuts)
// and unsigned (&cups)
cout << "cups value = " << cups;
cout << " and cups address = " << &cups << "\n";
return 0;
}
// pointer.cpp _ our first pointer variable
#include <iostream.h>
int main(void)
{
int updates = 6; // declare a variable
int * p_updates; // declare pointer to an int
p_updates = &updates; // assign address of int to pointer
// NOTE: some users may need to type cast the addresses
// in cout statements to unsigned or unsigned long
// express values two ways
cout << "Values: updates = " << updates;
cout << ", *p_updates = " << *p_updates << "\n";
// express address two ways
cout << "Addresses: &updates = " << &updates;
cout << ", p_updates = " << p_updates << "\n";
// use pointer to change value
*p_updates = *p_updates + 1;
cout << "Now updates = " << updates << "\n";
return 0;
}
// init_ptr.cpp -- initialize a pointer
#include <iostream.h>
int main(void)
{
int higgens = 5;
int * pi = &higgens;
// NOTE: Some users may need to type cast the addresses
// in cout statements (&higgens and pi) to unsigned
cout << "Value of higgens = " << higgens
<< "; Address of higgens = " << &higgens << "\n";
cout << "Value of *pi = " << *pi
<< "; Value of pi = " << pi << "\n";
return 0;
}
// use_new.cpp _ using the new operator
#include <iostream.h>
int main(void)
{
int * pi = new int; // allocate space for an int
*pi = 1001; // store a value there
// NOTE: Some users may need to type cast the addresses
// in cout statements (pi and pd) to unsigned
cout << "int ";
cout << "value = " << *pi << ": location = " << pi << "\n";
double * pd = new double; // allocate space for a double
*pd = 10000001.0; // store a double there
cout << "double ";
cout << "value = " << *pd << ": location = " << pd << "\n";
cout << "size of pi = " << sizeof pi;
cout << ": size of *pi = " << sizeof *pi << "\n";
cout << "size of pd = " << sizeof pd;
cout << ": size of *pd = " << sizeof *pd << "\n";
return 0;
}
// arraynew.cpp _ using the new operator for arrays
#include <iostream.h>
int main(void)
{
double * p3 = new double [3]; // space for 3 doubles
p3[0] = 0.2; // treat p3 like an array name
p3[1] = 0.5;
p3[2] = 0.8;
cout << "p3[1] is " << p3[1] << ".\n";
p3 = p3 + 1; // increment the pointer
cout << "Now p3[0] is " << p3[0] << " and ";
cout << "p3[1] is " << p3[1] << ".\n";
return 0;
}
// addpntrs.cpp -- pointer addition
#include <iostream.h>
int main(void)
{
double wages[3] = {10000.0, 20000.0, 30000.0};
short stacks[3] = {3, 2, 1};
// NOTE: Some users may need to type cast the addresses
// in cout statements (ps and pw) to unsigned
// Here are two ways to get the address of an array
double * pw = wages; // name of an array = address
short * ps = &stacks[0]; // use address operator
// with array element
cout << "pw = " << pw << ", *pw = " << *pw << "\n";
pw = pw + 1;
cout << "add 1 to the pw pointer:\n";
cout << "pw = " << pw << ", *pw = " << *pw << "\n\n";
cout << "ps = " << ps << ", *ps = " << *ps << "\n";
ps = ps + 1;
cout << "add 1 to the ps pointer:\n";
cout << "ps = " << ps << ", *ps = " << *ps << "\n\n";
cout << "access two elements with array notation\n";
cout << stacks[0] << " " << stacks[1] << "\n";
cout << "access two elements with pointer notation\n";
cout << *stacks << " " << *(stacks + 1) << "\n";
cout << sizeof wages << " = size of wages array\n";
cout << sizeof pw << " = size of pw pointer\n";
return 0;
}
// pntstr.cpp _ using pointers to strings
#include <iostream.h>
#include <string.h> // declare strlen(), strcpy()
int main(void)
{
char animal[20] = "bear"; // animal holds bear
char * bird = "penguin"; // bird holds address of string
char * ps; // uninitialized
cout << animal << " and "; // display bear
cout << bird << "\n"; // display penguin
cout << ps << "\n"; // blunder - display garbage
cout << "Enter a kind of animal: ";
cin >> animal; // ok if input < 20 chars
cout << "Enter a kind of bird: ";
cin >> bird; // not recommended, may work on
// some systems, but behavior
// is undefined
// cin >> ps; Too horrible a blunder to try; ps doesn't
// point to allocated space
ps = animal; // set ps to point to string
cout << ps << "s!\n"; // ok, same as using animal
ps = bird; // reassign pointer
cout << ps << "s!\n"; // risky, same as using bird
ps = new char[strlen(animal) + 1]; // get new storage
strcpy(ps, animal); // copy string to new storage
cout << "Now ps points to " << ps << "!\n";
return 0;
}
// newstrct.cpp _ using new with a structure
#include <iostream.h>
struct inflatable // structure template
{
char name[20];
float volume;
double price;
};
int main(void)
{
inflatable * ps = new inflatable;// allot structure space
cout << "Enter name of inflatable item: ";
cin.get( (*ps).name, 20); // method 1 for member access
cout << "Enter volume in cubic feet: ";
cin >> ps->volume; // method 2 for member access
cout << "Enter price: $";
cin >> ps->price;
cout << "Name: " << ps->name << "\n";
cout << "Volume: " << ps->volume << " cubic feet\n";
cout << "Price: $" << ps->price << "\n";
return 0;
}
// delete.cpp _ using the delete operator
#include <iostream.h>
#include <string.h>
char * getname(void); // function prototype
int main(void)
{
char * name; // create pointer but no storage
// NOTE: some users may need to typecast name to
// unsigned instead of to (int *)
name = getname(); // assign address of string to name
cout << name << " at " << (int *) name << "\n";
delete [] name; // memory freed
name = getname(); // reuse freed memory
cout << name << " at " << (int *) name << "\n";
return 0;
}
char * getname(void) // return pointer to new string
{
char temp[80];
cout << "Enter last name: ";
cin >> temp;
char * pn = new char[strlen(temp) + 1];
strcpy(pn, temp); // copy string into smaller space
return pn; // temp lost when function ends
}