CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2012
    Posts
    11

    Object method does not respond

    I am studying the creation of classes in C + +. When executing the code below the method LEN class String2 not work. Someone can tell me why?

    code ----------------------------------------------------------------------------------------------
    #include <iostream>
    #include <string.h>
    using namespace std;

    const int MAX = 80;

    class string2
    {
    private:
    char str[MAX];
    public:
    string2() {str[0]='\0';}
    string2(char s[]) {strcpy_s(str,s);}
    string2(char ch, int n);
    int len() const {return strlen(str);}
    void print() const {cout << str;}
    };

    string2::string2(char ch, int n)
    {
    int ii;
    for(int i=0;i<n;i++)
    {
    str[i] = ch;
    ii = i;
    }
    str[ii] = '\0';
    }
    void main()
    {
    string2 s1("Feliz Aniversario! "),
    s2('=',18),
    s3 = "Estamos na Primavera. ",
    s4,s5;
    cout << "\nS1 = "; s1.print();
    cout << "\nS2 = "; s2.print();
    cout << "\nS3 = "; s3.print();

    s4 = "O Mundo gira!";

    cout << "\nS4 = "; s4.print();

    s5 = s4;

    cout << "\nS5 = "; s5.print();

    cout << "\nTamanho de S1: "; s1.len();
    cout << "\nTamanho de S2: "; s2.len();
    cout << "\nTamanho de S3: "; s3.len();
    cout << "\nTamanho de S4: "; s4.len();
    cout << "\nTamanho de S5: "; s5.len();
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Object method does not respond

    For starters, you need to indent your code properly, e.g.,
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    const int MAX = 80;
    
    class string2
    {
    private:
        char str[MAX];
    public:
        string2() {str[0]='\0';}
        string2(char s[]) {strcpy_s(str,s);}
        string2(char ch, int n);
        int len() const {return strlen(str);}
        void print() const {cout << str;}
    };
    
    string2::string2(char ch, int n)
    {
        int ii;
        for(int i=0;i<n;i++)
        {
            str[i] = ch;
            ii = i;
        }
        str[ii] = '\0';
    }
    
    int main()
    {
        string2 s1("Feliz Aniversario! "),
            s2('=', 18),
            s3 = "Estamos na Primavera. ",
            s4, s5;
        cout << "\nS1 = "; s1.print();
        cout << "\nS2 = "; s2.print();
        cout << "\nS3 = "; s3.print();
    
        s4 = "O Mundo gira!";
    
        cout << "\nS4 = "; s4.print();
    
        s5 = s4;
    
        cout << "\nS5 = "; s5.print();
    
        cout << "\nTamanho de S1: "; s1.len();
        cout << "\nTamanho de S2: "; s2.len();
        cout << "\nTamanho de S3: "; s3.len();
        cout << "\nTamanho de S4: "; s4.len();
        cout << "\nTamanho de S5: "; s5.len();
    }
    I took the liberty of changing <string.h> to the C++ version: <cstring>, and void main to int main.

    Next, how does it not work? Have you checked to be sure that the member array of char really is a null terminated string?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jan 2009
    Posts
    596

    Re: Object method does not respond

    This code is a bit odd:
    Code:
    string2::string2(char ch, int n)
    {
        int ii;
        for(int i=0;i<n;i++)
        {
            str[i] = ch;
            ii = i;
        }
        str[ii] = '\0';
    }
    Why do you need the ii variable? That code is equivalent to:
    Code:
    string2::string2(char ch, int n)
    {
        for(int i=0;i<n;i++)
        {
            str[i] = ch;
        }
        str[n-1] = '\0';
    }
    Edit: Strictly speaking, the second code sample is only equivalent to the first when the loop executes at least once, i.e. when n>0. Otherwise, if the loop does not execute at all, ii is uninitialized so the first code sample would use an uninitialized variable.
    Last edited by Peter_B; October 18th, 2012 at 07:30 AM.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Object method does not respond

    Regardless of whether string2::len() does what it's supposed to, which I didn't check, this would never output the string length:

    Code:
        cout << "\nTamanho de S1: "; s1.len();
    what you actually want probably rather is this:

    Code:
        cout << "\nTamanho de S1: " << s1.len();
    In contrast, this does what you expect it to do:

    Code:
        cout << "\nS1 = "; s1.print();
    This may seem paradox compared to the above, but it only "works" because string2::print() does the printing entirely on its own inside its implementation; the cout at the beginning of this line has nothing to do with that.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Oct 2012
    Posts
    11

    Re: Object method does not respond

    You're right, missed the << operator. Thank you for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured