show() method does not show...

This is a discussion on show() method does not show... within the C++ Programming forums, part of the General Programming Boards category; Hi, all. I was working on this and I thought I finally figured it out but when I ran this ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    Unhappy show() method does not show...

    Hi, all. I was working on this and I thought I finally figured it out but when I ran this program what it did is to output blank lines. This is confusing...
    OK, so I got a class called MyString which is as follows
    Code:
    class MyString {
    private:
        int           len;
        char *     str;
        static int counter;
    public:
    	MyString(); 
    	MyString(char* nts_string);
    	MyString(const char * string_n);
            MyString(const MyString& s); 
    	static int numStrings(void); 
    	~MyString(); 
    	friend ostream& operator<<(ostream& os, const MyString &s); 
            void showit();
    And, also I have this class:
    Code:
    class People {
    private:
    	int      age;
    	MyString name;
    	MyString address;
    public:
        People();              
        People(char* nts_string1, int n, char* nts_string2); 
        People(int n); 
        friend ostream& operator<<(ostream& os, const People &s); 
        MyString getName() const; 
        MyString getAddress() const; 
        void show(void); 
    };
    I did this in main, and it displayed correctly, so I assumed if I do the same thing in People, it should work.
    Code:
    MyString s2("Hello");
    s2.showit();
    Well, when I called this:
    Code:
    cout << "People::adam.show()" << endl;
    adam.show();
    there were blank lines only. Why is that?
    The inline implementation of the methods is as such:
    Code:
    MyString getName() const { return name; }
    MyString getAddress() const { return address; }     
    void show(void) { // print data including the cars owned
                 cout << age << "\n";
                 cout << getName() << "\n";
                 cout << getAddress() << endl;
    }
    The age gets displayed (of course), but the rest is just blank lines... Can anybody help me with this?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    MyString s2("Hello");
    cout << s2;
    Does that work? Also, I see you have these constructors:

    Code:
        People();              
        People(char* nts_string1, int n, char* nts_string2); 
        People(int n);
    If you do only the People(int n); constructor, for example, do you put in values for the name and address?

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Quote Originally Posted by Tonto
    Code:
    MyString s2("Hello");
    cout << s2;
    Does that work?
    Yes, displays fine....

    Quote Originally Posted by Tonto
    Also, I see you have these constructors:

    Code:
        People();              
        People(char* nts_string1, int n, char* nts_string2); 
        People(int n);
    If you do only the People(int n); constructor, for example, do you put in values for the name and address?
    All of the constructors ask for data from keyboard, either all of it, or some part is given, like age.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    OK, I made some changes...
    Code:
    class People {
    private:
    	int      age;
    	MyString name;
    	MyString address;
    public:
        People() {
                 cout << "Enter name (no blanks please): " << endl;
                 char nameKey[LENGTH];
                 cin >> nameKey;
                 MyString name(nameKey);
                 cout << "Enter age: " << endl;
                 cin >> age;
                 cout << "Enter address (no blanks please): " << endl;
                 char addrKey[LENGTH];
                 cin >> addrKey;
                 MyString address(addrKey);
                 }
                 
        People(char* nts_string1, int n, char* nts_string2) {
                     /* cout << "oops" << endl; */
                     MyString name(nts_string1);
                     age = n;
                     MyString address(nts_string2);
                     }
        People(int n) {
                 cout << "Enter name (no blanks please): " << endl;
                 char nameKey[LENGTH];
                 cin >> nameKey;
                 MyString name(nameKey);
                 cout << "Enter address (no blanks please): " << endl;
                 char addrKey[LENGTH];
                 cin >> addrKey;
                 MyString address(addrKey);
                 age = n;
                 }
        friend ostream& operator<<(ostream& os, const People &s) {
                 os << s.getName() << s.getAddress() << endl;
                 return os;
                 }                  
        int showAge() const { return age; }
        MyString getName() const { return name; }
        MyString getAddress() const { return address; }     
        void show(People & p) { // print data including the cars owned
                 cout << p.age << "\n";
                 cout << p << endl;
                /* cout << getAddress() << endl; */
                 }
    
    };
    I call these methods as such:
    Code:
    People adam, john("John", 20, "Main Street");
    People susan = People(10);
    adam.show(adam);
    What did I do wrong? I'm about to give up, I guess....

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
        People() {
                 cout << "Enter name (no blanks please): " << endl;
                 char nameKey[LENGTH];
                 cin >> nameKey;
                 MyString name(nameKey);
                 cout << "Enter age: " << endl;
                 cin >> age;
                 cout << "Enter address (no blanks please): " << endl;
                 char addrKey[LENGTH];
                 cin >> addrKey;
                 MyString address(addrKey);
                 }
    Yo ho. Those are local variables of the constructor, not your class's instance variables.

    Code:
        People() {
                 cout << "Enter name (no blanks please): " << endl;
                 char nameKey[LENGTH];
                 cin >> nameKey;
                 name(nameKey);
                 cout << "Enter age: " << endl;
                 cin >> age;
                 cout << "Enter address (no blanks please): " << endl;
                 char addrKey[LENGTH];
                 cin >> addrKey;
                 address(addrKey);
                 }
    Same with whatever else.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Thanks! Oh...... That's why it was showing ONLY when in MyString class.......
    But how do I indicate that I want "name" to act in People class as MyString constructor?

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    to avoid this kindof confusion it's a good idea to prefix members with m_ or add a trailing _ or something. This is not hungarian notation, as I'm indicating scope not type.

    as for your question, you could declare a People constructor that takes arguements
    Code:
    class People
    {
        int m_age;
        std::string m_name;
        std::string m_address;
    public:
        People(const std::string& name,
                const std::string& address,
                int age)
        : m_name(name) // this is called an initialiser list
        , m_address(address)
        , m_age(age) 
        {
        }
    };
    BTW it's generally considered bad design to have your user interface code inside a "model" class such as people.

    this design is better
    Code:
    int main()
    {
                std::string name;
                cout << "Enter name (no blanks please): " << endl;
                cin >> name;
    
                int age = 0;
                cout << "Enter age: " << endl;
                cin >> age;
    
                std::string address;
                cout << "Enter address (no blanks please): " << endl;
                cin >> address;
    
                People p(name, address, age);
    }
    that way, if you change from a command line to a gui you don't have to rewrite People.
    while we're at it People is the wrong name for that class. It represents one person so it should be Person. People is a collection of Persons
    e.g.
    Code:
    typedef std::vector<Person> People;
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Life would much easier if I could use string, but I have to use NTS instead... And I do not know anything about vectors yet... Thanks, but that will not solve my problem.......
    The whole trouble is that I have to use a class MyString (actually its constructor) and then use MyString type to create a member of People class... Does it sound like it is possible? Should be, but how?

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Can anybody explain to me what is going on below?
    Code:
    class A {
    public:
              A(int i) {}
    };
    
    class B {
              char* myString;
              int myFlag;
              A* myA;
    public:
              B(int j) : myString(""), myFlag(0), myA(&A(j)) {} 
    };
    No inheritance is involved here... I'm confused about the line in blue. What does it exactly do?

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    B(int j) : myString(""), myFlag(0), myA(&A(j)) {}
    That is an 'initializer list'. It makes the myFlag variable 0, myString "", and then constructs an 'A' object and makes myA point to it.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Thanks Tonto, I'll think about it!

  12. #12
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by kocika73
    Life would much easier if I could use string, but I have to use NTS instead... And I do not know anything about vectors yet... Thanks, but that will not solve my problem.......
    The whole trouble is that I have to use a class MyString (actually its constructor) and then use MyString type to create a member of People class... Does it sound like it is possible? Should be, but how?
    yep, it's actually pretty easy.
    Code:
    class People {
    private:
    // as I said use something to indicate the member vars
    	int      m_age;
    	MyString m_name;
    	MyString m_address;
    public:
                 
         People(const MyString& name, int age, const MyString& address)
        : m_name(name)
        , m_age(age)
        , m_address(address)
       {
        }
    };
    
    // usage
    People p(MyString("kocika73"), 78, MyString("antartica"));
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Thanks, ChaosEngine! I'll try and see what I can do with it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 08:51 AM
  2. Using my own method in a constructor...
    By AngryStyro in forum C++ Programming
    Replies: 13
    Last Post: 06-04-2008, 02:02 PM
  3. Virtual Method question...
    By keira in forum C++ Programming
    Replies: 14
    Last Post: 05-23-2008, 08:56 PM
  4. Delegate Calling a method that Calls a delegate.
    By xddxogm3 in forum C# Programming
    Replies: 2
    Last Post: 05-05-2008, 01:59 AM
  5. NEED MAJOR HELP WITH HW....Newton-Raphson method
    By clezzie in forum C Programming
    Replies: 8
    Last Post: 04-14-2008, 08:52 PM

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