1
class A{
   private :  string a[3];
   public :   A();
              void ShowA();
}

A::A(){ string a[3] = {"aa","bb","cc"}  }
void A::ShowA(){
   for(int x=0;x<=2;x++){
       cout<< a[x];
   }
}
int main(){
    A a;
    a.ShowA();
    return 0;
}

In this code, I think the output is aabbcc but there is nothing. Just blank is existed. Could you tell me why it happens and how to fix it. Cheers guys.

4
  • 2
    string a[3] in your constructor creates a new local variable (completely unrelated to member variable with the same name), which is immidiately discarded. See Initializing a member array in constructor initializer or C++ Initializing Non-Static Member Array for a more modern approach. Commented May 21, 2020 at 19:12
  • You're shadowing your own definition of a and assigning to a local variable that's later discarded. Commented May 21, 2020 at 19:12
  • 1
    you have a member called a and in the constructor you have an a that is local to the constructor. Read about scope (and std::array and initialization of members) Commented May 21, 2020 at 19:12
  • Consider using std::vector here. Commented May 21, 2020 at 19:12

2 Answers 2

2

As the comments tell you, you are creating a local variable a inside your constructor, instead of setting the value of the attribute a. You can set the value of a in member initializer list.

The code becomes

#include <iostream>
#include <string>

using namespace std;

class A {
private:
    string a[3];

public:
    A();
    void ShowA();
};

A::A() : a{"aa"s, "bb"s, "cc"s} {}

void A::ShowA() {
    for(int x = 0; x <= 2; x++) {
        cout << a[x] << std::endl;
    }
}
int main() {
    A a;
    a.ShowA();
    return 0;
}

Note: The 's' after the "aa", "bb" and "cc" strings is a string literal. It is not really necessary in this case, since the compiler know you are creating an array of std::string objects.

Sign up to request clarification or add additional context in comments.

3 Comments

can ask A::A() : a{"aa"s, "bb"s, "cc"s} {} what is s mean??
You can always ask. That is the member initializer list. It is a way to initialize member variables. The syntax is ":" followed by variable name and its value between parenthesis or curly brackets. If you have multiple member variables you add a comma and then the next variable and so on. Notice that it is not in the constructor body (this has advantages). See the link for "member initializer list" in the answer.
oh I missed last comments. I'll try to understand and think myself. Thanks a lot , Cheers!
0

You are re-defining the string "a" in the constructor, you should simply assign it in the constructor instead of using the "string" type again. If you give the type again it creates a local variable available in the scope of the constructor only.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.