RE: [Dev-C++] newbie questions without answer
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ho...> - 2002-08-11 19:26:53
|
<http://run.to/noicys>=20 -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Carlos Garc=EDa del Monte Sent: Sunday, August 11, 2002 9:53 PM To: dev-cpp-users-list Subject: Re: [Dev-C++] newbie questions without answer =20 The code post is only a part, talking about the files he gave (perhaps you didn't receive the files?). =20 =20 Nope, i did not receive any attachment. Thanks for the info. Now lets answer to the original poster: =20 =20 I wrote principally for him, but as you want see all, here is the code:=20 The code for the struct thing (newbiequestion.cpp) is:=20 #include <iostream>=20 #include <stdlib.h>=20 #include <string.h>=20 struct date{=20 int month;=20 int day;=20 int year;=20 };=20 struct employ{=20 char *first;=20 char *second;=20 date eyear; /*i want to use a reference to the year in the first struct=20 but then i get an error message from the compiler since i didn't=20 =20 You can use a reference provided that you have a date object and a constructor for struct employ.=20 =20 E.g.: =20 struct date{=20 int month;=20 int day;=20 int year;=20 };=20 =20 struct employ{=20 char *first;=20 char *second;=20 int &eyear; =20 employ(char *f, char *s, int &y):first(f), second(s), eyear(y) {} }; =20 int main() { date d; =20 employ e(new char, new char, d.year); } =20 =20 Just for the record, i do not like the approach you are using, it is too C-style. =20 =20 =20 };=20 int main()=20 {=20 date d;=20 employ e;=20 d.day=3D30;=20 d.month=3D8;=20 d.year=3D1982;=20 e.eyear =3D d;=20 e.first=3Dnew char [strlen("senem")]; // a way of doing this shortly?? =20 =20 Using a constructor and standard library facilities like std::string. If you do not use a reference mentioned previously, you could use an initializer list but it is too naked. =20 =20 e.second=3Dnew char [strlen("beken")];=20 e.first=3D"senem";=20 e.second=3D"beken";=20 cout<<e.first<<" "<<e.second<<endl;=20 cout<<"birthday:\n"=20 <<e.eyear.day<<"/"<<e.eyear.month<<"/"<<e.eyear.year<<endl;=20 system("PAUSE");=20 return 0;=20 }=20 Another is the stringclas.cpp file which source is:=20 #include <iostream>=20 #include <stdlib.h>=20 #include <cassert>=20 #include <string.h>=20 #include <iomanip.h>=20 class string{=20 friend ostream &operator<<(ostream &,const string&);=20 friend istream &operator>>(istream &,string&);=20 public:=20 string(const char * =3D"");=20 string(const string &);=20 ~string();=20 const string &operator=3D(const string &);=20 const string &operator+=3D(const string &);=20 bool operator!()const;=20 bool operator=3D=3D(const string&)const;=20 bool operator<(const string&)const;=20 bool operator!=3D(const string& right)const=20 {return !(*this=3D=3Dright);}=20 bool operator>(const string& right)const=20 {return (right<*this);}=20 bool operator<=3D(const string& right)const=20 {return !(right<*this);}=20 bool operator>=3D(const string& right)const=20 {return !(*this<right);}=20 char &operator[](int);=20 const char &operator[](int)const;=20 string operator()(int,int);=20 int getlength()const;=20 private:=20 int length;=20 char * sptr;=20 void setstring(const char*);=20 };=20 string::string(const char *s):length(strlen(s)){=20 cout<<"conversion constructor :"<<s<<"\n";=20 setstring(s);=20 }=20 string::string(const string©):length(copy.length){=20 cout<<"copy constructor: "<<copy.sptr<<"\n";=20 setstring(copy.sptr);=20 }=20 string::~string(){=20 cout<<"destructor: "<<sptr<<"\n";=20 delete[]sptr;=20 }=20 const string &string::operator=3D(const string &right){ //burda nie ref var??=20 cout<<"operator=3D called\n";=20 if(&right!=3Dthis){=20 delete[]sptr;=20 length=3Dright.length;=20 setstring(right.sptr);=20 }=20 else=20 cout<<"attempted assignment of a string to itself\n";=20 return *this;=20 }=20 const string &string::operator+=3D(const string &right){=20 char *string1=3Dnew char[length+right.length+1]; ///DON'T MODIFY THE SIZE=20 strcpy(string1,sptr); /* i tried to change this part a little=20 so that the first string is not modified,but then=20 the program started to terminate abnormally=20 and i have absolute no idea what i did wrong!! here is the original: =20 =20 What kind of abnormal termination. assert()?=20 char *tempptr=3Dsptr;=20 lenght+=3Dright.length;=20 sptr=3Dnew char [length+1];=20 assert(sptr!=3D0);=20 strcpy(sptr,tempptr);=20 strcat(sptr,right.ptr);=20 delete[]tempptr;=20 return *this;*/ =20 =20 =20 I must mention that C++ provides already a string type defined in <string>. So why do you bother? :)=20 =20 =20 Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.cjb.net <http://www.noicys.cjb.net/>=20 * Alternative URL: http://run.to/noicys =20 |