class member definition

Can private class functions use private members without specifying in the function implementation?


Like this? Yes, private functions can use private members.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

class MyClass
{
public:
    int SetAndPrintX(int i) 
    { 
        x = i;
        print();
    }
private:
    int x;
    void print()
    {
        std::cout << x;
    }
};

int main()
{
    MyClass c;
    c.SetAndPrintX(3);
    return 0;
}
3

Last edited on
Topic archived. No new replies allowed.