working with OOP: Just return your answers in two separate text documents Answer
ID: 3560211 • Letter: W
Question
working with OOP: Just return your answers in two separate text documents
Answer the following questions based on the below String class. Explain, with reasons, the results of running the program by incorporating the following changes. If any of the followings do not run, write your explanation for it.
1. What do you understand from encapsulation and how it is incorporated in the code?
2. What do you understand from information hiding and how it is incorporated in the code?
3. Make changes to any of the three constructors so as to return a value on successful initialization of the object. What do you realize?
4. If any of the constructors are:
5. Add a fourth default constructor that does not initialize any variables.
6. Replace the keyword 'class' with 'struct' in the program. Comment on the difference between the two implementations. What are the differences?
Program of String.cpp :
Note: When you do implementation of String class, separate specifications from implementation (e.g. String.h, String.cpp, and main.cpp)
#include <iostream>
#include <string>
using namespace std;
class String {
char *char_ptr; // pointer to string contents
int length; // length of string in characters
public:
// three different constructors
String(char *text); // constructor using existing string
String(int = 80); // creates default empty string
String(String& Other_String); // for assignment from another
// object of this class
~String() {delete [] char_ptr; cout<<
Explanation / Answer
1.
Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.
Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.
C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. We already have studied that a class can contain private, protected and publicmembers. By default, all items defined in a class are private.
2. In the class string there are three sections namely public, private and protected.
All the memebr functions are public and variables are declared as private, which makes it unavalible outside the class.
*char_ptr, length are hidden ie; private
3. Even though you try to return value in constructor. Constructors are functions that do not return a value not even void;
4. The constructor will not be invoked because it is not accessible to the program, that's what private means. If you declared a member function or variable private, you would not be able to access them either.
5. String();
This is a constructor without any arguments, called as default constructor.
Even if you dont intialize a constructor, when an object of the class is created, by default a constructor is created by the class.
6. it can implement simply by replacing the keyword class to struct String;
but there arises certain diffrences in both the implementations
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.