Classes and member functions

This is a discussion on Classes and member functions within the C++ Programming forums, part of the General Programming Boards category; Hi Folks, I'm still trying to get my head around Classes... Specifically, I am having problems defining functions within a ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    4

    Classes and member functions

    Hi Folks,
    I'm still trying to get my head around Classes...

    Specifically, I am having problems defining functions within a class. I can only seem to define them outside a class. If i put them in a class I get compiler errors.

    Here is my code. How would I change this to put definitions within the person class?

    Also, I get errors if I define the destructor just as ~person();

    Code:
    ////////////////////////////////////////////////////
    // Demonstrates Class, Member Data, Member Functions
    ////////////////////////////////////////////////////
    
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    ///////////////////////////////////////
    // Define person class
    ///////////////////////////////////////
    
    class person
    {
        public:
               /////////////////////////////////
               // constructor
               /////////////////////////////////
               
               person(int age)
               {
               itsname="undefined";
               itsage=age;
               }
               
               /////////////////////////////////              
               // destructor
               /////////////////////////////////
               
              ~person()
              {
              
              }
        
               /////////////////////////////////
               // function prototypes
               /////////////////////////////////
    
               void setage(int age);
               int getage();
               void setname(string name);
               string getname();
               
    
        private:
                string itsname;
                int itsage;
    };
            
    /////////////////////////////////////////////////////////////////////////
    
    
               /////////////////////////////////
               // function definitions           
               /////////////////////////////////
               
               
               void person::setage(int age)
                    {
                    itsage=age;
                    }
                    
               int person::getage()
                    {
                    return itsage;
                    }
                   
               void person::setname(string name)
                    {
                    itsname=name;
                    }
                    
               string person::getname()
                    {
                    return itsname;
                    }
                                   
    /////////////////////////////////////////////////////////////////
    
    
    
    ///////////////////////////////////////
    // Main Program
    ///////////////////////////////////////
            
    int main()
    {
                
                /////////////////////////////////////////
                // test class
                /////////////////////////////////////////
                
            
                // create person class called test
            
                person test(9999);                                      
            
            
                // change member variables and print to test
            
                cout << "initial age = " << test.getage() << "\n";    
                
                test.setage(13);
                cout << "altered age = " << test.getage() << "\n";
                
                cout << "initial name = " << test.getname() << "\n";
                
                test.setname("bobbins");
                cout << "altered name = " << test.getname() << "\n";
                
                    
                /////////////////////////////////////////
                // wait for user to end program
                /////////////////////////////////////////
                   
                cin.get();
                return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,287
    Code:
    class person
    {
        public:
               
               person(int age)
               {
               itsname="undefined";
               itsage=age;
               }
               
               /////////////////////////////////              
               // destructor
               /////////////////////////////////
               
              ~person()
              {
              
              }
        
               void setage(int age)
               {
                    itsage=age;
               }
    
               int getage()
              {
                    return itsage;
              }
               
               void setname(string name)
               {
                    itsname=name;
               }
    
               string getname()
               {
                    return itsname;
               }
               
    
        private:
                string itsname;
                int itsage;
    };
    I would also add a few const qualifiers, and place semi-colons between the function definitions, but that's my personal preference.

    And I would avoid having "using namespace std;" before the class declaration (yes, that means typing "std::string" instead of "string"). That is a stylistic thing, but has a significant chance of becoming a technical problem if the class declaration is moved into its own header file in future, and that header is #include'd by multiple source files.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    4
    Got it!

    Many thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes as member variables
    By Stonehambey in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2008, 07:01 PM
  2. Classes being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 10:19 AM
  3. Classes with Other Classes as Member Data
    By njd in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2005, 09:30 AM
  4. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  5. help with classes and member functions
    By syner in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2002, 08:45 PM

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