c++ - “class::data member is private” error -
who can check “class::data member private” error.
#include <iostream> using namespace std; class marks { private: char * name; char * grade; float gpa; public: char* set_name(char * n) { name=n; cout<<"enter name :"; cin>>name; return name; } float set_gpa(float g) { gpa=g; cout<<"enter cgpa :"; cin>>gpa; return gpa; } char set_grade() { if(gpa>=3.00&&gpa<=4.00) { grade[1]='a'; } else if(gpa>=2.00&&gpa<=2.99) { grade[1]='b'; } else if(gpa>=0.00&&gpa<=1.99) { grade[1]='f'; } else { cout<<"error"<<endl; } return grade[1]; } }m1; int main() { char name[20]; char grade[1]; float gpa; cout<<"=================================="<<endl; cout<<" enter information "<<endl; cout<<"=================================="<<endl; m1.set_name(name); m1.set_gpa(gpa); m1.set_grade(); cout<<"=================================="<<endl; cout<<" result slip "<<endl; cout<<"=================================="<<endl; cout<<"name : "<<m1.name; cout<<"gpa : "<<m1.gpa; cout<<"grade : "<<m1.grade; return 0; }
error list
in function 'int main()':| line 9|error: 'char* marks::name' private| line 85|error: within context| line 11|error: 'float marks::gpa' private| line 86|error: within context| line 10|error: 'char* marks::grade' private| line 87|error: within context| line 70|warning: unused variable 'grade'| ||=== build finished: 6 errors, 1 warnings ===|
have @ definition of class marks
:
private: char * name; char * grade; float gpa;
all 3 members declared private, means can accessed class' member functions - done, example, in set_
functions. if want access data outside of context of class itself, define get_
functions or make data public - depending on want do.
also, since c++, should using std::string
strings.
Comments
Post a Comment