Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

overloaded functions & inheritance

Status
Not open for further replies.

atara

Technical User
May 5, 2003
2
GB
I have a base class with 2 public functions :
CBase & Func (const char *);
CBase & Func (char);
I have derived class with a public inharitance from the base.
In the derived class I have a public function
CBase & Func (int);

when I call in main function
derived.Func("aaa"); I got a compilation error.
when derived.Func('a'); works!
WHY?
 
The functions are not the same. The base class recieves a char or char* and the derived class recieves an int. Also they are not virtual so you will not be able to use pollymorphism. Example of inheritance

Code:
class base
{
public:
   base(){}
   virtual bool IsThisTheBaseClass(){return true;}
};

class derived:public base
{
   derived(){}
   virtual bool IsThisTheBaseClass(){return false;}
}

implementation

base* pBase = new Base;

if(pBase->IsTHisTHeBaseClass())
   cout<<&quot;YES\n&quot;;
else
   cout<<&quot;NO\n&quot;;

delete pBase;
pBase = new derived;

if(pBase->IsTHisTHeBaseClass())
   cout<<&quot;YES\n&quot;;
else
   cout<<&quot;NO\n&quot;;



// OUTPUT

YES
NO

Also there is pure virtual funcitons which can be in the base class but MUST be defined int the derived class.

Example

Code:
class base
{
public:
   base(){}
   virtual bool IsThisTheBaseClass(){return true;}

   virtual void pureVirtualFunc()=0;
};

class derived:public base
{
   derived(){}
   virtual bool IsThisTheBaseClass(){return false;}

   virtual void purVirtualFunc(){cout<<&quot;HI I AM PURE VIRTUAL\n&quot;;
}


Hope that helped

Matt

 
If the functions are not the same I should have BOTH functions in the derived class: one that takes int and second which takes char*.
I want to imply the function that takes the char* in the BASE class and in the derived class i want to ADD another function - which is DIFFERENT cause it takes an int.
SO - I should have 2 different function in the derived class. That what overloading function is for.
thanks

 
yep... both Base and Derived will have a function that can take a char*

I misunderstood the original question. Because the base class does NOT have a function that takes a single char 'a', the ascii equivalent is passed to the Func(int) function.

Why you are seeing a problem with &quot;aaaa&quot; is weird, it should know how to iplicitly convert that. Try

char* test = &quot;aaaa&quot;;
Derived d;
d.Func(test)

and see what happens.

Matt
 

You are getting an error because &quot;aaaa&quot; is not a const char*. What Matt shows is not a const char* either. What you need to pass in is a const char*. Your function is defined as follows: CBase & Func(const char *);

Either take out the const or pass in a const char*.

In C, constant values default to external linkage, so they can appear only in source files. In C++, constant values default to internal linkage, which allows them to appear in header files.

The const keyword can also be used in pointer declarations.

char *const aptr = mybuf; // Constant pointer

*aptr = 'a'; // Legal
aptr = yourbuf; // Error

A pointer to a variable declared as const can be assigned only to a pointer that is also declared as const.

const char *bptr = mybuf; // Pointer to constant data

*bptr = 'a'; // Error
bptr = yourbuf; // Legal


Brother C
 
#include <stdio.h>


class Interface_class
{
public:
virtual void Func(const char *str)=NULL;
virtual void Func(int ii)=NULL;
};


class Base_class: public Interface_class
{
public:
virtual void Func(const char *str){ printf(&quot;%s\n&quot;, str); }
virtual void Func(int ii){ printf(&quot;%d\n&quot;, ii); }
};


class Derived_class: public Base_class
{
public:
virtual void Func(int ii){ printf(&quot;%d\n&quot;, ii+1); }
};


int main(void)
{
Derived_class derived_obj;
Interface_class *interface_ptr;

interface_ptr=static_cast<Interface_class*>(&derived_obj);

interface_ptr->Func(&quot;aaaa&quot;);
interface_ptr->Func(5);

return(0);
}
 
I dont think so... &quot;aaaa&quot; should be treated as a const char*, however, if you were trying to pass a const char* to a function that recieved a char* you would see problems because it cant convert from const to non-const.

Matt
 
You must use a pointer to the base class. Not the class you call Base_class (very bad name by the way) for virtual functions to work correctly. Also, only the base class needs to define the func() as virtual.

Run the code below:

#include <stdio.h>


class Interface_class
{
public:
virtual void Func(const char *str){ printf(&quot;Interface Class char\n&quot;); }
virtual void Func(int ii){ printf(&quot;Interface Class int\n&quot;); }
};


class Base_class: public Interface_class
{
public:
void Func(const char *str){ printf(&quot;Base Class %s\n&quot;, str); }
void Func(int ii){ printf(&quot;Base Class %d\n&quot;, ii); }
};


class Derived_class: public Base_class
{
public:
void Func(int ii){ printf(&quot;Derived Class %d\n&quot;, ii+1); }
};


int main(void)
{
Interface_class* pInterfaceClass;

Interface_class interface_ptr;
Base_class base_class;
Derived_class derived_obj;

// use interface class func()
pInterfaceClass = &interface_ptr;

pInterfaceClass->Func(&quot;aaaa&quot;);
pInterfaceClass->Func(4);

// use derived class func()
pInterfaceClass = &base_class;

pInterfaceClass->Func(&quot;bbbb&quot;);
pInterfaceClass->Func(5);

// use derived class func()
pInterfaceClass = &derived_obj;

pInterfaceClass->Func(&quot;aaaa&quot;);
pInterfaceClass->Func(4);

return(0);
}


Brother C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top