In C++, implement a string class using a dynamically allocated array. The array
ID: 3806527 • Letter: I
Question
In C++, implement a string class using a dynamically allocated array. The array of char should be null terminating. You must implement the following methods:
Default constructor (allocates 128 characters as capacity)
Constructor that takes an integer value as the capacity to allocate
Copy constructor
Destructor
Reallocate that changes the capacity of a string (larger or smaller) and preserves as much of the contents of the string as possible.
Assignment – overload the operator= with standard C++ semantics.
Constant time swap – swaps two strings in constant time regardless of their capacity
Length
Capacity
You can NOT use STL or any other predefined library, built in types (such as std::string), or C functions (such as strcpy).
Previous
Explanation / Answer
default constructor : for a string class
string();
constructor that takes a aparameter as an integer value
which is a parameterized constructor
string( int capacity);
copy constructor:
string(const string &obj)
destructor();
Reall_string.reallocate(20);
For(loop for parsing )
{
Business logic
Reall_string.push_back(char)
}
#include <iostream>
Class Distance{
Int feet,inches;
Distance()
{feet = 0;inches =0;
}
Distance(int f,int i)
{
Feet = f;
Inches = I;
}
Void operator =(const distance &d){
Feet = D.feet;
Inches =D.inches;
}
Void displayDistance(){
Cout<<”f”<<feet<<”I”<<inches<<endl;
}
};
Int main(){
Distance D1(12,15) , D2(15,19);
Cout<<”first distance:”;
D1.displayDistance();
Cout<<”second distance”
D2.displayDistance();
D1=D2;
Cout<<first distance:;
D1.displaydistance();
Return 0;
}
for all these basic oops concepts analysis and practice will give more knowledge