polymorphism prblm

This is a discussion on polymorphism prblm within the C++ Programming forums, part of the General Programming Boards category; Her. is an example of a progpam that is supposed to use function overloading. Why doesn't it work. Code: // ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    polymorphism prblm

    Her. is an example of a progpam that is supposed to use function overloading. Why doesn't it work.

    Code:
    // display string prblm #1 pg368 c++ primer plus
    #include <iostream>
    using namespace std;
    
    //prototype (overloading, polymorphism)
    void sho_str(const char * str);
    void sho_str(const char * str, int n=0);
    
    int main()
    {
    	char dog[10]="Dakota";
    
    	sho_str(dog);
    	sho_str(dog,10);
    	sho_str(dog,10);
    	sho_str(dog,10);
    
    	return 0;
    }
    
    void sho_str(const char * str)
    {
    	cout << str;
    }
    
    void sho_str(const char * str, int n)
    {
    	n++;
    
    	// number of times function has been executed
    	static int times=0;
    	times++;
    	for (int i=0;i<times;i++)
    		{
    			cout << str;
    		}
    }
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    poly.cpp:
    Error E2015 poly.cpp 13: Ambiguity between 'sho_str(const char *)' and 'sho_str(
    const char *,int)' in function main()
    *** 1 errors in Compile ***

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    don't you need the .h in iostream.h?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void sho_str(const char * str, int n=0);
    The overloaded function has a default value for the second parameter. If you don't include a value for that parameter then the compiler will use the default value, thus the ambiguity. When you call sho_str(dog), the compiler uses the default value and ends up with sho_str(dog, 0).

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    52
    thanks, thats a little info the book doesn't mention
    c++ primer plus is an excellent book though
    next chptr oop and classes i can hardly wait

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >thats a little info the book doesn't mention
    Because it's a book on basic syntax, those authors have to assume that you know nothing about programming. How are they going to explain the rules of function overloading without causing some people to have a brain hemorrhage?

    >c++ primer plus is an excellent book though
    Yes it is

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 06:06 PM
  2. A C++ program examples showing Polymorphism, please help.
    By MarkSquall in forum C++ Programming
    Replies: 19
    Last Post: 06-06-2008, 05:41 AM
  3. Question on polymorphism
    By 6tr6tr in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2008, 10:05 AM
  4. change sorting method using polymorphism
    By Forever82 in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2003, 02:21 PM
  5. Polymorphism & Overloaded Operators :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 09:40 PM

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