Class question

This is a discussion on Class question within the C++ Programming forums, part of the General Programming Boards category; Given the following class: class counting { private: int countA; int countB; int countC; public: void setCount(int x); int getCount(); ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Class question

    Given the following class:


    class counting
    {
    private:

    int countA;
    int countB;
    int countC;

    public:

    void setCount(int x);
    int getCount();
    }

    Could you use the same public methods to set and retrieve the count of all 3 private members of this class?

    If so How?

    If not, Is the only solution, having a setCount and getCount for each private member?

    Any better solutions?

    Thanks for your time,
    Alan

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    this is just a programming style adopted by a lot of programmers and if you find it cumbersome you can come up with your own......these functions are user created "defined" and are not necessary .

    .........

    you can accomplish 2 in 1 by simply:

    Code:
    class counting
    {
       private:
                int countA;
                int countB;
                int countC;
    
        public:
      int GetSetCount(int x);
    };l
    
    
    define it like that....
    
    int Counting::GetSetCount(int x)
    {
       int SomeCount;
       SomeCount = SomeCount + x // for example
    
       return SomeCount;
    }
    hope this makes it clearer...

    Regards,
    matheo917

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Not exactly what I was looking for

    This would allow me to combine the 2 methods into 1. But would that allow me to use the getset method to access 'any' of the 3 public members?

    How would I identify which private member.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is the only solution, having a setCount and getCount for each private member?
    Only if you need to set and retrieve each value individually, this is the most clear method but it gets combersome with larger classes. The trick is to minimize the number of private members that external sources need to view. Or you can be creative in how the data is accessed so as to minimize the number of class methods:
    Code:
    class counting 
    { 
      int countA; 
      int countB; 
      int countC;
    public: 
      void setAll ( int x, int y, int z ); 
      int getCount ( char mode ); 
    };
    
    void counting :: setAll ( int x, int y, int z )
    {
      countA = x, countB = y, countC = z;
    }
    
    int counting :: getCount ( char mode )
    {
      switch ( toupper ( mode ) ) {
      case 'A': return countA;
      case 'B': return countB;
      case 'C': return countC;
      default : return FAIL;
      }
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    any public member of your class can access private data members...... what that means is that you can define your function to do pretty much whatever you want all i did is combined SET function, that takes a parameter, with the old GET function.......

    simply said:

    Code:
     int GetSetCount:: GetSetCount(int x)
    {
        cout << countA;
         cout << countB;
         cout << countC;
    
          // so whatever else you want here....
    
        int SomeInt;
        SomeInt= SomeInt + x;
        
        // blah blah.... 
    
    
        return SomeInt;
    }
    do you get the picture???

    HINT * - a function can ONLY return ONE (1) value (item) as a parameter(return stament)......if you want the function to return more than 1 there's a different way of obtaining it......POINTERS.... but i won't indulge into this now.......

    hope that helps.....

    Regards,
    matheo917

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  2. Replies: 8
    Last Post: 10-02-2005, 01:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21