170 likes | 250 Views
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();
E N D
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;
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; }
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; }
ostream & operator<<(ostream &os ,const strclass & st) { os<<st.str; return os; } We can’t initialize static member inside class declaration.
#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”; }
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;
call1(headline1); cout<<headline1<<endl; call2(headline2); cout<<headline2<<endl; strclass sailor=sport; cout<<sailor<<endl; strclass headline3= headline1; cout<<headline3<<endl; return 0; }
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;
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
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.
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”; }
Assignment operators The syntax is : class_name & class_name::operator=(const class_name &) { }
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;
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; }
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 [ ].
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.
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.