Overview C++ for beginners (part 2)
In the first part of the Overview C++, we talked about the history of C++, and about some of its tools and software. Today, let’s cover the remainder, which is the overview of its syntax, definition of its terms and vocabulary to make you feel more familiar with.
Introduction to C++ language and syntax
Overview C++, C++ is an object-oriented programming language. Its programs are modeled around objects and classes, which help you control and manipulate by applying functions. OOP languages offer a clear structure to a program and enable developers to model real-world problems.
The language is designed to provide you with a lot of freedom and power, which is both good and bad. The good thing is you’re in full control of how your system utilizes resources; but the bad thing is there is no automatic memory management like in Java.
You have the ability to choose between how memory is allocated (i.e. stack or heap), which is good; but there is no interpreter in C++ to stop you from writing buggy code, which is bad.
In order to get started with C++, you need to familiarize yourself with the syntax. This will pave the way for the rest of your C++ journey and help you create optimized programs that are safe and bug-free.
What is syntax?
Syntax is like the grammar of a programming language. It is the basic foundation for everything you’ll write in C++.
These are the rules that define how you write and understand C++ code. Let’s look at an example of some code to familiarize ourselves with the syntax.
If you run this syntax, the output will be
Hello World Learn C++ Educative Team
The syntax explained
#include <iostream> is a header file library. A header file imports features into your program. We’re basically asking that the program copy the content from a file called <iostream>. This stands for input and output stream, and it defines the standards for the objects in our code.
using namespace std means that we are using object and variable names from the standard library (std). This statement is often abbreviated with the keyword std and the operator ::. The int main ( ) is used to specify the main function.
It is a very important part of C++ programs. A function essentially defines an action for your code. Anything within the curly brackets { } will be executed.
cout is an object (pronounced see – out). In this example, it defines our outputs: the strings of words. We write a new object using cout on the second line. The character \n makes the text execute on a different line.
Including two \n\n creates a blank space. By writing return 0, we are telling the program that nothing will return. We are only outputting strings of text. Note that we use the << operator to name our objects. The semi colon ; functions like a period.
C++ terms and vocabulary
Now that we have a sense of what C++ code looks like, let’s define some of the terms we mentioned and introduce you to a few more.
Keywords
Keywords are predetermined names that can be used to identify things in your code, such as particular objects, variables, or actions. You can also make your own keywords. Here are a few examples of it:
-
-
-
- goto
- float
- public
- class(1)
- int
-
-
Variables
Variables are like containers that store values. To declare a variable, you must give it a value and a type using the correct keyword. All variables in C++ need a name, or identifier. You should follow some basic syntax rules when making identifiers.
-
-
-
- Names are case sensitive
- Names can contain letters, numbers, and underscores
- Names must begin with a letter or an underscore
- Names cannot contain whitespaces or special characters (!, #, @, etc.)
- Names cannot use reserved keywords (for example, keywords in C++ like int)
-
-
There are six different types of variables:
Data types
Data types are the classifications for different kinds of data you can use in a program. Data types tell our variables what data they can store. There are three data types in C++:
-
-
-
- Primitive data types: these are the built-in data that you can use to declare variables. They include integer, character, boolean, floating point, double floating point, void, and wide character.
- Derived data types: these are derived from the primitive data types. They include function, reference, array, and pointer.
- User-Defined data types: these are defined by you, the programmer.
-
-
Strings
Strings are objects in C++. They are a set of characters within ” “ quotes, like our ”Hello World” string. Since they are objects, we can perform functions to them, like the length ( ) function, which determines the length of a string.
Operators
Operators are symbols that manipulate your data and perform operations. In C++, you can overload operators to make them work for programmer-defined classes. Overloading an operator basically means that an operator can have more than one function at a time. There are four kinds of operators in the C++ language:
Objects
An object is a collection of data that we can act upon. An object in C++ has an attribute (its traits) and method (its abilities). You construct objects using a class. Think of this like a blueprint for an object.
You create a class using the class keyword. You must define an access specifier, such as public, private, or protected. The public keyword states that class is accessible from outside that class. Once you define your class, you can define your attributes and objects. Take a look below at an example of a class and object.
Output for this code is:
Dog name is: rover Dog gender is: male Dog age is: 5
Functions
Functions are blocks of code that run when they are invoked. They are the workhorse for your program and are used to perform operations and manipulations on your code.
They are extremely important for code reusability and help to better modularize your code. Think of these like actions that you initiate. In C++, there are predetermined functions, like the main ( ) of our initial example.
To create a function, you have to give it a name (called the declaration) and parentheses ( ). You can then invoke this function at any point by using that name ( ).
There are a lot of ways to use functions. You can also attach return values to your functions, which determine if a function should output any information. The void keyword states that there will be no return. The return keyword, on the other hand, will call for a data type output.
Conditional statements
These allow you to perform checks on whether a block of code should be executed or not. There are four conditional statements in C++:
-
-
-
- if: a certain action will be performed if a certain condition is met
- else: a certain action will be performed instead if that condition is not met
- else if: a new condition will be tested if the first is not met
- switch: tests a variable against a list of values
-
-
Loops
Loops are similar to conditional statements. They execute blocks of code as long as a certain condition is reached. There are two types of loops in C++:
-
-
-
- while loops: this loop will continue to iterate through your code while a condition returns true.
- for loops: this is used when you know the exact number of times you want to loop in your code
-
-
iRender - The best GPU cloud for C++
At iRender, we provide multiple GPUs for renting with state-of-the-art RTX3090. Our remote machines are Optimized for Scientific Computing, Machine Learning, Deep Learning.
We support not only Overview C++, but also other programming languages such as: Python, Java, R, and many IDEs and libraries such as TensorFlow, Jupyter, MXNet, PyTorch, Keras, CNTK, Caffe and so on.
This is our packages tailored for AI/ Deep Learning:
Plus, at iRender, we provide you more support than just those hardware.
NVLink available for better performance
If 24GB VRam is not enough for your project, we always have NVLink to help you access more than that. You can access this article to know how to set up NVLink on our machine.
Free and convenient transferring tool
iRender offers a powerful and free file transfer tool: Gpuhub Sync. With fast file transfer speed, large data capacity, and completely free. You can transfer all the necessary data into our Gpuhub Sync tool at any time without connecting to the server. The data will be automatically synchronized in the Z drive inside the server, ready for you to use.
Flexible price
Besides hourly rental, you can always save from 10% to 20% with our Fixed Rental feature. For those who’s in need of a server for more than a day, or have extremely large projects, we advise to choose a daily/ weekly/monthly rental package. The discount is attractive (up to 10% for daily packages, 20% on weekly and monthly packages), and you don’t have to worry about over-charging if you forget to shutdown the server.
Real human 24/7 support service
Users can access our web-based online platform and use multiple nodes to render at the same time. Hence, with us, it does not matter where you are present – as long as you are connected to the internet, you can access and enjoy the 24/7 rendering services that we provide, and if you meet any issue, our real human 24/7 support team is always ready to support you.
With these advantages and no hidden cost when using our machine, we believe it’s worth trying your project. You can create an account via this link to experience our service. And don’t hesitate to contact us via WhatsApp: (+84) 916806116 for advice and support.
Thank you & Happy Training!
Source: educative.io
Related Posts
The latest creative news from Cloud Computing for AI,