Help with this C++ coding, I\'m using Microsoft visual studio OOP, operator over
ID: 3852895 • Letter: H
Question
Help with this C++ coding, I'm using Microsoft visual studio
OOP, operator overloading, constructors (copy constructor, default constructor etc.), destructors, ‘this’ pointer, friendship relation, and static variables.
String Class implementation. DO NOT USE/INCLUDE string type (e.g. #include <string>)
Objective: Learning design and use of object oriented programming, operator overloading, constructors (copy constructor, default constructor etc.), destructors, ‘this’ pointer, friendship relation, and static variables.
Input: String or strings (e.g. s1+s2 will return concatenation of strings s1 and s2)
Output: Required operations’ outputs e.g. s1>s2 returns true or false, s1[2] returns third char in string s1, s1.Length() returns length of s1 etc.
Project Description: You are required to create your own String class (Note: String class is different than built in string class) like string type in C++. This class should include following operations:
(10 Points) Separate specifications from implementation. Keep header file and class implementation in separate files.
(20 Points) Can initialize created object with a given string or given instance of the same class (use copy constructor)
(10 Points) Dynamically allocates memory for a given string (e.g. String s(“Joe”); in this case allocates 3 memory spaces for String class instance s)
(10Points) Returns string length if it is requested (e.g. s.Length())
(10 Points) Clears dynamically allocated memory for String object while destructing (do it in destructor)
(20 Points) Checks boundaries (e.g. if String s size is 10, s[11]= ‘A’; will display error message on screen)
(20 Points) In public section of the String class only include interfaces. All other variables and functions must be in private section.
(30 Points) Overloads + operator so that two string can be concatenated. (e.g. String s1,s2,s3; s3 = s1+s2; ) when only have s1+s2; concatenated string will be included in s1.
(30 Points) Overloads [ ] operator so that s[index] will be possible.
(30 Points) Compares two given objects (e.g. String a,b) a>b or a<b or a==b. To do that overload <,>,== operators
(30 Points) Copies one object to another (e.g. a=b or a=b=c) (overload ‘=’ operator for this purpose)
(20 Points) When required returns number of objects created so far. (e.g. String obj; obj.count()). Obj.count() returns total number of non-empty String objects).
(30 Points) Can get input from the istream class instance (e.g. cin>>s;). Make it friend of the String class.
(30 Points) Can output to the ostream class instance (e.g. cout<<s;). Make it friend of the String class.
You may use below given class definition as a reference. Complete code for IntArray class is given under Sample Codes folder in Blackboard.
class IntArray
{ private:
int *aptr; // Pointer to the array int arraySize; // Holds the array size void subscriptError(); // Handles invalid subscripts public:
IntArray(int); // Constructor
IntArray(const IntArray &); // Copy constructor
~IntArray(); // Destructor
int size() const // Returns the array size { return arraySize; }
int &operator[](const int &); // Overloaded [] operator friend ostream& operator <<(ostream& stream,IntArray ob); };
Explanation / Answer
Program in C++:
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
class StringOperations
{
char str[20];
int len;
public:
StringOperations() { len=0; }
void read()
{
cout<<" Enter the string : ";
cin>>str;
}
int operator ~()
{
int i;
len=0;
for(i=0;str[i]!='';i++)
{
len++;
}
return(len);
}
friend istream &operator >>(istream &in,StringOperations &s)
{
int i;
~s;
cout<<" Reverse string: ";
for(i=s.len-1;i>=0;i--)
{
cout<<s.str[i];
}
}
void operator ==(StringOperations s)
{
read();
int i;
char str2[20];
for(i=0;str[i]!='';i++)
str2[i]=str[i];
str2[i]='';
cout<<" Copied String : ";
cout<<str2;
}
void operator +(StringOperations s2)
{
~*this;
int j=len;
for(int i=0;s2.str[i]!='';i++)
{
str[j]=s2.str[i];
j++;
}
str[j]='';
cout<<str;
}
void operator =(StringOperations s2)
{
int i,f=0;
for(i=0;str[i]!='';i++)
{
if(str[i]==s2.str[i])
{
f=1;
}
else
f=0;
break;
}
if(f==1)
cout<<" Stings are equal!";
else
cout<<" Strings are not equal";
}
void operator ||(StringOperations s2)
{
int i,j=0,f=0,count=0;
cout<<" Positions of occurence: ";
for(i=0;str[i]!='';i++)
{
if(str[i]==s2.str[j])
{
j++;
}
else
{
j=0;
}
if(s2.str[j]=='')
{
f=1;
count++;
cout<<" "<<(i-j+2);
j=0;
}
}
cout<<" No of occurence of substring: "<<count;
}
friend ostream &operator <<(ostream &out,StringOperations &s)
{
out<<s.str;
return(out);
}
};
int main()
{
int c,l;
char m;
StringOperations s1,s2;
do
{
cout<<" MENU: 1.Length of the string 2.Copy 3.Reverse 4.Concatination 5.Compare two strings 6.Find Substring 7. Exit Choice= ";
cin>>c;
switch(c)
{
case 1: s1.read();
l=~s1;
cout<<" Length of the string is: "<<l;
break;
case 2: s1==s2;
break;
case 3: s1.read();
cin>>s1;
break;
case 4: cout<<"First String :"; s1.read();
cout<<"Second String :"; s2.read();
s1+s2;
break;
case 5: cout<<"First String :"; s1.read();
cout<<"Second String :"; s2.read();
s1=s2;
break;
case 6: cout<<"Original String :"; s1.read();
cout<<"Substring to search :"; s2.read();
s1||s2;
break;
case 7: exit(1);
break;
default:cout<<"Invalid";
break;
}
//cout<<" Do you want to continue?-y/n: ";
//cin>>m;
}while(c!=7);
return 0;
}
Output screenshot:
MENU:
1.Length of the string
2.Copy
3.Reverse
4.Concatination
5.Compare two strings
6.Find Substring
7. Exit
Choice= 1
Enter the string : well
Length of the string is: 4
MENU:
1.Length of the string
2.Copy
3.Reverse
4.Concatination
5.Compare two strings
6.Find Substring
7. Exit
Choice= 2
Enter the string : hel
Copied String : hel
MENU:
1.Length of the string
2.Copy
3.Reverse
4.Concatination
5.Compare two strings
6.Find Substring
7. Exit
Choice= 3
Enter the string : rev
Reverse string: ver
MENU:
1.Length of the string
2.Copy
3.Reverse
4.Concatination
5.Compare two strings
6.Find Substring
7. Exit
Choice= 4
First String :
Enter the string : hai
Second String :
Enter the string : hello
haihello
MENU:
1.Length of the string
2.Copy
3.Reverse
4.Concatination
5.Compare two strings
6.Find Substring
7. Exit
MENU:
1.Length of the string
2.Copy
3.Reverse
4.Concatination
5.Compare two strings
6.Find Substring
7. Exit
Choice= 6
Original String :
Enter the string : wel
Substring to search :
Enter the string : we
Positions of occurence: 1
No of occurence of substring: 1
MENU:
1.Length of the string
2.Copy
3.Reverse
4.Concatination
5.Compare two strings
6.Find Substring
7. Exit
Choice= 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.