Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Initializing base class data

Initializing base class data

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformancetutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SunilKrSingh
    wrote on last edited by
    #1

    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.

    P E 2 Replies Last reply
    0
    • S SunilKrSingh

      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.

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      What's wrong in having a copy c'tor in base class, too. That would take care of this.

      1 Reply Last reply
      0
      • S SunilKrSingh

        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.

        E Offline
        E Offline
        enhzflep
        wrote on last edited by
        #3

        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.

        S 1 Reply Last reply
        0
        • E enhzflep

          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.

          S Offline
          S Offline
          SunilKrSingh
          wrote on last edited by
          #4

          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

          E 1 Reply Last reply
          0
          • S SunilKrSingh

            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

            E Offline
            E Offline
            enhzflep
            wrote on last edited by
            #5

            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;
            

            }

            S 1 Reply Last reply
            0
            • E enhzflep

              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;
              

              }

              S Offline
              S Offline
              SunilKrSingh
              wrote on last edited by
              #6

              Thanks It cleared my confusion.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups