-
December 1st, 2010, 08:59 AM
#1
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>
-
December 1st, 2010, 09:03 AM
#2
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 ?
-
December 1st, 2010, 09:05 AM
#3
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 :-/
-
December 1st, 2010, 09:10 AM
#4
Re: Using getter and setter in Class
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.
-
December 1st, 2010, 09:12 AM
#5
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.
-
December 1st, 2010, 09:13 AM
#6
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?
-
December 1st, 2010, 09:23 AM
#7
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.
-
December 1st, 2010, 09:29 AM
#8
Re: Using getter and setter in Class
I forgot to link the getter and setter to the class ::
Thanks anyhow!
-
December 1st, 2010, 09:31 AM
#9
Re: Using getter and setter in Class
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;
}
};
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|