I've tried so hard to do it but I couldn't so I really need help since
I have to send it to my professor tonight so please HELP

class String{ 

char * const cp; // pointer to the first element 

int Alng; // the N# of the elements in the array [max N# of char is Alng -1 ] 

public: 

String(const char *str, int alng) ; 

~String(); 

const char* getstr()const{ return cp;} 

}; 

class String{

const char *cp; // pointer to the first element 

int Clng; // the length of the contents of the array

int Alng; // the N# of the elements in the array [max N# of char is Alng -1 ]

public: 

String(const char *str, int alng); // constructor

~String();// destructor 

char getstr(){ return cp;}

};

- implement the constructor and destructor

- provide three overloaded operator (two + version, - ) . the effect of these operator as follow

String mst("this is a test",40);

String yst(" message", 15);

mst + yst // to canconat the content of the array of yst to the contents of mst

mst - 3; // cute the last three characters of mst by placing '\0' char

2 + mst; // replace the first two char of mst by 'A' char

- here is a test code and the expected output

void main(){ 

String mst("this is a test",40); 

String yst(" message", 15); 

cout<<"========================="<<endl; 

cout<< "mst "<<mst.getstr()<<endl; 

cout<<"yst "<<yst.getstr()<<endl; 

cout<<"========================="<<endl; 

mst + yst; // to canconat the content of the array of yst to the contents of mst 

cout<< "mst "<<mst.getstr()<<endl; 

cout<<"yst "<<yst.getstr()<<endl; 

cout<<"========================="<<endl; 

mst - 3; // cute the last three characters of mst by placing '\0' char 

cout<<"========================="<<endl; 

cout<< "mst "<<mst.getstr()<<endl; 

cout<<"yst "<<yst.getstr()<<endl; 

cout<<"========================="<<endl; 

2 + mst; // replace the first two char of mst by 'A' char 

cout<<"========================="<<endl; 

cout<< "mst "<<mst.getstr()<<endl; 

cout<<"yst "<<yst.getstr()<<endl; 

cout<<"========================="<<endl; 

}

The output:

=========================

mst this is a test

yst message

=========================

mst this is a test message

yst message

=========================

mst this is a test mess

yst message

=========================

mst this is a test mess

yst message

=========================

Recommended Answers

All 6 Replies

>I've tried so hard to do it but I couldn't
I don't believe you. Post your attempts and describe how they didn't work to prove that you've tried "so hard".

>I have to send it to my professor tonight so please HELP
We generally don't care how much of a hurry you're in, so don't appeal to our sense of urgency.

I hope you don't mind I'll send you parts of my attempts
and tell you my problem and I hope you will help then I'll
move to the next step

constructor & destructor implementation
first class
string::string(const char *str,int alng)
{
   const *str=*const cp;
   alng=Alng;
}
string::~string()
{
}

second class
string::string(const char *str,int alng)
{
   const *str=const *cp;
   alng=Alng;
}
string::~string()
{
}

now my problem is that i can't know the difference between

char * const cp;

and

const char *cp;


by the way I'm an arabian girl so I need time to understand your english
so I hope you don't mind

Member Avatar for iamthwee

unless I am otherwise mistaken you have to put stuff in between the {} string::~string()
{
//stuff goes here
}


And the difference between :
char * const cp;

and

const char *cp;

clearly the two sentences have the same letters however, their positions relative to each other are different.

I hope that helps.

By the way I am not Arabian nor a girl.

thank you iamthwee
but as we took in universiry the destructor doesn't
have any thing in {}

and about your help in the difference between
char * const cp;

and

const char *cp;

i already know what you said what i need to know if there is any difference
in writing the implementation


also it's fine with me that your not arabian nor a girl

Could you not post everything in bold text, please?

>i already know what you said what i need to know if
>there is any difference in writing the implementation
There isn't. In fact, you don't even need two classes for this as the declaration of an object of String will handle the constness for you. Of course, you can't define two classes of the same name anyway, so the point is moot.

Example:

class MyString{
  char *cp; // pointer to the first element
  int Alng; // the N# of the elements in the array [max N# of char is Alng -1 ]
public:
  MyString(const char *str, int alng) ;
  ~MyString();
  const char* getstr()const{ return cp;}
};

MyString::MyString(const char *str,int alng)
{
  cp = new char[alng + 1];
  for ( char *p = cp; *str != '\0'; ++p, ++str )
    *p = *str;
  Alng = alng;
}
MyString::~MyString()
{
  delete[] cp;
}

>also it's fine with me that your not arabian nor a girl
He was implying that we don't care what your nationality or gender are.

thanks a lot for everyone for your help
my teacher gave us the answer so i 'm
posting it for anyone that might need it

#include <iostream.h>
#include <string.h>
#include <conio.h>
class  String{
	 char  * const cp; // pointer to the first element    - constant pointer 
	 int     Clng;        // the length of the contents of the array  
	 int      Alng;   // the N# of the elements in the array [max N# of char is Alng -1 ]
	 public:
	     String(const char  *str, int alng) : cp(new char [alng]){
			  Alng = alng;
			 strcpy(cp, str);
                         Clng = strlen(str);
		}
	     ~String(){
		     delete [] cp;
		}
	     const char*   getstr()const{ return cp;}

// mst + yst;
	     void operator+(String &p){
		      strcat(cp,p.cp);
	      }

//mst  - 3;   
	     void  operator-(int n){
		  int  i = strlen(cp) - n;
		   cp[i] = '\0';
		}

// 2 + mst;
friend void  operator+(int n, String &s);

};

void  operator+(int n, String &s){
      for(int i=0; i<n; i++)
	   s.cp[i] = 'A';
}

void main(){
String    mst("this is a test",40);
String    yst(" message", 15);
cout<<"========================="<<endl;
cout<< "mst  "<<mst.getstr()<<endl;
cout<<"yst   "<<yst.getstr()<<endl;
cout<<"========================="<<endl;
mst + yst;  // to canconat the content of the array of yst to the contents of  mst
cout<< "mst  "<<mst.getstr()<<endl;
cout<<"yst   "<<yst.getstr()<<endl;
cout<<"========================="<<endl;
mst  - 3;   // cute the last three characters of mst by placing '\0' char
cout<<"========================="<<endl;
cout<< "mst  "<<mst.getstr()<<endl;
cout<<"yst   "<<yst.getstr()<<endl;
cout<<"========================="<<endl;
2 + mst; // replace the first two char of mst by  'A' char  
cout<<"========================="<<endl;
cout<< "mst  "<<mst.getstr()<<endl;
cout<<"yst   "<<yst.getstr()<<endl;
cout<<"========================="<<endl;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.