Encapsulation

Discussion in 'C++' started by Peter_APIIT, May 4, 2007.

  1. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    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.
     
    Last edited by a moderator: May 4, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Please provide the code block when posting code in posts. I have done that for you.
     
  3. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    I bag your pardon because i realized that when provide code block is it more difficult to check the code.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice