C++ First program
Where do I start?
The most effective way I know is to learn by doing. Here are my recommended steps.
download a software that allows you to practice writing C++ programs.
There are two popular free software (it is called IDE -- Integrated Development Environment). One is Microsoft Visual C++ Express. It comes with a compiler already. If you're running Windows, this is the easiest way.
The other popular IDE is Eclipse. Eclipse was originally designed for Java. However, it is well adapted to C++. When you search for download, try "eclipse CDT" (C Development Tooling). Many good installation guides are readily available, one example here.
get yourself comfortable (well, a little bit) with the IDE
Run the built-in "hello world" program. Both Microsoft and Eclipse come with a good tutorial. No need to finish them all though.
go to cplusplus.com and follow the tutorials (Basics of C++) there.
you can follow variety of online tutorial, or you can ride with us. Try the following exercise.
Create a project with simple hello to cout
main() {
cout << "hello";
}
Why it won't compile?
can not find cout
Where is cout? How do we tell compiler about it?
#include <iostream>
Why it still won't compile?
std::cout
Good, it compiles. But do I always have to use std::count? What if I have a lot of cout?
using namespace std;
What is namespace?
How do I know the program runs?
breakpoint
single step
It is VERY important to learn the debugging skill
#include <iostream>
using namespace std;
void main()
{
int i=12;
int k;
float f=1.23456;
cout << sizeof(f); // you can find out how many BYTES are used for a data type
cout << "hello, ana";
cout << "sdfjldsfkjdsf";
cin >> i, k;
cout << i;
cout << i << k << f;
}
Let's add a few more things to the simple program above.
How about different data types (int, unsigned, short, double, float, bool, char, etc) and variable declaration with initialization?
int a=5, b=2, c; // a variable needs to be defined, but not necessarily initialized
float f;
bool flag;
char tab = '\t';
Some operators can be cascaded, e.g. << , do you know why?
cout << "Hello frank " << a << " *** \t" << c << endl;
What is the \t thing? What does it do? Are there other escape sequences? Try \n, \a, \\
How do we repeat something in a loop? For loop is one of the most used form. The other popular one is while.
for (int i=1; i<5; i++) {
cout << "Hello frank" << i << endl;
if (i==c) cout << "find it" << endl;
}
Notice that you can create new local variables (int i ) inside the scope of for loop. Try cout << i after the for loop. That is the scoping rule.
How about conditional? The famous if..then..else.
if (i==c) cout << "find it" << endl;
Once you know the conditional, it is good time to know the boolean data type bool.
if (f == c) flag=true;
else flag=false;
alternatively, one can do
flag = (f!=c);
is it the same as
flag = f != c;
What is "operator precedence"?
Did you notice that c=a/b; and f = a / b; yield the same result even though one is integer, the other is float? How come?
#include <iostream>
using namespace std;
int main() {
int a=5, b=2, c;
float f;
bool flag;
char tab = '\t';
cout << "Hello frank " << a << " *** \t" << c << endl;
c=a/b;
cout << "c = \t" << c << " or using " << tab << endl;
for (int i=1; i<5; i++) {
cout << "Hello frank" << i << endl;
if (i==c) cout << "find it" << endl;
}
f = a / b;
cout << f << endl;
if (f == c) flag=true;
else flag=false;
cout << flag << endl;
flag = (f!=c);
cout << flag << endl;
return 0;
}