RedDucati748
MIS
I'm trying to use a base class from another project and create a derived class with three new functions for left/mid/right. I'm still receiving a few errors. What am I missing in my code? Any pointers or hints would help.
Thanks-
#include <iostream>
#include <string>
using namespace std;
////////////////////////////////////////////////
class String
{
protected:
enum {SZ=80};
char str[SZ];
public:
String getstring()
{
cin.get(str, SZ);
return str;
}
String()
{ str[0] = '\0'; }
String( char s[] )
{ strcpy(str, s); }
void display()
{ cout << str; }
operator char* ()
{return str;}
bool operator == (String st) const
{return ( strcmp (str, st.str)==0 ) ? true : false;}
bool operator < (String st) const
{return ( strcmp (str, st.str)<0 ) ? true : false;}
bool operator > (String st) const
{return ( strcmp (str, st.str)>0 ) ? true : false;}
bool operator <= (String st) const
{return ( strcmp (str, st.str)<=0 ) ? true : false;}
bool operator >= (String st) const
{return ( strcmp (str, st.str)>=0 ) ? true : false;}
bool operator != (String st) const
{return ( strcmp (str, st.str)!=0 ) ? true : false;}
};
////////////////////////////////////////////////
class Pstring2: public String
{
public:
Pstring2() : String() {} ;
Pstring2(char s[ ]): String(s) {};
void right(Pstring2, int l);
void left(Pstring2, int l);
void mid(Pstring2, mid, int l);
};
void Pstring2::right(Pstring2 s, int n)
{
int i,j;
for (j=0, i= n<strlen(s) ? strlen(s)-n : 0;
i<=strlen(s);
i++,j++)
str[j]=s.str;
str[j] = '\0';
}
void Pstring2::left(Pstring2 s, int n)
{
int i,j;
for (j=0, i= n<strlen(s) ? strlen(s)-n : 0;
i<=strlen(s);
i--,j++)
str[j]=s.str;
str[j] = '\0';
}
void Pstring2::mid(Pstring2 s, int m, int n)
{
int i,j;
for (j=0, i= n<strlen(s) ? strlen(s)-n : 0;
i<=strlen(s);
i--,j++)
str[j]=s.str[n+i-1];
str[j+1] = '\0';
}
//////////////////////////////////////////////////////
void main()
{
Pstring2 s1, s2;
int length; mid;
cout<<"Enter phrase: ";
s1.getstring();
cout<<"Enter concatenation length: ";
cin>>length;
s2.right(s1,length);
cout << "\nPhrase now reads: " ; s2.display();
cout<<"\n\n";
cout<<"Enter phrase: ";
s1.getstring();
cout<<"Enter concatenation length: ";
cin>>length;
s2.left(s1,length);
cout << "\nPhrase now reads: " ; s2.display();
cout<<"\n\n";
cout<<"Enter phrase: ";
s1.getstring();
cout<<"Enter concatenation length: ";
cin>>length;
cin>>mid;
s2.mid(s1, mid, length);
cout << "\nPhrase now reads: " ; s2.display();
cout<<"\n\n";
}
Thanks-
#include <iostream>
#include <string>
using namespace std;
////////////////////////////////////////////////
class String
{
protected:
enum {SZ=80};
char str[SZ];
public:
String getstring()
{
cin.get(str, SZ);
return str;
}
String()
{ str[0] = '\0'; }
String( char s[] )
{ strcpy(str, s); }
void display()
{ cout << str; }
operator char* ()
{return str;}
bool operator == (String st) const
{return ( strcmp (str, st.str)==0 ) ? true : false;}
bool operator < (String st) const
{return ( strcmp (str, st.str)<0 ) ? true : false;}
bool operator > (String st) const
{return ( strcmp (str, st.str)>0 ) ? true : false;}
bool operator <= (String st) const
{return ( strcmp (str, st.str)<=0 ) ? true : false;}
bool operator >= (String st) const
{return ( strcmp (str, st.str)>=0 ) ? true : false;}
bool operator != (String st) const
{return ( strcmp (str, st.str)!=0 ) ? true : false;}
};
////////////////////////////////////////////////
class Pstring2: public String
{
public:
Pstring2() : String() {} ;
Pstring2(char s[ ]): String(s) {};
void right(Pstring2, int l);
void left(Pstring2, int l);
void mid(Pstring2, mid, int l);
};
void Pstring2::right(Pstring2 s, int n)
{
int i,j;
for (j=0, i= n<strlen(s) ? strlen(s)-n : 0;
i<=strlen(s);
i++,j++)
str[j]=s.str;
str[j] = '\0';
}
void Pstring2::left(Pstring2 s, int n)
{
int i,j;
for (j=0, i= n<strlen(s) ? strlen(s)-n : 0;
i<=strlen(s);
i--,j++)
str[j]=s.str;
str[j] = '\0';
}
void Pstring2::mid(Pstring2 s, int m, int n)
{
int i,j;
for (j=0, i= n<strlen(s) ? strlen(s)-n : 0;
i<=strlen(s);
i--,j++)
str[j]=s.str[n+i-1];
str[j+1] = '\0';
}
//////////////////////////////////////////////////////
void main()
{
Pstring2 s1, s2;
int length; mid;
cout<<"Enter phrase: ";
s1.getstring();
cout<<"Enter concatenation length: ";
cin>>length;
s2.right(s1,length);
cout << "\nPhrase now reads: " ; s2.display();
cout<<"\n\n";
cout<<"Enter phrase: ";
s1.getstring();
cout<<"Enter concatenation length: ";
cin>>length;
s2.left(s1,length);
cout << "\nPhrase now reads: " ; s2.display();
cout<<"\n\n";
cout<<"Enter phrase: ";
s1.getstring();
cout<<"Enter concatenation length: ";
cin>>length;
cin>>mid;
s2.mid(s1, mid, length);
cout << "\nPhrase now reads: " ; s2.display();
cout<<"\n\n";
}