Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
restrepo
GitHub Repository: restrepo/ComputationalMethods
Path: blob/master/material/cpp.ipynb
934 views
Kernel: ROOT C++

A Hello world example

The usual way to make programs in C++ is open an editor, write some program, like the one belows:

#include <iostream> using namespace std; int main() { cout << "Hello world"; return 0; }

which includes the required main() function, compile the program and run it.

Literal programming with C++

In our parading of literal programming, we just mush open the notebook with the proper kernel, in this case the C++ implementation of the ROOT kernel. In the implementation of this kernel the declaration of the main function is no longer required. Therefore, we can focus in learn the concepts of the language without compilation issues. In this way, the equivalent "Hello world program is just"

We can assume which we are inside main function

#include <iostream> using namespace std; std::cout << "Hello world";
Hello world
cout << "This " << "is " << "awesome!";
This is awesome!

New Line

The cout operator does not insert a line break at the end of the output. One way to print two lines is to use the endl manipulator, which will put in a line break.

cout << "Hello world!" << endl; cout << "I love programming!";
Hello world! I love programming!

New Lines

The new line character \n can be used as an alternative to endl. The backslash () is called an escape character, and indicates a "special" character.

cout << "Hello world! \n"; cout << "I love programming!";
Hello world! I love programming!

New Lines

Two newline characters placed together result in a blank line.

cout << "Hello world! \n\n"; cout << "I love programming!";
Hello world! I love programming!

Multiple New Lines

Using a single cout statement with as many instances of \n as your program requires will print out multiple lines of text.

cout << " Hello \n world! \n I \n love \n programming!";
Hello world! I love programming!

In python could see something like

std.cout ....
cout << "Hello world";
Hello world

As in any other kernel, all the defined in the evaluation cells can be used later. Also ROOT is a full data analysis system so that the visualization tools are avalaible and ready to be used. We don not cover ROOT usage here.

Printing Text

Comments

You can add comments to explain what is doing the code. The compilator will ignore all in a comment.

To do a comment in C++ begin the line writing //\textbf{//}, then write the comment; for example, if we want write that a function prints "Hello world", we do:

#include <iostream> using namespace std; int main(){ //prints "Hello world" cout<< "Hello world!"; return 0; }

When above code is compiled the output is like:

//prints "Hello world" cout<< "Hello world!"; return 0;
Hello world!

Take care! ###

This is only for one-line comments, if we want a multi-line comment begin writing /*\textbf{/*}, write the comment and then we close with */\textbf{*/}. The comment can be span multiples lines not only one.

/*this is a one-line comment wrote as multi-line comment*/ /* and this is a comment in two lines*/

Its very important close the multi-line comment, compare these two codes:

/* this function prints "Hello world" cout<< "Hello world"; return 0;
/* this function prints "Hello world"*/ cout<< "Hello world"; return 0;
Hello world

If we write a comment with // in a comment with /* / or a comment whit / */ in a comment whit //, so we can "nest" comments.

/* comment out printing of Hello world! cout<< "Hello world!"; // prints Hello world! */

Variables

Working with variables

You have the option to assign a value to the variable at the the time you declare it and assign a value later. You can also change the value of a variable

int a; int b = 54; a = 10; b = 3;
std::cout << a;
10

Copy and paste the code in an execution cell replacing the blanks to declare variable c of type int and then assign 7 as its value

_ c; a _ 7;
int c; a = 7;

WARNING: not yet implemented here

int num;
std::cin >> num;
std::cout?

Copy and paste the code in an execution cell replacing the blanks to to declare sum as a variable, assign it the value 21+7, and print out its value.

int sum; ___=21+1; ___<<"sum is equal to "<<__<<endl;
int sum; sum=21+1; cout << "sum is equal to " << sum << endl;

More on variables

Basic Arithmetic

Assignment and incremental Operators