Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Introduction to String | Variables and Data Types
C++ Introduction
course content

Course Content

C++ Introduction

C++ Introduction

1. Getting Started
2. Variables and Data Types
3. Introduction to Operators
4. Introduction to Program Flow
5. Introduction to Functions

bookIntroduction to String

You can also store text in variables, in addition to symbols using char. For this, you need to:

  • Connect the string file;
  • Use std scope resolution;
  • Declare a variable of type string.
cpp

main

copy
1234567891011
#include <iostream> #include <string> int main() { //declaring string variable std::string myStringVariable = "codefinity"; //displaying our string variable std::cout << myStringVariable << std::endl; }

Strings variables can also contain numbers (as text):

cpp

main

copy
12345678910
#include <iostream> #include <string> int main() { std::string myStringVariable = "1024"; //displaying our string variable std::cout << myStringVariable << std::endl;; }

If you try to add two string variables, you get a concatenation (it works without spaces):

cpp

main

copy
1234567891011
#include <iostream> #include <string> int main() { std::string var1 = "Hello "; //space is also a symbol std::string var2 = "World"; //displaying the sum of string variables std::cout << var1 + var2 << std::endl; }

The same thing will happen with numbers – they will not be added algebraically.

What will the console display?

What will the console display?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4
some-alt