Thread: { What's wrong with my code ? }

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    { What's wrong with my code ? }

    As we can see, I set default assignment. But the output is "Empty!"
    You can try to run this code snippet to see the output.

    Code:
    class Shape{
    public:
         virtual void setType( const std::string& str = "abc") = 0;
         virtual void getType() const;
    private:
          std::string str_;
    };
    
    void Shape::getType() const { 
         if( str_.empty() ){
              std::cout<<"Empty!"<<std::endl;
         }else{
              std::cout << str_ << std::endl; 
         }
    }
    
    void Shape::setType( const std::string& str) {
         str_ = str;
    }
    
    class Square:public Shape{
    public:
         void setType( const std::string& str = "abc"){
              str_=str;
         }
    private:
         std::string str_;
    };
    
    int main(){
         Shape * sp = new Square;
         sp->setType();
         sp->getType();
         return 0;     
    }
    Last edited by meili100; 12-15-2007 at 11:24 AM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Works for me - VS2005.
    What compiler?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Showed "Empty" on VS2003. I noticed that you have str_ twice though. Both in your base and derived classes. I removed str_ in the derived class and changed private to protected in the base class and it worked fine.

    Code:
    class Shape{
    public:
    	virtual void setType( const std::string& str = "abc") = 0;
    	virtual void getType() const;
    protected:
    	 std::string str_;
    };
    
    class Square:public Shape{
    public:
    	void setType( const std::string& str = "abc"){
    		str_=str;
    	}
    };

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Don't give virtual functions default arguments. There was a thread recently about this. Just don't do it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thanks, but would you explain why 2 "str_" code doesn't work?
    As we can see in my code the 2 "str_" are private to each class...

    Quote Originally Posted by Desolation View Post
    Showed "Empty" on VS2003. I noticed that you have str_ twice though. Both in your base and derived classes. I removed str_ in the derived class and changed private to protected in the base class and it worked fine.

    Code:
    class Shape{
    public:
    	virtual void setType( const std::string& str = "abc") = 0;
    	virtual void getType() const;
    protected:
    	 std::string str_;
    };
    
    class Square:public Shape{
    public:
    	void setType( const std::string& str = "abc"){
    		str_=str;
    	}
    };
    Last edited by meili100; 12-15-2007 at 11:17 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What did you do to the code? It was indented before! It's a mess now!
    The thing is, that str_ is private in each class and getType only exists in the base class, so it calls the base class's function, which prints out the base class's str_.
    But since you overloaded setType for both classes and made them virtual, the compiler will call the derived setType and set the derived's str_ to the value you specify.
    Thus:

    Base: Empty.
    Derived: Non-empty.

    There's your problem.
    Last edited by Elysia; 12-15-2007 at 11:21 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Quote Originally Posted by Elysia View Post
    What did you do to the code? It was indented before! It's a mess now!
    The thing is, that str_ is private in each class and getType only exists in the base class, so it calls the base class's function, which prints out the base class's str_.
    But since you overloaded setType for both classes and made them virtual, the compiler will call the derived setType and set the derived's str_ to the value you specify.
    Thus:

    Base: Empty.
    Derived: Non-empty.

    There's your problem, I believe.
    Thank you. Sorry for the mess.

    But getType() is a virtual function, the sp->getType(); should follow virtual table to the Square::getType(), shouldn't it?
    Last edited by meili100; 12-15-2007 at 11:20 AM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There is no Square::getType().
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Which is precisely the problem. The compiler can't call a getType in the derived class because there isn't one, so it calls the base class's instead.
    Though I still recommend you make str_ protected and getType and setType should be non-virtual sicne there's no point in doing it this way. Of course, since this probably is an exercise, carry on.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM