Using getter and setter in Class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Using getter and setter in Class

  1. #1
    Join Date
    Dec 2010
    Posts
    12

    Angry Using getter and setter in Class

    Basically I'm pretty new to C++, I'm trying to implement the getting and setter function within a class data member using a struct, I got no idea where to start. I've looked around the internet but there's nothing that I can see. Here is the code: (Can you keep any explanations extremely simple)

    <code>

    #include "stdafx.h"
    #include <fstream>
    #include <iostream>
    #include <math.h>
    #include <stdio.h>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <sstream>
    using namespace std;

    #define _CRTDBG_MAP_ALLOC
    #include <crtdbg.h>


    struct SEmployee
    {
    string name;
    int id;
    };

    class CPayRoll
    {
    private:
    SEmployee* mp;
    public:
    CPayRoll( string name, int id );
    ~CPayRoll();
    void SetDetails( string name, int id );
    void GetDetails( string& name, int& id );
    };

    CPayRoll::CPayRoll( string name, int id )
    {
    mp = new SEmployee;

    mp->name = name;
    mp->id = id;
    }

    CPayRoll::~CPayRoll()
    {
    delete (mp);
    }

    void SetDetails( string name, int id )
    {

    }

    void GetDetails( string& name, int& id )
    {

    }

    int main()
    {
    CPayRoll* Olly = new CPayRoll("Olly",100);



    cout << endl;
    system("pause");

    delete(Olly);
    _CrtDumpMemoryLeaks();
    }

    </code>

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Using getter and setter in Class

    Welcome to CodeGuru.

    Code tags use [], not <>.

    getting and setter function within a class data member using a struct, I got no idea where to start.
    'Get' and 'Set' functions are just part of function name... nothing more, nothing less. So what is it what you want to know about function names ?

  3. #3
    Join Date
    Dec 2010
    Posts
    12

    Re: Using getter and setter in Class

    Ahh thanks,

    Well how do I implement the Getter and Setter using a struct?

    I've tried cout << = *mp->name;

    But I just get lots of errors :-/

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,118

    Re: Using getter and setter in Class

    Quote Originally Posted by abakiz View Post
    Ahh thanks,

    Well how do I implement the Getter and Setter using a struct?

    I've tried cout << = *mp->name;

    But I just get lots of errors :-/
    Your question on the surface really makes little sense. Typically a Set method just takes a value as an argument and assigns it to a member of a class or struct. A Get method just returns a member of a class or struct.

    So, what are you really trying to do. Explain what you want to do, not how you want to do it.

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Using getter and setter in Class

    I've tried cout << = *mp->name;
    cout writes data to the terminal window, so that has nothing to do with what you want.

    A class and a struct are almost the same thing, so you can add get and set functions to your struct the same way you did in the CPayRoll class.

  6. #6
    Join Date
    Dec 2010
    Posts
    12

    Re: Using getter and setter in Class

    But how do I do this?

    I mean how do I relate the struct created in the constructor to Get function?

  7. #7
    Join Date
    Dec 2010
    Posts
    12

    Re: Using getter and setter in Class

    Get and set functions are just a public interface for getting read/write access to private data members:

    How to I implement this with struct in the private data member of the class.

  8. #8
    Join Date
    Dec 2010
    Posts
    12

    Re: Using getter and setter in Class

    I forgot to link the getter and setter to the class ::

    Thanks anyhow!

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,118

    Re: Using getter and setter in Class

    Quote Originally Posted by abakiz View Post
    But how do I do this?

    I mean how do I relate the struct created in the constructor to Get function?
    I'll say again, say what you're trying to do not how you want to do it, because your question still isn't making sense.

    Code:
    struct SomeStruct
    {
      ...
        int m_member;
        void SetMember(int nValue)
        {
            m_member = nValue;
        }
        int GetMember()
        {
            return m_member;
        }
    };

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center