Getting Started
This chapter introduces most of the basic elements of C++: types, variables,expressions, statements, and functions.
I write this post in 2021, today there are many powerful tools can help you write program more efficiently and quickly, I recommend you use vs2019 and create a “Linux project” and running in a virtual machine. you can simply create a virtual machine by VMware.
this post is based on c plus plus primer.
write a simple c++ program
Every c++ program contains one or more functions, one of which must be named main. The operating system runs a C++ program by calling main, this is a simple c++ program.
#include<cstdio>
int main()
{
printf("hello world!\n");
return 0;
}
A function definition has four elements: a return type, a function name, a (possibly empty) parameter list enclosed in parentheses, and a function body.
In previous example, the int is return type, main is function name, and don’t have parameter. The function body is a block of statements starting with an open curly brace and ending with a close curly.
Note the semicolon at the end of statement, it can lead to compiler error messages when forgotten.
Input and output
The c++ don’t define any statement to do input and output, but c++ include a extensive standard library that provide input and output. In previous case, we print the string “hello world” by use the “printf” function that define in cstdio standard library. Actually, you are recommend use the iostream standard library, the cstdio is from the stdio.h in c language.
Fundamental to iostream are istream and ostream, which represent input and output streams. The library defines four IO objects.
name | in where | referred |
---|---|---|
cin | istream | standard input |
cout | ostream | standard output |
cerr | ostream | standard error |
clog | ostream | general information about the execution of the program |
This is a sample case, read two numbers form standard input and print the sum by standard output.
#include <iostream>
int main()
{
int num1, num2;
std::cout << "Enter two numbers:" << std::endl;
std::cin >> num1 >> num2;
std::cout << "the sum is:" << num1 + num2 << std::endl;
return 0;
}
//running
//Enter two numbers:
//10 20
//the sum is:30
The first line in the previous case is #include <iostream>
tell the compiler that wo want use the iostream library, we called it header in c++ program. And we notice that the program use the std::cin and std::cout rather than cin and cout, the profix std::
mean the cin and cout are defined in the namespace called std
. The namespace allow us to use function that has same name in a program.
comments
Comments help human reading program, the often to summarize a algorithm and explain what the variable’s mean, the compiler ignore the comments, so they need be identified by special way.
//this is a comment
/*
you can also write conmment is this way
*/
/*Comment Pairs Do Not Nest
/*Comment Pairs Do Not Nest, the end of a comment is */
compler think this line is a statement, because the comments is end*/
Flow of Control
Statement normally execute sequentially, the first statement at main block execute first, follow by second, the most important control ways in program are while loop, for loop and if branch. the following program add all even numbers between 0 to 20.
#include <iostream>
int main()
{
/*
* this is a simple program that
* add all even numbers between 0 to 20
*/
int n = 0, sum = 0;
// while loop end when n greater than 20
while (n <= 20)
{
// if n is a even number
if (n % 2 == 0)
{
sum += n;
}
n++;
}
std::cout << "the sum of even numbers between 0 to 20 is " << sum << std::endl;
sum = 0;
// for loop end when i greater than 20
for (int i = 0; i <= 20; i++)
{
// if i is a even number
if (i % 2 == 0)
{
sum += i;
}
}
std::cout << "the sum of even numbers between 0 to 20 is " << sum << std::endl;
return 0;
}
Introducing Classes
one of the most difference between c language and c++ language is the c++ has classes. In c++, we define our data structure by define a classes, a class defines a type along with many operations that are related to this type. We will discuss more details in the next post.