Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Explain how encapsulation and data hiding are incorporated in C++ classes. Why a

ID: 3528014 • Letter: E

Question

Explain how encapsulation and data hiding are incorporated in C++ classes. Why are these concepts important in object-oriented programming? Explain the basic C++ syntax for creating a class. Include an explanation of the private and public section of a class and class members. Include a code segment to illustrate your answer.

Explanation / Answer

Classes are used to create user defined types. An instance of a class is called an object and programs can contain any number of classes. As with other types, object types are case-sensitive. Classes provide encapsulation as defined in the Object Oriented Programming (OOP) paradigm. A class can have both data members and functions members associated with it. Unlike the built-in types, the class can contain several variables and functions, those are called members. Classes also provide flexibility in the "divide and conquer" scheme in program writing. In other words, one programmer can write a class and guarantee an interface. Another programmer can write the main program with that expected interface. The two pieces are put together and compiled for usage. Note: From a technical viewpoint, a struct and a class are practically the same thing. A struct can be used anywhere a class can be and vice-versa, the only technical difference is that class members default to private and struct members default to public. Structs can be made to behave like classes simply by putting in the keyword private at the beginning of the struct. Other than that it is mostly a difference in convention. The C++ standard does not have a definition for method. When discussing with users of other languages, the use of the word method to represent a member function can at times become confusing or raise problems to interpretation, like referring to a static member function as a static method. It is even common for some C++ programmers to use the term method to refer specifically to a virtual member function in an informal context. class MyClass { /* public, protected and private variables, constants, and functions */ }; by default, all class members are initially private. keywords public and protected allow access to class members. classes contain not only data members, but also functions to manipulate that data. a class is used as the basic building block of OOP (this is a distinction of convention, not of language-enforced semantics). A class can be created before main() is called. when a function is called in which the object is declared. when the "new" operator is used. Class Names Name the class after what it is. If you can't determine a name, then you have not designed the system well enough. Compound names of over three words are a clue your design may be confusing various entities in your system. Revisit your design. Try a CRC card session to see if your objects have more responsibilities than they should. Avoid the temptation of naming a class something similar to the class it is derived from. A class should stand on its own. Declaring an object with a class type doesn't depend on where that class is derived from. Suffixes or prefixes are sometimes helpful. For example, if your system uses agents then naming something DownloadAgent conveys real information. Data Abstraction A fundamental concept of Object Oriented (OO) recommends an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. The class, by design, allows its programmer to hide (and also prevents changes as to) how the class is implemented. This powerful tool allows the programmer to build in a 'preventive' measure. Variables within the class often have a very significant role in what the class does, therefore variables can be secured within the private section of the class. [edit]Access labels The access labels Public, Protected and Private are used within classes to set access permissions for the members in that section of the class. All class members are initially private by default. The labels can be in any order. These labels can be used multiple times in a class declaration for cases where it is logical to have multiple groups of these types. An access label will remain active until another access label is used to change the permissions. We have already mentioned that a class can have member functions "inside" it; we will see more about them later. Those member functions can access and modify all the data and member function that are inside the class. Therefore, permission labels are to restrict access to member function that reside outside the class and for other classes. For example, a class "Bottle" could have a private variable fill, indicating a liquid level 0-3 dl. fill cannot be modified directly (compiler error), but instead Bottle provides the member function sip() to reduce the liquid level by 1. Mywaterbottle could be an instance of that class, an object. /* Bottle - Class and Object Example */ #include #include using namespace std; class Bottle { private: // variables are modified by member functions of class int iFill; // dl of liquid public: Bottle() // Default Constructor : iFill(3) // They start with 3 dl of liquid { // More constructor code would go here if needed. } bool sip() // return true if liquid was available { if (iFill > 0) { --iFill; return true; } else { return false; } } int level() const // return level of liquid dl { return iFill; } }; // Class declaration has a trailing semicolon int main() { // terosbottle object is an instance of class Bottle Bottle terosbottle; cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote