Program in C++ for Beginners
Danish computer scientist Bjarne Stroustrup developed the high-level, versatile programming language C++. C++ initially appeared in 1985 as a component of the programming language C, and it has since evolved greatly. As of 1997, it included working, universal, and oriented toward objects features, as well as low-level memory tampering capabilities for creating OSes like Windows and Linux and creating devices like computer chips. It is nearly generally deployed as a language that is compiled, and a wide range of manufacturers, such as Microsoft, LLVM, IBM, Embarcadero, Oracle, and the Free Software Foundation, offer C++ compilers.
Example Program:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
Output:
![Program in C++ for Beginners/>
<!-- /wp:html -->
<!-- wp:heading -->
<h2 class=](https://static.tutorialandexample.com/cpp/program-in-cpp-for-beginners1.png)
The code above prints Hello, World! On the screen, use the cout << command.
Keep in mind these crucial cout facts:
- Double quotes "" encapsulate the content that needs to be printed.
- Semicolons ; are used to terminate each cout <<.
- Your code won't run correctly and you'll get problems if you don't follow the guidelines above.
C++ Comments:
Comments may be employed to improve readability and provide clarification for C++ code. In evaluating alternate code, it may be employed as well to stop operation. One or more lines can be included in a remark.
Example Code:
#include <iostream>
using namespace std;
int main() {
// print memory allocation to the screen
cout << "memory allocation";
return 0;
}
Output:
![Program in C++ for Beginners/>
<!-- /wp:html -->
<!-- wp:heading {](https://static.tutorialandexample.com/cpp/program-in-cpp-for-beginners2.png)
Comment on a single line:
Comments on a single line begin with two forward slash characters (//).
The compiler fails to execute anything that is within // and the conclusion of the line.
Before a line of code, the following instance employs a single-line comment:
Example Code:
#include <iostream>
int main() {
// declaring the variable
int x;
// initializing the variable 'x' with the value 7
x = 7;
// print the value of 'x'
std::cout << "Value of x is: "<< x;
return 0;
}
Output:
![Program in C++ for Beginners/>
<!-- /wp:html -->
<!-- wp:heading {](https://static.tutorialandexample.com/cpp/program-in-cpp-for-beginners3.png)
Multi-line Comments in C++:
Comments with multiple lines begin with /* and conclude with */.
The compiler will not process any text that appears between /* and */:
Example Code:
#include <iostream>
int main() {
/* declaring variable
to store rollnumber to Student
*/
int rollnumber = 27;
// print the rollnumber
std::cout << "rollnumber: "<< rollnumber;
return 0;
}
Output:
![Program in C++ for Beginners/>
<!-- /wp:html -->
<!-- wp:heading -->
<h2 class=](https://static.tutorialandexample.com/cpp/program-in-cpp-for-beginners4.png)
The function is a section of code that executes only after it is triggered.
A function can accept parameters, or data, as input.
Functions are essential for the reuse of code since they carry out specific tasks: Write the code once, then utilize it frequently.
Make a Function:
C++ comes with a few pre-defined functions that are used to run code, including main(). However, you can also write your applications to carry out specific tasks.
To define (also called declare) a function, put its name in parentheses () and press the function key:
Syntax:
Void myfunction {
}
Call a Function:
Declared functions take time to perform. The fact that they are "rescued for later use" means that they are going to be used whenever needed.
When calling a function, enter its name, two parentheses (), and a semicolon ;
In the instance that follows, myFunction() is employed to generate a text whenever it is invoked (the action):
Example Code:
#include <iostream>
using namespace std;
void myFunction() {
cout << "Lara jean!";
}
int main() {
myFunction();
return 0;
}
Output:
![Program in C++ for Beginners/>
<!-- /wp:html -->
<!-- wp:heading -->
<h2 class=](https://static.tutorialandexample.com/cpp/program-in-cpp-for-beginners5.png)
There are two components to a C++ function:
- Declaration: the function's title, parameters (if any), and the result type
- Definition: the function's body or the code that has to be run
Syntax:
Void myfunction{
}
Note: An error will arise if a user-defined function, like myFunction(), is declared after the main() function.
Example Code:
#include <iostream>
using namespace std;
int main() {
myFunction();
return 0;
}
void myFunction() {
cout << "I just got executed!";
}
Output:
![Program in C++ for Beginners/>
<!-- /wp:html -->
<!-- wp:heading -->
<h2 class=](https://static.tutorialandexample.com/cpp/program-in-cpp-for-beginners6.png)
Functions can receive parameters with information given to them. Within the function, parameters take the role of variables.
The parameters are enclosed in parentheses and are supplied after the function name. You are free to add as many variables as you like; simply comma-separate them:
Syntax:
vVoid functionname ( parameter1 , parameter2 ){
}
Example Code:
#include <iostream>
#include <string>
using namespace std;
void myFunction(string fname) {
cout << fname << " Refsnes\n";
}
int main() {
myFunction("Lara");
myFunction("Peter");
myFunction("kitty");
return 0;
}
Output:
![Program in C++ for Beginners/>
<!-- /wp:html --></p>
</div>
<div class=](https://static.tutorialandexample.com/cpp/program-in-cpp-for-beginners7.png)