-2

In C++, how are the local class variables declared? I'm new to C++ but have some python experience. I'm wondering if C++ classes have a way of identifying their local variables, for example, in python your class' local variables are marked with a self. so they would be like:

self.variable_name

Does C++ have something similar to this for local variables or does it have something completely different? In pseudocode, I think the class' variables would look something like this:

class Code:
     public:
          <some code>
     private:
          int self.variable
          double self.other_variable
          <more code>

but then, I could be completely wrong. Thanks in advance.

Nightdra
  • 3
  • 4
  • 6
    I'm voting to close this question as off-topic because it is very unclear. I suggest you read a basic textbook. Your question is actually highly ambiguous, since you are using the word "initialized" to mean something very different from what is actually considered to be initialisation in C++. Your "code" doesn't help clarify what you mean either. Spend some times learning the basics. Once you know some basics, you'll you'll be able to ask a less confusing question. – Peter May 21 '17 at 05:29
  • I've edited the question to hopefully make the question clearer. – Nightdra May 22 '17 at 05:03
  • Welcome to Stack Overflow! Sound like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver May 24 '17 at 12:47

2 Answers2

0

That's pretty close! Within the class, however, one would mention the class variables simply using their own name, therefore as variable as opposed to class.variable.

(Also, note that your functions need to have a semicolon following them, and by convention these functions tend to be defined under the class itself, or in separate files)

class Circle {
     public:
          Circle(double var, bool otherVar); //constructor
          double getVariable(); //getter
          void setVariable(double var); //setter
          // you can put more functions here

     private:
          double variable;
          bool otherVariable;
          //you can put more functions here
};

Circle::Circle(double var, bool otherVar){
    variable = var;
    otherVariable = otherVar;
}

Circle::getVariable(){
    return variable;
}

Circle::setVariable(double var){
    variable = var
}

For a better look at this topic, please look at this similar question/answer., or as noted in a comment, consider reading a textbook on C++...

EDIT: I wrote this answer based on the question title of "identifying" variables, by noting that the problem was likely that code couldn't compile because class.variable is not how things are referred to in C++. However, I'm unsure if the question refers to initializing, declaring, etc.

Community
  • 1
  • 1
Darwin Huang
  • 105
  • 4
-1

When you read Effective C++ (written by Scott Meyers), member variables are init when ctor initializer. After ctor, all assignment is assignment, not init. You can write ctor like this.

Circle(double value, bool isTrueFalse, <More Variables>) : class.variable(value), class.othervariable(isTrueFalse), ..<More Variables> //this is init.
{ 
     class.variable  = value; //this is assignment. not init.
}

C++ init orders is up-side-down, not ctor initializer order.

 private:
       double class.variable; //First init;
       bool class.variables;//Second init; 

If you want local variables init, you pass value to ctor.

in C++. assignment and init is diffrent. class local members only init at ctor Initializer. init is faster than assignment. because init is just one call ctor, and end. but assignment is call ctor, and assignment Operator one more. you should to use ctor Initializer, for performece.