1. C++ does not have an interface keyword like Java does. How can you make the e
ID: 3837577 • Letter: 1
Question
1. C++ does not have an interface keyword like Java does. How can you make the equivalent in C++?
2. What do you write to find the length of a string when the string variable is declared like this:
std::string someString;
3. What do you write to find the length of a string when the string variable is declared like this:
std::string *someString;
4.
How often must you use the delete keyword in a C++ program?
What happens if you use it too few times?
What happens if you use it more than enough times?
What is the Java equivalent of delete?
Explanation / Answer
Question 1:
C++ doesn't have inteface keyword like Java does. C++ uses inheritance and polymorphism to acheive this feature. In C++, we define one base class which is an abstract class i.e, whose object is not created. This base class must have pure virtual function i.e, a function that is similar to derived class functions. pure virtual functions contains nothing, they are used to provide an interface to the derived class functions.
Then, we define more than one derived classes which consists of similar functions like pure virtual functions of base class.
Now, we declare one base class pointer which can point to any base or derived class objects. Using this method, we can call the required function we want.
For example:
class Base
{
public:
virtual void show(){} //Virtual function acts as interface.
};
class Derived1
{
public:
void show(){
cout<<" Derived1";
}
};
class Derived2
{
public:
void show()
{
cout<<" Derived2";
}
};
int main()
{
Derived1 D1;
Derived2 D2;
Base *B;
//Choosing to work with Derived1.
B = &D1;
B->show(); //calls show() from derived class Derived1
//Choosing to work with Derived2.
B = &D2;
B->show(); //calls show() from derived class Derived2
return 0;
}
Question 2:
std::string someString;
int len = someString.length(); //Returns length of the string
Question 3:
std::string *someString;
This is a string pointer. To calculate length we should write:
int len = someString->length();
Question 4:
i) delete keyword is used to deallocate memory that is being pointed to by a pointer which is initialized by the new operator. delete operator calls free() function to deallocate memory.
We should use delete keyword only we're sure that we don't need the pointer. This is generally when we call destructor in the class.
ii) If we don't use delete keyword then the program might enter into memory leak. All of the memory gets allocated to pointers and are not get freed when not needed. Now, if this happens for long, it may happen that there is no more memory left for the program.
iii) If we use delete more than enough then it'll slow down the process as the program has to deallocate memory frequently. Not just that, in this case, we'll have to use the new operator very frequently too. Thus, in this case, time complexity increases.
iv) Java do not posses any direct equivalent of this delete operator. We cannot tell JVM to deallocate memory. JVM cleans up memory automatically according to its convenience when an object is no longer referenced.
In java, there is a mechanism called finalization which is a clean up function with a low guarantee of being called.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.