C++ Introduction

Intro to C++

C++ shares quite a few similarities with Java and other languages such as Python. It is a complex language with a very robust set of standard libraries.  But, we’ll start small and easy.

C++ is a general-purpose programming language emphasizing the design and use of type-rich, lightweight abstractions…C++ is a language for someone who takes the task of programming seriously.  Our civilization depends critically on software; it had better be quality software.” ~B. Stroustrup, Original Designer of C++, “The C++ Programming Language, Fourth Edition”, Addison- Wesley, 2013.

Important take aways from Overland: Chapter 1

I’ll use the same sample program that he introduces in section 1.1 to point out some important things in addition to Overland.

#include <iostream>
using namespace std;

int main ()
{
    cout << "Hello World";
    return 0;
}
  • This is a complete C++ program.
  • You’ll immediately notice something different from Java: there’s no class. C++ doesn’t require that you have a class in every program as Java does. So you can have functions/methods (whatever you want to call them; I don’t really care) that are “global” in a sense meaning that you don’t have to use a class reference to access them.
  • The #include directive is, in a sense, similar to the import directive of Java. Ultimately, it gives you access to code that lives somewhere else.
  • iostream is a library.  It contains “stuff” dealing with input and output.  In the main method, you see cout and the weird looking << operator.  Those are both defined in iostream.  cout stands for (I think) “console out”.  << is an operator – the official name, ready for this, stream insertion operator.
    • Think about a stream of characters “flowing” from your computer’s processor to your screen (screen identified by cout).
    • The operator << inserts characters into the stream it is pointed to.  That way, they’ll show up.
    • We’ll see a similar operator later that looks like this: >>.  Don’t get them mixed up.  🙂

Pay attention to the importance of the using directive as covered by Overland Section 1.4.  Section 1.5 introduces simple primitive variables.  There’s quite a bit of overlap between the primitive types in Java and c++.  cin is also introduced, which is related to cout, but allows you to read from the console (keyboard really).  Notice the >> operator – named the stream extraction operator.

Section 1.6 introduces basic control structures of C++.  Here’s the beauty: they’re exactly the same as they are in Java.  iffor, while, do…while – all act the same and get the same job done.

Section 1.7 mentions the concept of “global variable declarations”.  Don’t every use global data unless you can justify why the scope can’t be more narrow than global.  Pay attention to section 1.8 and 1.9 as well.

Speak Your Mind

*

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.