Here is the OOP Assignment No 03 Functionality of Strings
Question:
We often make use of built-in string data-type that in addition to serving as character array, also provides many useful functions. In this assignment, you are required to mimic the functionalities of built-in string data-type by writing your own class named String. The String class should have a character pointer as its only attribute/member-variable.
- Provide a parameterized constructor that should receive a string literal that should further be passed to another member-function named Length( ) that is supposed to determine and return the length/size of it. Based on returned length, dynamic memory allocation needs to be done in the constructor which should be pointed by the character pointer member-variable. After memory allocation, next another member-function named Copy( ) should be invoked from constructor which should receive the string literal as well as its length as parameters and should copy all characters from the string literal into the address pointed by the character pointer.
- Write the code for overloading % operator using a member-function that should receive a string literal as a parameter and return a new string holding the content stored in character pointer but must not have the string literal passed as parameter.
- Write the code for overloading + operator that should concatenate two String objects that return a new object holding the concatenated string in its member-variable.
- Write the code for a Print( ) member function that should print the contents stored in the character pointer.
- Write the code in the main() to demonstrate the usage of the user-defined String class. You must write all the required code to ensure the usage of the various functions that you have defined inside your String class.
Code:
The below is the code for OOP Assignment No 03 Functionality of Strings
#include<iostream> using namespace std; class String { private: char *pointer; public: String(string name) { int number=length(name); pointer= new char [number]; Copy(name,number); } int length(string na) { int n= na.length(); return n; } void Copy(string Name,int num) { for(int i=0;i<num;i++) { pointer[i]=Name[i]; } } String operator% (string minus) { string temporary=pointer; int l1= temporary.length(); int l2= minus.length(); int j=temporary.find(minus); string temporary2; for(int i=0;i<l1;i++) { if(i<j) temporary2=temporary2+temporary[i]; } for(int i=0;i<l1;i++) { if(i>j+l2) temporary2=temporary2+temporary[i]; } String newobj(temporary2); return newobj; } string operator+(string s2) { string temporary=pointer; return temporary+s2; } void print() { cout<<pointer; } }; int main() { string original_string1,original_string2,original_string3; cout<<"Enter the string : "; getline(cin,original_string1); String s1(original_string1); cout<<"Enter the string to remove : "; getline(cin,original_string2); String s2(original_string2); String s3=s1%original_string2; s3.print(); cout<<endl; cout<<"Enter the string to add: "; getline(cin,original_string3); cout<<"The String After Addition: "<<s3+original_string3; }
For more details about Huffman coding click here
For other assignments and quizzes click here
Output: