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>
Re: Using getter and setter in Class
Welcome to CodeGuru.
Code tags use [], not <>.
Quote:
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 ?
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 :-/
Re: Using getter and setter in Class
Quote:
Originally Posted by
abakiz
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.
Re: Using getter and setter in Class
Quote:
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.
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?
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.
Re: Using getter and setter in Class
I forgot to link the getter and setter to the class ::
Thanks anyhow!
Re: Using getter and setter in Class
Quote:
Originally Posted by
abakiz
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;
}
};