1 / 17

Dynamic Memory for Class

Dynamic Memory for Class. #include<iostream> #ifndef STRCLASS_H_ #define STRCLASS_H_ class strclass { private: char *str; int len; static int num_strings; public: strclass(const char *s); strclass(); ~strclass();

annick
Download Presentation

Dynamic Memory for Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dynamic Memory for Class #include<iostream> #ifndef STRCLASS_H_ #define STRCLASS_H_ class strclass { private: char *str; int len; static int num_strings; public: strclass(const char *s); strclass(); ~strclass(); friend ostream & operator << (ostream &os, const strclass &st); } ; #endif;

  2. A static class member is created for all objects ( it is share); #include<cstring> #include “strclass.h”; int strclass :: num_strings=0; strclass::strclass(const char *s) { len=strlen(s); str=new char[len+1]; strcpy(str,s); num_strings++; cout<<num_strings<<“: “<<str<<endl; }

  3. strclass::strclass() { len=4; str=new char[4]; strcpy(str,”C++”); num_strings++; cout<<num_strings<<“: “<<str<<endl; } strclass::~strclass() { cout<<str<< “ object deleted ”; --num_strings; cout<<num_strings<< “ left \n”; delete [ ]str; }

  4. ostream & operator<<(ostream &os ,const strclass & st) { os<<st.str; return os; } We can’t initialize static member inside class declaration.

  5. #include<iostream> #include “strclass.h”; void call1( strclass & rsb) { cout<<“pass by reference \n”; cout<<rsb<<“\n”; } void call2( strclass sb) { cout<<“pass by value \n”; cout<<sb<<“\n”; }

  6. int main() { strclass headline1(“this is test1”); strclass headline2(“this is test2); strclass sport(“A won B”); cout<<headline1<<endl; cout<<headline2<<endl; cout<<sport<<endl;

  7. call1(headline1); cout<<headline1<<endl; call2(headline2); cout<<headline2<<endl; strclass sailor=sport; cout<<sailor<<endl; strclass headline3= headline1; cout<<headline3<<endl; return 0; }

  8. after calling call2(headline2) we see some junk character; At the end of main we see : This is test1 object deleted, 2 left; This is test2 object deleted, 1 left; This is testBV object deleted, 0 left; @ object deleted, -1 left; -| object deleted, -2 left;

  9. It gets messy! C++ automatically provides member functions : default constructor if you don’t define one copy constructor if you don’t define one assignment operator if you don’t define one default destructor if you don’t define one Address operator if you don’t define one

  10. copy constructors is used when we explicitly initialize an object to another one. It copies all non-static member one by one (deep copying) In the previous example when we call call2(); the default copy constructors called so it doesn’t increase the num_strings. When we assign sport to sailor then we have sailor.str=sport.str; But actually the pointers point to the same string By deleting one the other one is deleted.

  11. Solution: Fixing the problem withexplicit copy constructor (deep copy) strclass::strclass(const strclass & st) { num_strings++; len=st.len; //same length; str=new char[len+1]; strcpy(str,st.str); cout<<num_strings<<“: “<<str<<endl; cout<<“ object created \n”; }

  12. Assignment operators The syntax is : class_name & class_name::operator=(const class_name &) { }

  13. When we use Assignment operators assigning one object to existing object. strclass headline3; headline3=headline1; // = operator invoked. It copies member-to-member. But actually the pointers point to the same string hedline3.str=headline1.str;

  14. Solution strclass & strclass:: operator=(const strclass & st) { if(this== & st) return *this; delete [ ] str; len=st.len; str=new char[len+1]; strcpy(str,st.str); return *this; }

  15. Things to remember when using new in Constructors • After using new to initialize a pointer, we should use delete in destructor. • new and delete should be compatible. new with delete and new [ ] with delete [ ]. • If there are multiple constructors, all should use new the same way, either all with [ ] or without [ ].

  16. We should use a copy constructor that initialize one object to another by doing deep copying. • We should define an assignment operator that copies to another by doing deep copying.

  17. Static Class Member Function We can declare a member function as being static. A static member function doesn’t have to be invoked by an object. A static member function can use only static data member.

More Related