Thread: Silly Class Question

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    18

    Silly Class Question

    How do you create a class where the base object can be assigned a value?

    I'd like to do something like:

    Code:
    class number{
      public:
      blah blah blah
    
      void increment() {
        // Probably something like self++ ???
      }
    
    };
    
    
    int someFunction() {
      number val = 100;
      val.increment();
      return val;
    }
    I've seen it done enough times but have no clue how to actually implement it.

    I guess I could always study the string class . . .
    I doubt, therefore I might not be

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First off, you wouldn't be actually incrementing the class. You'd be incrementing some base type that the class contains. Example:

    Code:
    class Number {
        private:
            int num;
        public:
            Number( ) { num = 0; }
            void Increment( ) { num++; }
    };
    Because really, what else would you be incrementing? There has to be something to actually increment.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    18

    Further clarification

    You know how you can do a

    Code:
    string somestring = "Hello there";
    somestring.replace(4, 2, "xx");
    I want to know how they are able to do that.

    Maybe it's not something you can do with a class . . .
    I doubt, therefore I might not be

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    the replace function is one that works with strings.

    If you wanted to do the same thing with your class, then you would need to write a function that would do that.

    Later,
    WebmasterMattD
    WebmasterMattD.NET

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    The replace function was used just as an example. I guess I'm not explaining it clear enough.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Are you just wanting to increment one value held inside of the class?
    If so, that is simple.
    Create a function ( declared public as part of the class ) that will increment the value that you want.
    WebmasterMattD
    WebmasterMattD.NET

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Question

    >>I want to know how they are able to do that.

    >>Maybe it's not something you can do with a class . . .

    You can do anything with a class!

    Anyhow, I don't understand your question, so I will leave it at that

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    When you use the ANSI string class you can do a 'string stringname="text"'

    Using that string you can do things like "stringname.insert(foo)" and "stringname.erase(foo)". Of course foo isn't valid syntax for those commands.

    Using classes I know how to do things like:
    Code:
    class someclass {
    public:
      int val;
      int someotherval;
    
      void somefunction() {}
    
    };
    
    someclass object;
    object.val = 10;
    object.somefunction();
    and so on . . .

    What I want to know is how they are able to assign a value to "stringname" without needing to do something like "stringname.val = somevalue".

    Hope this clarifies my question a bit.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It's called operator overloading. It is a tool that enables you to use more natural syntax for your classes. In essence, it works like this:

    Code:
    class DoubleNum {
    
    private:
    
    int value1;
    int value2;
    
    public:
    
    DoubleNum():value1(0), value2(0){}
    
    void operator = (int this_value) {
      value1 = this_value;
      value2 = this_value;
     }
    
    void print() {
      cout << endl << "Value One: " << value1 << endl
      << "Value Two: " << value2 << endl;
     }
    };
    
    
    int main()
     {
      DoubleNum d;
      d.print();
      d = 12;
      d.print();
      system("pause"); 
      return 0;
     }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    Thank you!!!! I owe you a beer.
    I doubt, therefore I might not be

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  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, 06:51 PM