Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,104 questions

40,777 answers

573 users

How to use static variable in class with C++

2 Answers

0 votes
#include <iostream>

class Example {
    public:
        static int num;
        Example() {};
};
    
int Example::num = 23;

int main() {
    Example obj;
    
    std::cout << obj.num << "\n";
    
    int x = obj.num + 47;
    std::cout << x;
}
  
  
  
  
/*
run:
  
23
70
  
*/

 





answered Nov 27, 2022 by avibootz
0 votes
#include <iostream>
#include <cmath>

class Circle {
public:
    double r;
    static double pi; // static variable - one copy for all objects
    static double newArea(double r) { return M_PI * r * r; }
};

int main()
{
    double area = Circle::newArea(3);
    
    std::cout << area;
}



/*
run:

28.2743

*/

 





answered Dec 1, 2022 by avibootz

Related questions

2 answers 127 views
1 answer 85 views
1 answer 39 views
1 answer 25 views
1 answer 35 views
...