Simple Question about void!

This is a discussion on Simple Question about void! within the C++ Programming forums, part of the General Programming Boards category; I've reached the classes in my book, and I just want to know why the writer put " void ", ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    42

    Simple Question about void!

    I've reached the classes in my book, and I just want to know why the writer put " void ", before the class's member functions....
    Code:
    #include <iostream>      // for cout
    #include <cstdlib>
    using namespace std;
    class Cat                   // begin declaration of the class
    {
      public:                   // begin public section
        int GetAge();           // accessor function
        void SetAge (int age);  // accessor function
        void Meow();            // general function
      private:                  // begin private section
        int itsAge;             // member variable
    };

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A void return type means that function doesn't return a value. Those are member functions, not variables.

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

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    SetAge() and Meow() don't need to return any values, so they're void to make things simpler.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    All member functions (except constructors and destructors) must have a return type associated with them. It's not legal to leave the type off; it used to be legal in C (if no type was specified, the return was "int") but it's not legal C anymore either.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    42

    uha!

    ok, I got it...

    Thanks everybody!
    I have a code which stops life!

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    70
    If you don't want to return any value from function then you have to specify it's return type as 'void' because if you don't specify any return type by default it takes 'int' return type.
    Chintan R Naik

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by cr_naik
    If you don't want to return any value from function then you have to specify it's return type as 'void' because if you don't specify any return type by default it takes 'int' return type.
    Not true in C++, nor in C anymore. Some compilers will still default to int, even in C++ mode (I've seen GCC do this), but it isn't legal.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> but it isn't legal.

    ... it isn't standards compliant.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Bah! You'll shall be arrested for violating the standard!
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. ChangeDisplaySettings - Blank?
    By Tonto in forum Windows Programming
    Replies: 13
    Last Post: 12-26-2006, 04:17 PM
  4. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  5. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM

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