Connecting Tech Pros Worldwide Help | Site Map

simpel c++ questions

Newbie
 
Join Date: Nov 2009
Posts: 8
#1: 2 Weeks Ago
Q1:
what is "string"? Is it a data type just like int, double, char? Also, when time comes that you need to compare two strings, how would you construct the comparison expression (example is needed)?


Q2:
Global vs. local variables? Explain what are Global and Local Variables
Newbie
 
Join Date: Nov 2009
Posts: 20
#2: 2 Weeks Ago

re: simpel c++ questions


In C++, a string can be described as a class for handling an array of characters; for example, text. I can't quite remember how to compare strings in C++(I use C mostly), but I'm pretty sure there is an operator to test this defined in the class.

Global variables are variables defined outside of any function, and are available to all functions in that file. They are not freed until the end of the program, so their value will always be there. The difference between declaring and defining, and the "extern" keyword, is beyond the scope of this explanation, so I won't go into it.

Local variables are variables defined inside a function, and are freed when the function returns, thus the values will no longer be there the next time the function is called unless they are declared with the "static" keyword. Local variables cannot be accessed by functions called from the function they were defined in, unless passed by reference, but they will still retain their value when the called function returns.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,363
#3: 2 Weeks Ago

re: simpel c++ questions


In C++ a string is a data type. You know it's a type becuse you can create variables oif the string type:

Expand|Select|Wrap|Line Numbers
  1. int  var;
  2. string var1;
In C a string is not a data type. But that's C, here we are talking about C++.

On question 2, global variables are those variables defined outside any function. You are not to use global variables in C++. Why? Read The Case Against Global Variables

Variables that are not global are called local.
Reply


Similar C / C++ bytes