Posts Tagged 'C++'

C++ Language Tutorial — Basics of C++: Structure of a Program

Initially, I looked at C++ Language Tutorial by Juan Soulié, but I found Thinking in C++ by Bruce Eckel recommended on the KDE development page, so I am going to read both.

At first glance, Eckel’s book looks like a really heavy read. If I may say so, it’s very… hard-core? Perhaps bleak? Okay, I may be biased toward pretty, colorful code blocks used in the C++ Language *Tutorial*, but really—Eckel’s first chapter is a slightly ominous, philosophical “Introduction to Objects.” The chapters are much more detailed than Soulié’s tutorial (after all, it’s just that, a tutorial).

Hopefully, I’ll be able to pluck up some courage to read that chapter once I’ve learned a reasonable amount of syntax and structure. It seems like a great book, but it looks way too scary right now.

So getting started with the tutorial:

A basic C++ program as a model, copied from the site:

// my first program in C++

using namespace std;

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

All text after two slash signs (//) are comments. Block comments can be inserted between /* and */.

Lines beginning with a hash sign (#) are directives for the preprocessor. In this example, #include tells the preprocessor to include the iostream file, which is the basic input-output library in C++ (which defines cout).

“All the elements of the standard C++ library are declared within what is called a namespace… with the name std.” We call it with using namespace std, and it is frequently used in C++ programs that use the standard library.

int main () is the beginning of the definition of the main function, which is where all C++ programs start their execution. Other functions may be defined elsewhere and anywhere, but the main function will be executed first (thus is essential to every C++ program). The parentheses can optionally enclose a list of parameters. The body of the function is enclosed in a pair of braces.

A statement is an expression that can produce an effect. cout << "Hello World!"; outputs the string of characters Hello World! into the standard output (e.g., the screen). All expression statements are terminated by a semicolon.

The return statement in this example is followed by a return code of 0, which generally indicates a successful execution of the program. C++ console programs are usually ended in this way.

With the exception of lines of preprocessor directives (they are not statements), whitespace is insignificant. Semicolons are important.

Learning Common Lisp

Many online sources say that Lisp is a “programmable programming language.” Defining my own macros, being creative, and all that sounded a lot like the Art of Problem Solving spirit. So I decided to learn it. I’m reading Practical Common Lisp by Peter Seibel. The examples make things clear. I can’t say much else because I’m still a novice.

From “If programming languages were cars…”:

Lisp: looks like a car, but with enough tweaking you can turn it into a pretty effective airplane or submarine.

[from Paul Tanimoto:]

Lisp: At first it doesn’t seem to be a car at all, but now and then you spot a few people driving it around. After a point you decide to learn more about it and you realize it’s actually a car that can make more cars. You tell your friends, but they all laugh and say these cars look way too weird. You still keep one in your garage, hoping one day they will take over the streets.

And from Paul Graham, author of On Lisp, another ostensibly good book:

Lisp code looks weird. But those parentheses are there for a reason. They are the outward evidence of a fundamental difference between Lisp and other languages.

For good measure, other entries from “If programming languages were cars…”:

Python is a great beginner’s car; you can drive it without a license. Unless you want to drive really fast or on really treacherous terrain, you may never need another car.

C++ is a souped-up version of the C racing car with dozens of extra features that only breaks down every 250 miles, but when it does, nobody can figure out what went wrong.

I hope I’m getting somewhere with all this.

I’m learning C++!

I thought it’d be nice to learn C++ alongside Python, so I submitted the same algorithm for SPOJ project code TEST in C++:

#include <iostream>
#include <cstdlib>

using namespace std;

int main () {
	int num;
	num = 0;
	while (num != 42) {
		cin >> num;
		if (num == 42) {
			return 0;
		cout << num << '\n';
	return 0;

w00t yeah. Intrepid comes with a newer version of libtools than KDevelop currently has, so it wouldn’t compile correctly at first. I found this useful post to help me out.

btw, for the purposes of this post, I indented the code above using lots of &nbsp;, which is really annoying. Does anyone have a better solution?