Saturday, September 4, 2010

Object Oriented Language: C++, Basic I/O operations

I am writing this article to prepare myself for the upcoming interview with FactSet. As a warm up, I'll need to review all the basics, object oriented concepts, data structure and algorithms.

It's best to review by writing up a few easy and simple source codes.

I'll start off assuming the reader does not have any prior or little programming experiences.

The first thing everyone might notice in a programming book is how to write "Hello World!" message to the output device, the screen. I'll start off by providing a simple output statement with C++:
cout << "Hello World!";
This is the statement required for printing the message onto your screen. cout serves as the key word to output messages.

For a complete program, you would have to type the following:
#include 
using namespace std;

int main(){
    cout << "Hello World!";

    return 0; // It's a good practice to always return a 0 indicating the program executed without any interruption or problems.
}
Now that you have created your first program, save it as "HelloWorld.cpp". Compile it with your favorite compiler to see the results.

After you have seen the results, you might be curious what that rest of the code does. I will give you a brief overview of each statement.
#include 
This is a include statement, we included the iostream library. IOstream library is required for source files that are required to do basic input/output related operation. If you forget to include the required library, the compiler would not recognize the keyword "cout" as in this example.
using namespace std;
That is namespace. Just remember to type this statement for each source file you are going to write for now. We'll go into more details later on.
int main(){
This is the main function. It served as the entry point, the starting point for all programs. This function is mandatory by all programs. We will discuss function soon in the future.
return 0;
This is the return statement. For now, just remember to type this statement before you close the main function.
}
Finally, the closing curly braces. This closes the previous opening curly braces, which is the '{' after the main function. Everything inside the two braces are called a block of code.

There is one more thing in the source code that we haven't mention.
// It's a good practice to always return a 0 indicating the program executed without any interruption or problems.
The two forward slashes forms what we called, Single Line Comment. Everything that is in the same line and after the "//" will be ignored by the compiler and serve as notes to the developer. More ways of writing comments will be introduced in the near future.




We are done with the first program, but we are not done with the lesson yet.
We are going to learn how to take in messages from our user now. The trick is simple:
int daysInMonth;
cout << "How many days are there in a month?\n";
cin >> daysInMonth;
cout << "You entered " << daysInMonth << "." << endl;
There are a few new keywords here, I'll go over all of them briefly. First of all the int keyword.
int daysInMonth;
This is a variable declaration statement, it declare the integer variable. An int (integer) is a real number that can hold a number within ranges of: −2,147,483,648 to +2,147,483,647. We'll discuss more about variables in the next lesson. A variable can be thought of as a container that is holds a type and is subject to changes. daysInMonth in our case is an integer variable that is able to hold real number from −2,147,483,648 to +2,147,483,647.
cout << "How many days are there in a month?\n";
This should be familiar with you guys as I taught you about it a few paragraphs ago. Some of you with sharp eyes might have notice the difference with this statement. Yes, there is newline character, denoted by '\n'. This character tells the compiler to go to the next line and continue printing from there. As most of you would be better familiar with the "Enter" key in the notepad. The newline is created when you presses the key.
cin >> daysInMonth;
This is the statement that we are interested in this mini-program. The cin keyword, as some of you might have already figured out, takes in the input from the user and stores it in the daysInMonth variable.
cout << "You entered " << daysInMonth << "." << endl;
The last line of the program is an output operation with a little bit freshness. I'll walk you through the new codes. You didn't know you could do that with cout, did you? The left arrow arrow, "<<", means that we are concatenating or linking the the previous cstring, a string literal, or variable with the next. This linking is required for printing a variable and a cstring. cstring will be discuss when we talk about the string class. Finally, the endl keyword. This keyword creates a newline character just as '\n' does; same function, different approaches, use whichever you feel like to. For a complete view of the second program, "program2.cpp", it will look like the following code:
#include 
using namespace std;

int main(){
    int daysInMonth;
    cout << "How many days are there in a month?\n";
    cin >> daysInMonth;
    cout << "You entered " << daysInMonth << "." << endl;

    return 0; // Once again, please do not forget to include this line in your code.
}
I'll wrap the lesson up with the following:
cout << "outputs the message";
cin >> inputsTheMessage; // assuming the variable is declared. 
Some of you might be curious about the annoying semicolon, ';', I typed after each and every statement. As a matter of fact, this character is required and it signifies the end of statement for the compiler.

Enjoy and I'll write you some more tutorials tomorrow. :)
Give me some feedbacks if you find this helpful, or would like to give a suggestion or advice.

No comments:

Post a Comment