Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. static min max values from object

static min max values from object

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 2 Posters 973 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on 1 Aug 2020, 03:01 last edited by
    #1

    Hello,

    i would like to achieve that my class has a static NumMin and numMax,

    so A::NumMin and A::numMax keep track of the smallest and biggest values of all objects of the same class.
    it should work sofar i can, but it doesn't

    class A
    {
      public:
        static int counter; // object counter
        static int numMin;
        static int numMax;
    
        int max = numeric_limits<int>::min() ;
        int min = numeric_limits<int>::max() ;
    
        int ones = 0;
        A()
        {
          counter++;
    
        }
        virtual ~A()
        {
          counter--;
        }
    
       void somefunction(int temp){
       
            setLargestNumber(temp);
        }
    
        void setLargestNumber(int temp){
            //std::cout << "temp: " << temp << std::endl; // always holds the correct value to check
            if( temp>max ) max = temp;
            if( temp<min ) min = temp;
            //std::cout << "min: " << min << "Max:" << max << std::endl;
            numMin = min; 
            numMax = max;
        }
    };
    
    int A::counter = 0;
    int A::numMin = 0;
    int A::numMax = 0;
    

    this is the orginal code:

    #include <iostream>
    #include <limits>
    using namespace std;
    
    int main()
    {
        int max = numeric_limits<int>::min() ;
        int min = numeric_limits<int>::max() ;
        int num ;
    
        std::cout << "min: " << min << "Max:" << max << std::endl;
    
        while( cout<<"Enter the number <any non-numeric character to end input> " &&
               cin >> num )
        {
            if( num>max ) max = num;
            if( num<min ) min = num;
        }
    
        cout << "largest number is: " << max << '\n'
             << "smallest number is: " << min << '\n' ;
    }
    

    the original code taken from http://www.cplusplus.com/forum/beginner/115708/works perfectly fine @ http://cpp.sh/

    J 1 Reply Last reply 1 Aug 2020, 07:14
    0
    • N Natural_Bugger
      1 Aug 2020, 03:01

      Hello,

      i would like to achieve that my class has a static NumMin and numMax,

      so A::NumMin and A::numMax keep track of the smallest and biggest values of all objects of the same class.
      it should work sofar i can, but it doesn't

      class A
      {
        public:
          static int counter; // object counter
          static int numMin;
          static int numMax;
      
          int max = numeric_limits<int>::min() ;
          int min = numeric_limits<int>::max() ;
      
          int ones = 0;
          A()
          {
            counter++;
      
          }
          virtual ~A()
          {
            counter--;
          }
      
         void somefunction(int temp){
         
              setLargestNumber(temp);
          }
      
          void setLargestNumber(int temp){
              //std::cout << "temp: " << temp << std::endl; // always holds the correct value to check
              if( temp>max ) max = temp;
              if( temp<min ) min = temp;
              //std::cout << "min: " << min << "Max:" << max << std::endl;
              numMin = min; 
              numMax = max;
          }
      };
      
      int A::counter = 0;
      int A::numMin = 0;
      int A::numMax = 0;
      

      this is the orginal code:

      #include <iostream>
      #include <limits>
      using namespace std;
      
      int main()
      {
          int max = numeric_limits<int>::min() ;
          int min = numeric_limits<int>::max() ;
          int num ;
      
          std::cout << "min: " << min << "Max:" << max << std::endl;
      
          while( cout<<"Enter the number <any non-numeric character to end input> " &&
                 cin >> num )
          {
              if( num>max ) max = num;
              if( num<min ) min = num;
          }
      
          cout << "largest number is: " << max << '\n'
               << "smallest number is: " << min << '\n' ;
      }
      

      the original code taken from http://www.cplusplus.com/forum/beginner/115708/works perfectly fine @ http://cpp.sh/

      J Offline
      J Offline
      JonB
      wrote on 1 Aug 2020, 07:14 last edited by
      #2

      @Natural_Bugger said in static min max values from object:

      it should work sofar i can, but it doesn't

      What doesn't work? How & when do you create new A instances and use them?

      If you call this more than once, be aware that second time round numMin & numMax --- because they are static --- will start off with their values from last time round where you went numMin = min; numMax = max;. However, your code sets those two variables in those two assignment statements, but (unlike the code you copied from) you do not actually use those two values anywhere. I don't know if either of these observations is relevant to your issue.

      N 2 Replies Last reply 1 Aug 2020, 15:22
      1
      • J JonB
        1 Aug 2020, 07:14

        @Natural_Bugger said in static min max values from object:

        it should work sofar i can, but it doesn't

        What doesn't work? How & when do you create new A instances and use them?

        If you call this more than once, be aware that second time round numMin & numMax --- because they are static --- will start off with their values from last time round where you went numMin = min; numMax = max;. However, your code sets those two variables in those two assignment statements, but (unlike the code you copied from) you do not actually use those two values anywhere. I don't know if either of these observations is relevant to your issue.

        N Offline
        N Offline
        Natural_Bugger
        wrote on 1 Aug 2020, 15:22 last edited by Natural_Bugger 8 Jan 2020, 15:27
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • J JonB
          1 Aug 2020, 07:14

          @Natural_Bugger said in static min max values from object:

          it should work sofar i can, but it doesn't

          What doesn't work? How & when do you create new A instances and use them?

          If you call this more than once, be aware that second time round numMin & numMax --- because they are static --- will start off with their values from last time round where you went numMin = min; numMax = max;. However, your code sets those two variables in those two assignment statements, but (unlike the code you copied from) you do not actually use those two values anywhere. I don't know if either of these observations is relevant to your issue.

          N Offline
          N Offline
          Natural_Bugger
          wrote on 1 Aug 2020, 15:37 last edited by
          #4

          @JonB
          thnx, i got it working now, i had to do some minor improvements and pass the tests.

          1 Reply Last reply
          0

          2/4

          1 Aug 2020, 07:14

          • Login

          • Login or register to search.
          2 out of 4
          • First post
            2/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved