How to initialize class variable in constructor with C++

1 Answer

0 votes
#include <iostream>

class Example {
public:
    float x, y;
    
    Example() {
        x = 0.0f;
        y = 0.0f;
    }
    
    void Print() {
        std::cout << x << ", " << y << "\n";
    }
};

int main() {
    Example ex;
    
    ex.Print();
    
}




/*
run:

0, 0

*/

 



answered Jan 29, 2023 by avibootz

Related questions

1 answer 127 views
2 answers 161 views
2 answers 142 views
142 views asked Jan 29, 2023 by avibootz
1 answer 167 views
1 answer 127 views
1 answer 160 views
...