Software & Finance





C++ - Data Hiding





Data Hiding is a concept in C++. In C Language Struct can not hide data of its data members. Where as in C++, we got the luxary of hiding data using e different access levels - private, protected, public

Private members and functions can only be accessed by class members of the class. You can think the data abstraction is also using data hiding techniques. Protected members can be accessed only by the class and its derived classes. Note: By using friend keyword, functions and classes will get access to the private and protected members of the classes.

 

class CStudent

{

private:

    char name[128];

    int age;

    char addr1[128];

    char addr2[128];

    char city[32];

    char zipcode[10];

 

public:

    CStudent() {}

    ~CStudent() {}

 

    const char* GetName() const { return name; }

    const char* GetAddr1() const { return addr1; }

    const char* GetAddr2() const { return addr2; }

    const char* GetCity() const { return city; }

    const char* GetZipCode() const { return zipcode; }

    int GetAge() const { return age; }

 

    void SetName(const char* p) { strcpy(name, p); }

    void SetAddr1(const char* p) { strcpy(addr1, p); }

    void SetAddr2(const char* p) { strcpy(addr2, p); }

    void SetCity(const char* p) { strcpy(city, p); }

    void SetZipCode(const char* p) { strcpy(zipcode, p); }

    void GetAge(int v) { age = v; }

};