2

How can i return the private char "n" from the member function to the main function?

#include <iostream>
using namespace std;
class TEST
{
    char n[10];
public:
    char getname()
    {
        cout<<"what's your name?:";
        cin.getline(n,10);
        return n;
    }
};
int main()
{   char name[10];
    TEST obj;
    name[10]=obj.getname();
    cout<<"Name :"<<name;
}
Kar Wai
  • 45
  • 6
  • 1
    How about a `std::string`, or is there an arbitrary restriction against it? – chris Aug 12 '16 at 14:17
  • Do you really think it matters that it is private? – juanchopanza Aug 12 '16 at 14:18
  • You can't return any array from any function, nor can you assign to an array. Also, `name[10]` is an out-of-bounds operation. There's a list of good books [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Aug 12 '16 at 14:21
  • @juanchopanza I actually don't really know... maybe I need to use a pointer or something but I don't know how. – Kar Wai Aug 12 '16 at 14:21
  • @KarWai So make it public and see? Problem solving 101. – juanchopanza Aug 12 '16 at 14:21
  • Your first problem is that `n` is not a `char`. It's a `char[10]`. Your use of "`name[10]`" makes me reckon you think the name is "name[10]" and the type is "char". It's not! – Lightness Races in Orbit Aug 12 '16 at 14:25
  • @LightnessRacesinOrbit alright i think i understand it, sorry if it's a silly question I'm still new to c++ hahaha... thanks btw – Kar Wai Aug 12 '16 at 14:43

2 Answers2

1

private or public is not matter. your problem is return char instead of char*.

I suggest you to use string instead of char array.

#include <iostream>
using namespace std;
class TEST
{
    char n[10];
public:
    char* getname()
    {
        cout<<"what's your name?:";
        cin.getline(n,10);
        return n;
    }
};
int main()
{   
    char* name;
    TEST obj;
    name = obj.getname();
    cout<<"Name :"<<name;
}
sorosh_sabz
  • 2,356
  • 2
  • 32
  • 53
1

How can i return the private char "n" from the member function to the main function?

You can return pointer to first element of character array which is under private label in your class. For it, you need to change the function definition of your getname function to char *getname().

Also, i would like to point out some error you have made in main function. Have a look at my version:

#include <iostream>
using namespace std;
class TEST
{
   char n[10];
public:
    char* getname()
    {
        cout<<"what's your name?:";
        cin.getline(n,10);
        return n;
   }  
};
int main()
{   
   TEST obj;
   char *name = obj.getname();
   cout<<"Name :"<<name;
}

But it is not a good practice to return a reference or pointer to private member of your class. Exposing any of your internal implementation details is potentially a violation of encapsulation. Instead of providing access to private data members of a class, you can define member functions which will perform desired operation on your private data members.

Also, try to avoid using using namespace.

abhiarora
  • 9,743
  • 5
  • 32
  • 57