Hi guys. I've worked on OOP before, and since in general 'get' functions are used to return private attributes in classes, I was thinking how I could make this test code work.

#ifndef _testing_h
#define _testing_h

class Testing
{
private:
	char * name;
public:
	Testing();
	Testing(char*);

	~Testing();

	char* getName();
	void setName(char name[]);
}

#endif;

Here's the get function code. It doesn't seem to work. Removing the const helps though, but that would defeat the purpose of having a private attribute.

char* Testing::getName() const
{
	return name;
}

Also, can I directly assign a character array to a pointer?
Something like name = newName when name is a char* type.

Well I really don't get it. Here are few mistakes in your question itself :

1>The class definition should end with a semicolon.You haven't done it.
2>Where are you trying to return the array to ? I mean if you want the array within the function setName then why all this fuss ? All member functions of a class can directly access the member variables even if they are private.
The term private says that those members can't be accessed by other functions,but the class member functions have no restrictions imposed on them,they can easily access any of the member variables.

>Also, can I directly assign a character array to a pointer?
If you are speaking of something like this :

char array[20];
char *p;
p=array;

Then its valid.Not a problem!!!

Your problem, what I am perceiving is, that you want to have an interface for your class which can return the c-string for name but don't let user change it.
Returning a pointer is not a good idea overall. Change your signature of the get function as:

void getName(char* dump);

This way, the user of your class can provide a pointer to char where he wants the content of the name to be transfered to. The function body should look something like this:

void getName(char* dump){
strcpy(dump,name);
}

Now, your user can use this function like this:

Testing t1("Somename");//construct t1
//
//
char name_of_t1[50];//change size to the maximum of what a name can be

t1.getname(name_of_t1);
std::cout<<name_of_t1;

Using C-string( null terminated char array) is never a good idea when you have the standard std::string to handle that.

See what a class of that sort (std::string surrogate) must have:

class Testing
{
public:
  Testing();
  Testing(const char*);
  Testing(const Testing&); // copy constructor
  Testing& operator=(const Testing&); // assignment
  ~Testing();
  ...
  const char* getName() const { return pName; }

  void setName(const char* pname);
  ...
private:
  char* pName;
};

Well I really don't get it. Here are few mistakes in your question itself :

1>The class definition should end with a semicolon.You haven't done it.
2>Where are you trying to return the array to ? I mean if you want the array within the function setName then why all this fuss ? All member functions of a class can directly access the member variables even if they are private.
The term private says that those members can't be accessed by other functions,but the class member functions have no restrictions imposed on them,they can easily access any of the member variables.

>Also, can I directly assign a character array to a pointer?
If you are speaking of something like this :

char array[20];
char *p;
p=array;

Then its valid.Not a problem!!!

Well, the thing is, I was told that for the GCE A levels, the get and set member functions are required, even if they're just studs in some situations. So, what I usually did was returning of name through parameter passing. Such as:

void Testing::getName(char retName[])
{
	strcpy(retName, name);
}

However, using this method means I've to create a field for the name of fixed length, which isn't exactly very efficient.

Your problem, what I am perceiving is, that you want to have an interface for your class which can return the c-string for name but don't let user change it.
Returning a pointer is not a good idea overall. Change your signature of the get function as:

void getName(char* dump);

This way, the user of your class can provide a pointer to char where he wants the content of the name to be transfered to. The function body should look something like this:

void getName(char* dump){
strcpy(dump,name);
}

Now, your user can use this function like this:

Testing t1("Somename");//construct t1
//
//
char name_of_t1[50];//change size to the maximum of what a name can be

t1.getname(name_of_t1);
std::cout<<name_of_t1;

Using C-string( null terminated char array) is never a good idea when you have the standard std::string to handle that.

I kinda get you on the part where:

void getName(char* dump);

This way, the user of your class can provide a pointer to char where he wants the content of the name to be transfered to. The function body should look something like this:

void getName(char* dump){
strcpy(dump,name);
}

However, will this work even if I make the change the function such that it's like this?

void getName(char* dump) const;

Or does it, by default, already prevent the class user from changing the attribute? Sorry if it seems like I'm not trying, but I'm having too many open projects trying to test out the different methods.

Also, how is the std::string better than the c-string? Sorry, but it seems that even though I do A Level C++, I've only been taught C-strings. Anyways, here's my test class and I've a little bit of issues.

Test_classes.h

#ifndef _testing_h
#define _testing_h
#include <string>
using std::string;
class Testing
{
private:
	char* name;
	string name2;
public:
	Testing();
	Testing(char* newName);
	~Testing();

	void setName(char* newName);
	char* getName() const;
	string getName2() const;
	void setName2(string newName);
};
#endif;

Test_classes.cpp

#include <iostream>
#include "Testing.h"
#include <string>
using namespace std;
using std::string;

Testing::Testing()
{};

Testing::Testing(char *newName)
{
	name = newName;
};

Testing::~Testing()
{
	delete name;
}


void Testing::setName(char *newName)
{
	name = newName;
};

char* Testing::getName() const
{
	return name;
};

string Testing::getName2() const
{
	return name2;
}

Driver.cpp

#include "Testing.h"
#include <iostream>
using namespace std;

void main()
{/*
	char* name;
	Testing testObject;
	cout << "Enter name: ";
	cin.getline(name, 255, '\n');
	testObject.setName(name);
	cout << testObject.getName() << endl;*/

	string name;
	Testing testObject;
	cout << "Enter name: ";
	getline(cin, name);
	testObject.setName2(name);
	cout << "Your name is: " << testObject.getName2();
}

And the errors I'm getting are:
Driver.obj : error LNK2019: unresolved external symbol "public: void __thiscall Testing::setName2(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setName2@Testing@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main

and

Visual Studio 2008\Projects\Test_classes\Debug\Test_classes.exe : fatal error LNK1120: 1 unresolved externals

See what a class of that sort (std::string surrogate) must have:

class Testing
{
public:
  Testing();
  Testing(const char*);
  Testing(const Testing&); // copy constructor
  Testing& operator=(const Testing&); // assignment
  ~Testing();
  ...
  const char* getName() const { return pName; }

  void setName(const char* pname);
  ...
private:
  char* pName;
};

What's a surrogate? And what is this

Testing& operator=(const Testing&); // assignment

for?

Well to start of with

void getName(char* dump){
strcpy(dump,name);
}

Only copys the variable name which is your private variable into dump. That way the user would only recieve a copy of your private member and not get access to it .

void getName(char* dump) const{
strcpy(dump,name);
}

The const specifier says that no values should be edited in this function and wouldnt allow dump to be changed. That is not what we want right ! .

And the errors I'm getting are:
Driver.obj : error LNK2019: unresolved external symbol "public: void __thiscall Testing::setName2(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setName2@Testing@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main

This error is because the compiler is actually searching for a function

setname() [/code] which has an argument [icode] string [icode] Which isn't available because we din't write one yet.

Though the std::string gives a lot of secure constructs . I still prefer that you develop the class with c-strings and dont forget to read about c++ strings later.[icode]
setname()
[/code] which has an argument

string [icode] Which isn't available because we din't write one yet.

Though the std::string gives a lot of secure constructs . I still prefer that you develop the class with c-strings and dont forget to read about c++ strings later.[icode]
string

Which isn't available because we din't write one yet.

Though the std::string gives a lot of secure constructs . I still prefer that you develop the class with c-strings and dont forget to read about c++ strings later.[icode]
Which isn't available because we din't write one yet.

Though the std::string gives a lot of secure constructs . I still prefer that you develop the class with c-strings and dont forget to read about c++ strings later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.