Absolute Beginners C++


Summary & Conclusions

1. C++ Elements Covered

Structure of a C++ program // this is a comment
#include <iostream>
using namespace std;
int main()
{
     statements;
     return(0);
}
Console output cout << expression;
cout << expression << expression;
cout << endl;
Variable declaration int ival=1;
double dval=1.0;
string sval="one";
Arithmetic +, -, *, /, %, (, ), ++
Constants const double PI=3.14159;
Console input cin >> ival;
cin >> dval;
cin >> sval;
Converting between
double and int
(double)ival;
(int)dval;
String functions #include <string>
int index = sval.find(target);
int rindex = sval.rfind(target);
string s2=sval.substr(start,count);
string s3=sval.substr(start);
If statement if (conditional_expression) statement;
if (conditional_expression) {
     statements;
}
if (conditional_expression) {
     statements;
}
else {
     statements;
}
Conditional expressions <, <=, ==, =>, >, !=, &&, ||, !, (, ),
Assignment statement variable = expression;
Output formatting #include <iomanip>
setw(width)
setbase(base)
setprecision(numdigit)
setiosflags(ios::fixed)
setiosflags(ios::scientific)
setiosflags(ios::left)
sval.c_str()
While loop while (conditional_expression) {
     statements;
}
Functions return-type function-name ( argument_list )
{
     statements;
     return(expression)
}
For loops for (initial_statement ; conditional_expression ; loop_statement) {
     statements;
}
Vectors #include <vector>
vector<int> itable(size,initial_value);
vector<double> dtable(size,initial_value);
vector<string> stable(size,initial_value);
itable[index_expression]
itable.push_back(expression);
itable.size()
Ordering of vectors #include <algorithm>
sort(table.begin(),table.end());
random_shuffle(table.begin(),table.end());
Program arguments int main(int argc,char *argv[])
Text files #include <fstream>
ifstream ipchannel(filename);
ofstream opchannel(filename);
ipchannel.fail();
ipchannel.eof();
ipchannel.close();
ipchannel.get();
ipchannel.put(expression);

2. Where next?

The course has introduced enough C++ to write quite useful programs: consider the last exercise of lesson 12 for example.  However if you start to look at other peoples' programs you will rapidly find elements of C++ that we have not discussed at all.  Any comprehensive treatment of C++ statements will also include the 'do' loop and the 'switch' statement, the very useful 'break' statement, and assignment statements that update variables ('+=', '-=', etc).  However, it is possible to write satisfactory programs without any of these.

On the other hand, we have given no sensible information or advice about how to use C++ to build large and complex programs.  In particular, we have almost totally failed to explain how C++ allows you to create your own software 'objects': packages of code and data with a well-defined interface.  The advantage of writing your program as a collection of interacting objects is that changes to your program tend to have limited consequences: a bug fix in one object doesn't break five other things.  Objects are the basic units of 'modular design' in C++; they are also useful components for sharing code with others.  The 'cin' and 'cout' objects are good examples: they have been written by others and they provide us with useful functionality for console input and output, but we don't really need to know how they work inside.

So if you want to pursue C++ further, a good place to start would be to find a book that introduced 'Object Oriented Programming with C++'.  I am sure you will find the elements of C++ covered in this course a help. Some links are given on the overview page.



(c) 1999 Mark Huckvale University College London