I am confused about the use of #include <string>
at the start of a program. For example, in the code below, I don't use #include <string>
but the function will still print out the string "Johnny's favorite number is" when it is run.
#include <iostream>
using namespace std;
void printVariable(int number){
cout << "Johnny's favorite number is" << number << endl
}
However, in this code below, it does contain #include <string>
.
#include <iostream>
#include <string>
using namespace std;
class Var{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string name;
};
int main(){
Var Classy;
Classy.setName("Johnny Bravo");
cout << Classy.getName() << endl;
return 0;
}
Do I only use #include <string>
if a variable represents a string?
A semicolon can replace a period if the writer wishes to narrow the gap between two closely linked sentences (independent clauses). Examples: Call me tomorrow; you can give me an answer then. We have paid our dues; we expect all the privileges listed in the contract.
Semicolons should introduce evidence or a reason for the preceding statement; for example, this sentence appropriately uses a semicolon. A colon, on the other hand, should be used for a stronger, more direct relationship. It should provide emphasis, an example, or an explanation.
When a comma separates two complete sentences joined by a conjunction (and, but, or, nor, for, so, or yet) the comma and the conjunction can be replaced with a semicolon. I ate dinner, and I went to the movies. = I ate dinner; I went to the movies. She finished top of her class, but she was struggling to find work.
Do I only use
#include <string>
if a variable represents a string?
Yes.
Use #include <string>
when you use a variable that has type std::string
.
The code "text here"
, contrary to intuition, is not a std::string
; it is a string literal, and a C-style string, and a const char[10]
convertible to const char*
. Welcome to C++ with its legacy oddities.
If you use the type std::string
in your code then you should include the <string>
header. There are also a few other types and functions in that header, but std::string
is the most commonly used one.
However, you do not need to include this header just to use string literals, which are built into the core language.
Your question arises from the fact that you know that something like "aabcd"
is a string literal. So, its type should be string
. Well, that's not quite true.
C++ has a lot of features from C. Including data types. So, that is a pointer to char (char*
), not a string
(an instance of the string
class). You can create an instance of the string
class from a char*
(including a string literal) by passing it as argument to the constructor of string
. But it is not a string, it's just some misleading terminology.
A similar case is calling things vectors when they are arrays.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With