program with constructor

This is a discussion on program with constructor within the C++ Programming forums, part of the General Programming Boards category; Hey guys I am making a program with the use of a constructor for the first time. I have everything ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    60

    program with constructor

    Hey guys I am making a program with the use of a constructor for the first time. I have everything working except I am not sure I can add hello on to my pre existing string world when I pass it to the alter function.


    Code:
    class Prep{
    char string[40];
    
    
    public:
     void print()
     {
     printf("%s");
     }
     int alter(char s[], int sz);
    
     Prep(char str[]);
    
    };
    
    
    Prep::Prep(char str[])
    {
    strcpy(str,string);
    }
    
    
    
    int Prepender::alter(char s[], int sz)
    {
    
    
    
    }
    
    
    
    int main(void){
      char s[40];
      char s2[10];
      int rc;
    
      strcpy(s,"world");
      strcpy(s2,"there");
      Pre("hello");  //calls constructor, hello is
                             //stored inside object
      h.print();             //prints hello to the screen
    
      //this next printf will print:
    //  alter returned: 1.  String is: hello world
      rc=h.alter(s, 40);  //s1 is big enough to add hello
      printf("alter returned: %d.  String is: %s\n",rc,s1);
    
    
      //this next printf will print:
    //  alter returned: 0.  String is:  there
      rc=h.alter(s2, 10);  //s2 is not big enough to add hello
      printf("alter returned: %d.  String is: %s\n",rc,s2);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    For starters, you should indent your code properly, e.g.,
    Code:
    class Prep {
    public:
        void print()
        {
            printf("%s");
        }
    
        int alter(char s[], int sz);
    
        Prep(char str[]);
    private:
        char string[40];
    };
    
    Prep::Prep(char str[])
    {
        strcpy(str, string);
    }
    
    int Prepender::alter(char s[], int sz)
    {
    }
    
    int main(void) {
        char s[40];
        char s2[10];
        int rc;
    
        strcpy(s, "world");
        strcpy(s2, "there");
        Pre("hello"); //calls constructor, hello is
        //stored inside object
        h.print(); //prints hello to the screen
    
        // this next printf will print:
        // alter returned: 1.  String is: hello world
        rc = h.alter(s, 40);  //s1 is big enough to add hello
        printf("alter returned: %d.  String is: %s\n", rc, s1);
    
        // this next printf will print:
        // alter returned: 0.  String is:  there
        rc = h.alter(s2, 10); //s2 is not big enough to add hello
        printf("alter returned: %d.  String is: %s\n", rc, s2);
    }
    Now, the problem that you have now is that you are indeed invoking the constructor of Prep to create an object (once you fix the typo), but it is just a temporary object. You should be creating an object named h, e.g.,
    Code:
    Prep h("hello");
    Oh, and you should be including the correct headers. Actually, you should be using std::string, available by #include <string> instead of using C-style null terminated strings, and then you should be using C++-style I/O, available by #include <iostream>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Code:
    <include <cstring>
    <include <cstdio>
    
    class Prep {
    public:
        void print()
        {
            printf("%s");
        }
    
        int alter(char s[], int sz);
    
        Prep(char str[]);
    private:
        char string[40];
    };
    
    Prep::Prep(char str[])
    {
        strcpy(str, string);
    }
    
    int Prep::alter(char s[], int sz)
    {
    }
    
    int main(void) {
        char s[40];
        char s2[10];
        int rc;
    
        strcpy(s, "world");
        strcpy(s2, "there");
        Prep h("hello"); //calls constructor, hello is
        //stored inside object
        h.print(); //prints hello to the screen
    
        // this next printf will print:
        // alter returned: 1.  String is: hello world
        rc = h.alter(s, 40);  //s1 is big enough to add hello
        printf("alter returned: %d.  String is: %s\n", rc, s1);
    
        // this next printf will print:
        // alter returned: 0.  String is:  there
        rc = h.alter(s2, 10); //s2 is not big enough to add hello
        printf("alter returned: %d.  String is: %s\n", rc, s2);
    }
    ok thanks for your advice i am just beginning in c++ from c so I haven't been using iostream just cstring and cstdio. But for my main problem is there anyway to put the hello in front of world? Would I just use my string member to do it?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Quote Originally Posted by Thegame16
    But for my main problem is there anyway to put the hello in front of world? Would I just use my string member to do it?
    What is the purpose of the Prep class?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Quote Originally Posted by laserlight View Post
    What is the purpose of the Prep class?
    just to set my data member using a constructor and then alter it.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Quote Originally Posted by Thegame16
    just to set my data member using a constructor and then alter it.
    What does the class model? What does "Prep" mean? What does it mean to "alter it"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Quote Originally Posted by laserlight View Post
    What does the class model? What does "Prep" mean? What does it mean to "alter it"?
    i want to insert the object in front of s. If it does this and doesn't
    exceed the sz I will return 1, if it does i will return 0.

    For example i pass world with a size of 40.
    the count on that would be less than 40 so I print hello world.

    then i pass there with a sz of ten. so it would return 0 and
    there because sz wasn't big enough.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    What I am getting at is that if you want to write a class, then you should think in terms of objects. For example, I might write it in this way:
    Code:
    #include <cstring>
    #include <iostream>
    
    class Prepender {
    public:
        explicit Prepender(const char str[])
        {
            strcpy(data, str); // assume no overflow.
        }
    
        const char* get() const
        {
            return data;
        }
    
        void prepend(const char str[])
        {
            using namespace std;
    
            size_t len = strlen(str);
            memmove(data + len, data, strlen(data) + 1);
            strncpy(data, str, len);
        }
    private:
        char data[40];
    };
    
    int main()
    {
        using namespace std;
    
        Prepender prepender("world");
        prepender.prepend("hello ");
        cout << prepender.get() << endl;
    }
    Notice that instead of printing a string that is local to the main function, I get the string from prepender. (I have left out any error checking in my example: you would need to modify it to include them. Incidentally, instead of returning an int, you might want to return a bool since prepend can either succeed or fail.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. calling constructor crashes program?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 07-04-2003, 11:17 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21