How to use constructor initializer list to initialize class members in C++

1 Answer

0 votes
#include <iostream>
#include <string>

class Example {
private:
    int m_A;
    std::string m_Str;
public:
    
    Example() : m_A(0), m_Str("***") {}
    
    void Print() const {
        std::cout << m_A << " " << m_Str << "\n";
    }
};
 

int main() {
    Example ex;
     
    ex.Print();
}
  
  
  
  
/*
run:
  
0 ***
  
*/

 



answered Feb 2, 2023 by avibootz

Related questions

1 answer 100 views
1 answer 226 views
1 answer 109 views
1 answer 127 views
...