problem encountered while doing mathematical operations on strings by overloaded '+'.
-
#include
#include
using namespace std;class st{
char \*p; int len; public: st(){len=0; p=0;} //creates null string st(const char \*s); //creates string from arrays st(const st &s); //copy constructor ~st(){delete p;} //destructor //+ operator friend st operator + (const st &, const st &); //<= operator friend int operator <= (const st &, const st &); friend void show(const st);
};
st :: st(const char *s){
len = strlen(s);
p = new char[len+1];
strcpy(p, s);
//cout<This program works fine when i replace
Quote:
st string1, string2, string3;
with
Quote:
st string1=s1, string2=s2, string3=s1+s3;
so what's the problem, i learned in class that we can pass the values in the constructor by calling it explicitly, and (string1, string2, string3) & (s1, s2, s3) have same return type i.e.class st.
So why isn't it working ?thank you
-
#include
#include
using namespace std;class st{
char \*p; int len; public: st(){len=0; p=0;} //creates null string st(const char \*s); //creates string from arrays st(const st &s); //copy constructor ~st(){delete p;} //destructor //+ operator friend st operator + (const st &, const st &); //<= operator friend int operator <= (const st &, const st &); friend void show(const st);
};
st :: st(const char *s){
len = strlen(s);
p = new char[len+1];
strcpy(p, s);
//cout<This program works fine when i replace
Quote:
st string1, string2, string3;
with
Quote:
st string1=s1, string2=s2, string3=s1+s3;
so what's the problem, i learned in class that we can pass the values in the constructor by calling it explicitly, and (string1, string2, string3) & (s1, s2, s3) have same return type i.e.class st.
So why isn't it working ?thank you
To make this part of code working:
st string1, string2, string3; string1 = s1; string2 = s2;
you must implement the assignment operator for your class st.
-
#include
#include
using namespace std;class st{
char \*p; int len; public: st(){len=0; p=0;} //creates null string st(const char \*s); //creates string from arrays st(const st &s); //copy constructor ~st(){delete p;} //destructor //+ operator friend st operator + (const st &, const st &); //<= operator friend int operator <= (const st &, const st &); friend void show(const st);
};
st :: st(const char *s){
len = strlen(s);
p = new char[len+1];
strcpy(p, s);
//cout<This program works fine when i replace
Quote:
st string1, string2, string3;
with
Quote:
st string1=s1, string2=s2, string3=s1+s3;
so what's the problem, i learned in class that we can pass the values in the constructor by calling it explicitly, and (string1, string2, string3) & (s1, s2, s3) have same return type i.e.class st.
So why isn't it working ?thank you