Hello, i have a question about encapsulation. Is it necessary to encapsulate the private data by using set and get function because we already have private access specifiers ? Below is my program: Code: // Class, Constructor and Destructor /* Base/Member Initialization - use this method as often as possible because it allows the compiler to create variables and objectly correctly instead of creating them with a default value and to use an additional assignment to set its value. */ #include<iostream> #include<string> using namespace std; using std::string; class Human { string name; int age; public: Human() : name(), age(0){} // Good method Constructors int setage(); string setname(); ~Human(){}; // Inline Destructors }; int main(int argc, char *argv[]) { Human nicholas_tse; for (;;) { cout << "My name is " << nicholas_tse.setname() << endl; cout << "My age is " << nicholas_tse.setage() << endl; cout << endl; } return 0; } string Human::setname() { Human peter; cout << "Enter your name : "; cin >> peter.name; return peter.name; } int Human::setage() { Human peter; cout << "Enter your age : "; cin >> peter.age; return peter.age; } // Polymorphism is a mechanism where base class object and // derived class object doing different things. /* Human \|/ Baby Human - Speaking Baby - Crying */ /*Abstract class is that class that only has function*/ // use get and set to facilitate encapsulation or this pointer. This program run without any errors but the second program has an errors. Below is the program: // Class, Constructor and Destructor /* Base/Member Initialization - use this method as often as possible because it allows the compiler to create variables and objectly correctly instead of creating them with a default value and to use an additional assignment to set its value. */ #include<iostream> #include<string> using namespace std; using std::string; class Human { string name; int age; public: Human() : name(), age(0){} // Good method Constructors void setname(); string getname(); void setage(); int getage(); ~Human(){}; // Inline Destructors }; int main(int argc, char *argv[]) { Human nicholas_tse; for (;;) { nicholas_tse.setname(); nicholas_tse.setage(); cout << "My name is " << nicholas_tse.getname() << endl; cout << "My age is " << nicholas_tse.getage() << endl; cout << endl; } return 0; } void Human::setname() { Human peter; cout << "Enter your name : "; cin >> peter.name; } string Human::getname() { Human peter; return peter.name; } void Human::setage() { Human peter; cout << "Enter your age : "; cin >> peter.age; } int Human::getage() { Human peter; return peter.age; } // Polymorphism is a mechanism where base class object and // derived class object doing different things. /* Human \|/ Baby Human - Speaking Baby - Crying */ /*Abstract class is that class that only has function*/ // use get and set to facilitate encapsulation or this pointer. And this is the reference: #include<iostream> using std::cout; using std::endl; #include "gradebook.h" int main(int argc, char *argv[]) { Gradebook gradebook1("Cs101 Introduction to C++ Programming"); Gradebook gradebook2("Cs102 Data Structures in C++"); cout << "Gradebook1 created for course : " << gradebook1.getCourseName() << endl; cout << "Gradebook2 created for course : " << gradebook2.getCourseName() << endl; return 0; } Gradebook::Gradebook(string name) { setCourseName(name); } void Gradebook::setCourseName(string name) { coursename = name; } string Gradebook::getCourseName() { return coursename; } void Gradebook::displayMessage() { cout << "Welcome to the grade book for " << getCourseName() << endl; } Why the gradebook program works correctly but my second program has an errors. Please explained to me the flow of the gradebook program. What is the purpose of set and get function ? Thanks for your help. Your help is greatly appreciated by me and others.
I bag your pardon because i realized that when provide code block is it more difficult to check the code.