cout << first; after #include
<iostream>
string a, b;
a = "this is a";
b = "this is b";
if ( a < b )
cout << "a is smaller" << endl;
string name = "John Xhu";
cout << name << endl;
size_t space_index = name.find(' '); // size_t: type for value return by collections
cout << "Index of space: " << space_index << endl;
if ( space_index == string::npos )
cout << "Not found" << endl;
cout << "Initials: " << name[0] << name[space_index + 1] << endl;
todo.cpp
string operator[](int ix) { .... }
We will discuss this later!!
private
double average(int num[], int length);
void compute_balance(Account new_account);
// untested, from http://tinyurl.com/zy8scsu
#include <math.h>
double sqrt(double x) {
if (x <= 0)
return 0; // if negative number throw an exception?
int exp = 0;
x = frexp(x, &exp); // extract binary exponent from x
if (exp & 1) { // we want exponent to be even
exp--;
x *= 2;
}
double y = (1+x)/2; // first approximation
double z = 0;
while (y != z) { // yes, we CAN compare doubles here!
z = y;
y = (y + x / y) / 2;
}
return ldexp(y, exp / 2); // multiply answer by 2^(exp/2)
}
todo.cpp for
full version of ToDo class with test bed main
ToDo:
class ToDo {
public:
ToDo();
ToDo(string first_item);
bool empty();
bool full();
void add(string next_item);
string next_item();
// returns the next item capitalized; this is to illustrate converting
// a string to upper case; use tolower to convert to lower case.
string next_item_capitalized();
private:
static constexpr int MAXITEMS = 100;
string items[MAXITEMS];
int count;
};
ToDo::ToDo() : count{0} { }
ToDo::ToDo(string first_item) : count{1} {
items[0] = first_item;
}
bool ToDo::empty() {
return count <= 0;
}
bool ToDo::full() {
return count >= MAXITEMS;
}
void ToDo::add(string next_item) {
if ( full() )
throw "Adding item to full todo list.";
items[count] = next_item;
++count;
}
string ToDo::next_item() {
if ( empty() )
throw "Removing item from empty todo list.";
string result = items[0];
for(int i = 0; i < count - 1; ++i)
items[i] = items[i + 1];
--count;
return result;
}
string ToDo::next_item_capitalized() {
string item = next_item();
// note use of size_t to initialize i; this avoids warnings when
// comparing i to item.length() (since item.length() is generally unsigned)
for(size_t i = 0; i < item.length(); ++i)
item[i] = toupper(item[i]);
return item;
}
#include
#include <iostream>: include header file for
the iostream library
samples/modules folder)
g++ -c printave.cpp
g++ -c ave.cpp
dir # note the .o files
g++ ave.o printave.o
dir # now have a.exe
libraries
|
v
.--------. .cpp .----------. .o .--------.
| IDE |------->| compiler |------>| linker |--> .exe
`--------' `----------' `--------'
.h files libraries
| |
v compilation v
.--------. .cpp .-----. unit (.i) .----------. .o .--------.
| IDE |----->| CPP |-------------->| compiler |------>| linker |--> .exe
`--------' `-----' `----------' `--------'
^
|
other .o's
#include <math>
#include "print.h"
void main()
{
prdbl(sqrt(2.0));
}
double sqrt(double);
double sin(double);
// etc.
void prdbl(double);
g++ -E <x.cpp gives the compilation unit
double sqrt(double);
double sin(double);
// etc.
void prdbl(double);
void main()
{
prdbl(sqrt(2.0));
}
cpp printave.cpp > printave.i
iostream?
istream, ostream
cin, cout
cerr:
cerr << "This is a message the user needs to see!" << endl;
cerr << "Warning: the sky is falling." << endl;
dollar.h, dollar.cpp: code
for Dollar
dollar.h itself
#ifndef _dollar_h
#define _dollar_h
at the top of Dollar.h (in general,
use _file_h
for header file file) and
#endif
at the bottom
#include "x.cpp"
you're doing something wrong!
x.cpp twice and have two copies of each in the final executable
new with classesToDo
items;
void add_lunch(ToDo items) {
items.add("eat lunch");
}
ToDo today;
add_lunch(today);
will get no change - ToDo passed by value
void add_lunch(ToDo *items) {
items->add("eat lunch");
}
ToDo *today = new ToDo();
add_lunch(today);
new to create
-> to access data