// public_private_ex4.cpp // Purpose: to demonstrate compile error // if private member called by mistake... // // Note: This program will NOT compile #include using namespace std; struct Student { // Define derived data type private: int no; char grade[14]; public: void display() const; void set(int n, char* g); }; main() { Student harry; harry.set(975, "ABC"); // calling public member functions set and display harry.display(); cout << harry.no; // ERROR .no IS PRIVATE! } // end of main() program void Student::display() const { cout << no << ' ' << grade << endl; } // end of display() public member function void Student::set(int n, char* g){ int i; no = n; // accept the Student number received // accept Student grades received for (i = 0; g[i] != '\0' && i < 13; i++) grade[i] = g[i]; grade[i] = '\0'; // set the last byte to null } // end of set() function