Re: definition for invariant and class invariant

Discussion in 'C++' started by James Kanze, Jul 30, 2010.

  1. James Kanze

    James Kanze Guest

    On Jul 30, 4:09 am, ", India"
    <> wrote:
    > In the websitehttp://en.wikipedia.org/wiki/InvariantI found the
    > following definition for 'invariant'
    > Invariant(ComputerScience), an expression whose value doesn't change
    > during program execution.


    No you didn't. There's no definition on the page you cite
    above, just a bit of text to disambiguate different entries.
    Any definition would be in one of the entries (and the entry
    which is referenced by the text you cite gives a complete
    definition).

    > For example, consider the following program:
    > #include <cstdlib>
    > #include <iostream>


    > using namespace std;
    >
    > class Test
    > {
    > public:
    > explicit Test(int arg = 0);
    > int get(void) const;
    > static int get_static();
    > private:
    > const int val;
    > static const int s_int;
    > };


    > const int Test::s_int = 100;


    > inline Test::Test(int arg) : val(arg)
    > {
    > }


    > inline int Test::get(void) const
    > {
    > return val;
    > }


    > inline int Test::get_static(void)
    > {
    > return s_int;
    > }


    > int main()
    > {
    > cout << "static const int Test::s_int = "
    > << Test::get_static() << endl;
    > Test first(1);
    > cout << first.get() << endl;


    > Test second(1000);
    > cout << second.get() << endl;


    > return EXIT_SUCCESS;
    > }


    > In the above program, (from the above definition, I thought an
    > 'invariant' is a non-static const data member and a 'class invariant'
    > is a static const data member)


    If I were you, I'd reread the page in question, then go to the
    entries it references. In particular, for class invariant, go
    to the page on class invariants referenced on that page.

    If you read the text in the article "invariant", you'll see that
    an invariant is always a predicate. Thus, in the above code,
    "Test::s_int == 100" is an invariant.

    > 'Test::val' is an invariant and
    > 'Test::s_int' is a class invariant. Is my understanding correct?


    Not at all.

    > If I am wrong, kindly provide the definitions for the terms
    > 'Invariant' and 'Class Invariant' with supporting program
    > sample.


    You might start by reading the articles in Wikipedia. The
    Wikipedia may not be the best reference in the world, but in
    this case, it does present a reasonably good introduction to
    class invariants; the article on loop invariants is also worth
    reading.

    --
    James Kanze
    James Kanze, Jul 30, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaKoadMunky

    Handling class invariant violations

    DaKoadMunky, Dec 4, 2004, in forum: C++
    Replies:
    8
    Views:
    1,451
    Victor Bazarov
    Dec 5, 2004
  2. Jianli Shen
    Replies:
    1
    Views:
    565
    Victor Bazarov
    Mar 13, 2005
  3. Giovanni Bajo

    min/max: "stable" invariant?

    Giovanni Bajo, Mar 11, 2008, in forum: Python
    Replies:
    1
    Views:
    242
    Terry Reedy
    Mar 11, 2008
  4. helveticus

    Keeping datetime culture invariant

    helveticus, Sep 22, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    425
    helveticus
    Sep 23, 2008
  5. Jonathan Lee
    Replies:
    3
    Views:
    477
    James Kanze
    Jul 31, 2010
Loading...

Share This Page