simpel c++ questions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • just curious
    New Member
    • Nov 2009
    • 5

    simpel c++ questions

    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
  • Ectara
    New Member
    • Nov 2009
    • 24

    #2
    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.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

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

      Code:
      int  var;
      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.

      Comment

      Working...