Thread: program with constructor

  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
    28,413
    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>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 11:17 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07: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