How to access string of private in class

i need access string name in private to sort name. So Can i access string name like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Student {
      private:
		char name[30];
		char as[10];
		float aver_grade;
      public:
		//Input information of student
		void InputStudent(){
			cout<<"Enter account student: ";
			cin>>as;
			cout<<"Enter name: ";
			cin>>name;
			cout<<"Enter average grade: ";
			cin>>aver_grade;
		}
		//Display information of student
		void DisplayStudent(){
			cout<<"Account Student: "<<as<<endl;
			cout<<"Name: "<<name<<endl;
			cout<<"Average grade: "<<aver_grade<<endl;
		}
		float average_grade(){return aver_grade;}
		char name_student(){ return name;}// This
}
  

That would access the private char string, just make sure you return a char array also otherwise it's returning just a char.
@crimsonzero2 , how i can return string name in private?
So, what you have right now would return an array character. If it was returning the char array, it would give that to whatever is calling it.

char x[] = student.name_student();

To just change the array, just say " name = <the string you want>;" and change the char to void in the front of the function. I personally think the string datatype would be much more helpful to you as well.

So instead of char[30] name;, I would recommend string name;. And don't forget the #include <string> to use it.
Last edited on
Topic archived. No new replies allowed.