Skip to content

Getting Started C++: A Very Simple Beginning

Getting Started C++: A Very Simple Beginning

C++ is not so horrible as someone describes.

C++ source file name and how to edit it

C++ source code is written, edited and saved as .cpp, .c++, .cxx, .cc, .C suffixes etc.

You can use any plain text editor, any IDE studio to create c++ source code file. A simple editor is very easy to use, and how to use a professional one is not discussed in this simple tutorial.

I prefer using vim without GUI on the terminal and console. Vim looks like a simple text editor on terminal and console, but it functions as a professional and powerful studio.

A bare C++ program

main.cpp

int main() {
}

The above code is very simple. It does "nothing", but it is really a complete program.

main() function is the entry point of C++ program.

Compile the c++ code

To compile C++ code, you need a c++ compiler.

Install C++ Compiler

On FreeBSD, C++ compiler clang is already integrated into the base system, so you don't have to install it.

On ArchLinux, you can install c++ compiler gcc with pacman:

$ su -
# pacman -Su gcc

Compile C++ Code

After you have a c++ compiler, you can compile it:

(FreeBSD)
$ clang++ main.cpp -o prog

(Arch Linux)
$ g++ main.cpp -o prog

Execute Your C++ Program

$ ./prog

Add print message feature into your C++ code: output

code

hello.cpp

#include <iostream>
int main() {
	std::cout << "Hello C++ World!" << std::endl;
}

Compile and Execute

$ clang++ hello.cpp -o hello
$ ./hello
Hello C++ World!

Explanation

• std:: ---- The C++ standard namespace, cout is "inside" it.

• cout ---- This is the object stands for output device, such as the (virtual) screen (terminal, console), an output pipe etc.

• << ---- The stream direction. Point to left: the stream flows from right to left. Point to right: the stream flows from left to right.

• #include <iostream> ---- To use the object std::cout, include the c++ STL standard header file: iostream.

Input: Read from the Input Device

Read Normal Variables

code

read.cpp

#include <iostream>

int main() {
	int val;
	std::cout << "Input an integer, When Finishing, press Enter Key: ";
	std::cin >> val;
	std::cout << "Your integer is: " << val << std::endl;
}

Compile and Execute

Explanation

• int val; ---- Define a variable, that can be used to hold integer value.

• cin ----- cin stands for the input device (virtually), such as the keyboard, input pipe, etc.

• std::cin >> val; ---- If you type an integer from the input device, and then press Enter key, the integer will "flow" from the input device to the variable val.

$ clang++ read.cpp -o read
$ ./read
Input an integer, When Finishing, press Enter Key: 512
Your integer is: 512

Read string variables

using std::cin

Same as above, but std::cin only reads the first word and ignores the left words, judging by space.

std::string str;
std::cin >> str;
std::cout << str << std::endl;

If you type "Hello, World!", str only stores "Hello," :

$ ./read
Hello, World!
Hello,

Using std::getline

std::getline is provided by iostream header too.

code: read.cpp

// This line is comment

#include <iostream>
#include <string> // provides std::string

int main() {
	std::string str;
	std::cout << "Input message and press enter: ";
	std::getline(std::cin, str);
	std::cout << "I got your message: " << str << '\n';
}

Compile and Execute

$ clang++ read.cpp -o read
$ ./read
Input message and press enter: Hello, C++ World!
I got your message: Hello, C++ World!

Explanation

• std::string str; ---- Declare a string variable str, used to hold string.

• std::getline(std::cin, str); ---- Read from input stream (input device) std::cin to variable str. Just as its name getline, it will read the whole line from input, the line end is judged by you press the Enter key.

C++ Object Oriented

C++ class and object

Class is used to create object. Object is the "real" object of the world. Class is the abstraction of the same objects.

Define a minimal C++ class:

class MyClass {
};

Define a C++ class with a constructor, a destructor, a property (member variable) and a method (member function):

class MyClass {
private:
	std::string name;	// This is property
public:
	// This is constructor
	MyClass(std::string getName) {
		name = getName;
	}
	// This is destructor
	~MyClass() {
	}
	// This is method.
	void print() {
		std::cout << name << std::endl;
	}
};

Create object using MyClass:

MyClass object{"Hello C++ World!"};

The Complete Code

object.cpp

#include <iostream>
#include <string>

class MyClass {
private:
	std::string name;	// This is property
public:
	// This is constructor
	MyClass(std::string getName) {
		name = getName;
	}
	// This is destructor
	~MyClass() {
	}
	// This is method.
	void print() {
		std::cout << name << std::endl;
	}
};

int main() {
	MyClass object{"Hello C++ World!"};
	object.print();
}

Compile object.cpp and Execute

$ clang++ object.cpp -o object
$ ./object
Hello C++ World!

Enable c++11, c++14, c++17, c++2a etc. on Your C++ Compiler

You do not need to wait many years to use modernize C++, and you do not need to be using modernize C++ after you become a professional c++ programmer.

You can use modernize C++ now even you are a new C++ rookie.

Enable c++17 on FreeBSD clang

(FreeBSD clang)
$ clang++ your-program.cpp -std=c++17 -o your-program

Enable c++17 on Arch Linux

(ArchLinux)
$ g++ your-program.cpp -std=c++17 -o your-program

COPYING

(c) Distributed Under the Beerware License by Fas Xmut

"THE BEER-WARE LICENSE" (Revision 42):
Fas Xmut wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return Fas Xmut.

Download nd format source of this document:

Getting-Started-C++.nd

 

 

 

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Form options

Submitted comments will be subject to moderation before being displayed.

ijku     SpartanCoin