Initializing base class data
-
class Base { char * ptr; public: Base(){} Base(char * str) { ptr = new char[strlen(str)]; strcpy(ptr,str); } }; class Derived : public Base { char * ptr_s; public: Derived(char * str1,char * str2):Base(str2) { ptr_s = new char[strlen(str1)]; strcpy(ptr_s,str1); } Derived(const Derived & sec)//:Base(sec.ptr) { this->ptr_s = new char[strlen(sec.ptr_s)]; strcpy(this->ptr_s,sec.ptr_s); } }; int _tmain(int argc, _TCHAR* argv[]) { Derived Obj1("sunil","singh"); Derived Obj2 = Obj1; return 0; } Obj1 is a derived class object where base class char pointer is initialized with "singh" and derived class char pointer is initilized with "sunil". I want to create Obj2 out of Obj1. Separate memory should be created for Obj2 char pointer (base part and derived part as well) and that should be initialized with the strings contained in Obj1. Here the problem is: Derived class part can be initialized with copy constructor. How to initialize the base class char poniter of Obj2 with the base class part of Obj1. char pointers in both the classes are private. I tried using initializer list but could not succeed. Is there some proper way to do this? Thanks for any help or suggestion in advance.
-
class Base { char * ptr; public: Base(){} Base(char * str) { ptr = new char[strlen(str)]; strcpy(ptr,str); } }; class Derived : public Base { char * ptr_s; public: Derived(char * str1,char * str2):Base(str2) { ptr_s = new char[strlen(str1)]; strcpy(ptr_s,str1); } Derived(const Derived & sec)//:Base(sec.ptr) { this->ptr_s = new char[strlen(sec.ptr_s)]; strcpy(this->ptr_s,sec.ptr_s); } }; int _tmain(int argc, _TCHAR* argv[]) { Derived Obj1("sunil","singh"); Derived Obj2 = Obj1; return 0; } Obj1 is a derived class object where base class char pointer is initialized with "singh" and derived class char pointer is initilized with "sunil". I want to create Obj2 out of Obj1. Separate memory should be created for Obj2 char pointer (base part and derived part as well) and that should be initialized with the strings contained in Obj1. Here the problem is: Derived class part can be initialized with copy constructor. How to initialize the base class char poniter of Obj2 with the base class part of Obj1. char pointers in both the classes are private. I tried using initializer list but could not succeed. Is there some proper way to do this? Thanks for any help or suggestion in advance.
What's wrong in having a copy c'tor in base class, too. That would take care of this.
-
class Base { char * ptr; public: Base(){} Base(char * str) { ptr = new char[strlen(str)]; strcpy(ptr,str); } }; class Derived : public Base { char * ptr_s; public: Derived(char * str1,char * str2):Base(str2) { ptr_s = new char[strlen(str1)]; strcpy(ptr_s,str1); } Derived(const Derived & sec)//:Base(sec.ptr) { this->ptr_s = new char[strlen(sec.ptr_s)]; strcpy(this->ptr_s,sec.ptr_s); } }; int _tmain(int argc, _TCHAR* argv[]) { Derived Obj1("sunil","singh"); Derived Obj2 = Obj1; return 0; } Obj1 is a derived class object where base class char pointer is initialized with "singh" and derived class char pointer is initilized with "sunil". I want to create Obj2 out of Obj1. Separate memory should be created for Obj2 char pointer (base part and derived part as well) and that should be initialized with the strings contained in Obj1. Here the problem is: Derived class part can be initialized with copy constructor. How to initialize the base class char poniter of Obj2 with the base class part of Obj1. char pointers in both the classes are private. I tried using initializer list but could not succeed. Is there some proper way to do this? Thanks for any help or suggestion in advance.
Does this do what you want? I: 1) Added +1 to all of the strlen statements used for memory allocation (strlen doesn't account for the NULL terminator - which you need to allow for when storing the string, as opposed to simply displaying it) 2) Made the data members
protected
3) Uncommented the call to the Base constructor in the 2nd constructor for Derived Code:#include <string.h>
#include <stdio.h>class Base
{
public:
Base(){}
Base(char * str)
{
ptr = new char[strlen(str)+1];
strcpy(ptr,str);
}
protected:
char * ptr;
};class Derived : public Base
{
public:
Derived(char * str1,char * str2):Base(str2)
{
ptr_s = new char[strlen(str1)+1];
strcpy(ptr_s,str1);
}
Derived(const Derived & sec):Base(sec.ptr)
{
printf("Derived(const Derived &sec)\n");
this->ptr_s = new char[strlen(sec.ptr_s)+1];
strcpy(this->ptr_s,sec.ptr_s);
}
void showName()
{
printf("%s %s\n", ptr_s, ptr);
}
protected:
char * ptr_s;
};int main(int argc, char* argv[])
{
Derived Obj1("sunil","singh");Derived Obj2 = Obj1; Obj1.showName(); Obj2.showName(); return 0;
}
Result: Derived(const Derived &sec) sunil singh sunil singh Process returned 0 (0x0) execution time : 0.055 s Press any key to continue.
-
Does this do what you want? I: 1) Added +1 to all of the strlen statements used for memory allocation (strlen doesn't account for the NULL terminator - which you need to allow for when storing the string, as opposed to simply displaying it) 2) Made the data members
protected
3) Uncommented the call to the Base constructor in the 2nd constructor for Derived Code:#include <string.h>
#include <stdio.h>class Base
{
public:
Base(){}
Base(char * str)
{
ptr = new char[strlen(str)+1];
strcpy(ptr,str);
}
protected:
char * ptr;
};class Derived : public Base
{
public:
Derived(char * str1,char * str2):Base(str2)
{
ptr_s = new char[strlen(str1)+1];
strcpy(ptr_s,str1);
}
Derived(const Derived & sec):Base(sec.ptr)
{
printf("Derived(const Derived &sec)\n");
this->ptr_s = new char[strlen(sec.ptr_s)+1];
strcpy(this->ptr_s,sec.ptr_s);
}
void showName()
{
printf("%s %s\n", ptr_s, ptr);
}
protected:
char * ptr_s;
};int main(int argc, char* argv[])
{
Derived Obj1("sunil","singh");Derived Obj2 = Obj1; Obj1.showName(); Obj2.showName(); return 0;
}
Result: Derived(const Derived &sec) sunil singh sunil singh Process returned 0 (0x0) execution time : 0.055 s Press any key to continue.
Thanks for your help. Here in my scenario char * ptr; is private in base class. So the Derived copy constructor statement will fail Derived(const Derived & sec):Base(sec.ptr) We will not be allowed to access the base class data using "sec.ptr". So if my base class data is private, what should I do to initialize the base class data. Thanks, Sunil
-
Thanks for your help. Here in my scenario char * ptr; is private in base class. So the Derived copy constructor statement will fail Derived(const Derived & sec):Base(sec.ptr) We will not be allowed to access the base class data using "sec.ptr". So if my base class data is private, what should I do to initialize the base class data. Thanks, Sunil
How about providing a copy constructor in the base class as others have suggested? Something like so (the displayed output is identical to my previous post):
#include <string.h>
#include <stdio.h>class Base
{
public:
Base(){}
Base(char * str)
{
ptr = new char[strlen(str)+1];
strcpy(ptr,str);
}
Base(const Base &src)
{
this->ptr = new char[strlen(src.ptr)+1];
strcpy(this->ptr,src.ptr);
}
virtual void showName()
{
printf("%s", ptr);
}
private:
char * ptr;
};class Derived : public Base
{
public:
Derived(char * str1,char * str2):Base(str2)
{
ptr_s = new char[strlen(str1)+1];
strcpy(ptr_s,str1);
}
Derived(const Derived & sec):Base(sec)
{
printf("Derived(const Derived &sec)\n");
this->ptr_s = new char[strlen(sec.ptr_s)+1];
strcpy(this->ptr_s,sec.ptr_s);
}
void showName()
{
printf("%s", ptr_s);
}
void showFullName()
{
showName();
printf(" ");
Base::showName();
printf("\n");
}
private:
char * ptr_s;
};int main(int argc, char* argv[])
{
Derived Obj1("sunil","singh");Derived Obj2 = Obj1; Obj1.showFullName(); Obj2.showFullName(); return 0;
}
-
How about providing a copy constructor in the base class as others have suggested? Something like so (the displayed output is identical to my previous post):
#include <string.h>
#include <stdio.h>class Base
{
public:
Base(){}
Base(char * str)
{
ptr = new char[strlen(str)+1];
strcpy(ptr,str);
}
Base(const Base &src)
{
this->ptr = new char[strlen(src.ptr)+1];
strcpy(this->ptr,src.ptr);
}
virtual void showName()
{
printf("%s", ptr);
}
private:
char * ptr;
};class Derived : public Base
{
public:
Derived(char * str1,char * str2):Base(str2)
{
ptr_s = new char[strlen(str1)+1];
strcpy(ptr_s,str1);
}
Derived(const Derived & sec):Base(sec)
{
printf("Derived(const Derived &sec)\n");
this->ptr_s = new char[strlen(sec.ptr_s)+1];
strcpy(this->ptr_s,sec.ptr_s);
}
void showName()
{
printf("%s", ptr_s);
}
void showFullName()
{
showName();
printf(" ");
Base::showName();
printf("\n");
}
private:
char * ptr_s;
};int main(int argc, char* argv[])
{
Derived Obj1("sunil","singh");Derived Obj2 = Obj1; Obj1.showFullName(); Obj2.showFullName(); return 0;
}
Thanks It cleared my confusion.