codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  C++ Inheritance Polymorphism  ahjo_nora at 20:43 on Tuesday, April 21, 2009
 

The following code gives error. Is there any explanation?
The issue is Child class is not able to use Parent class functions.

Using g++ (GCC) 3.4.6 20060404 (Red Hat 3.4.6-10)

--------------------------------

#include<iostream>
#include<string>
using namespace std;

class Parent {
public:
Parent(){}
virtual ~Parent(){}
virtual void Run()=0;
protected:
virtual void Form(string abc)=0;
protected:
virtual void Form(string abc, int i)
{
cout<<"ParentForm1 abc:"<<i<<endl;
}
};

class Child:public Parent{
public:
Child():Parent(){}
virtual ~Child(){}
virtual void Run()
{
cout<<"Child run "<<endl;
Form("ABC",1,4);
Form("DEF");
}
protected:
virtual void Form(string abc, string def)
{
cout<<"ChildForm 1 abc:"<<abc<<" Def:"<<def<<endl;
Form(def);
}
virtual void Form(string abc,int i , int b)
{
cout<<"ChildForm2 abc:"<<i<<b<<endl;
Form(abc,i);
}
virtual void Form(string abc)
{
cout<<"ChildForm3 Overridden abc:"<<endl;
}
virtual void TestForm(){
string abc("abababa");
int i=5;
//Parent::Form(abc,i);// This works
Form(abc,i);
}
};
main()
{
Child child;
child.Run();
}

---------------------------------------

$ g++ test_inherit.cpp
test_inherit.cpp: In member function `virtual void Child::Form(std::string, int, int)':
test_inherit.cpp:38: error: invalid conversion from `int' to `const char*'
test_inherit.cpp:38: error: initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
test_inherit.cpp: In member function `virtual void Child::TestForm()':
test_inherit.cpp:48: error: invalid conversion from `int' to `const char*'
test_inherit.cpp:48: error: initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'



  Re: C++ Inheritance Polymorphism  jinhao at 04:27 on Wednesday, April 22, 2009
 

There is a
virtual void Form(string abc, int i);
in class Parent.

and you want to invoke the method in
virtual void Form(string abc,int i , int b);
virtual void TestForm();
in class Child.

it is a mistake.

In class Child, you give two new methods
virtual void Form(string abc, string def);
virtual void Form(string abc,int i , int b);

and override two methods of Parent
virtual void Form(string abc,int i , int b);
virtual void Form(string abc);

They hides the name Form of Parent in class Child, if you want to invoke the method in class Child, you must specify the class name. like this.
Parent::Form(abc,i);





  Re: C++ Inheritance Polymorphism  ariseadi at 11:48 on Friday, April 24, 2009
 

nice one

  Re: C++ Inheritance Polymorphism  ahjo_nora at 13:35 on Friday, April 24, 2009
 

Thanks for the reply. But as the arguments are different here, should not the C++ compiler resolved it by using name mangling ?
ahjo_nora

  Re: C++ Inheritance Polymorphism  jinhao at 05:02 on Monday, April 27, 2009
 

>>should not the C++ compiler resolved it by using name mangling ?
You mean that overloading?
First, you overrided the method -Form in class Child, it hides the Form in class Parent. So, there are just two method -
virtual void Form(string abc, string def);
virtual void Form(string abc,int i , int b);
to be candidated if you invoke the method Form in class Child.
No virtual void Form(string abc) to be candidated, because you have already hidden it.

  Re: C++ Inheritance Polymorphism  ahjo_nora at 19:11 on Monday, April 27, 2009
 

Thanks jinhao for your input. So what I understand from the discussion is that:
if I override any of the function from the Parent in child, none of the other functions will be available in Child. Is it C++ standard rule?

Here Parent functions:
Form(string)=0;
Form(string, int);

Child Functions:
Form(string) // This has to be defined for parent pure virtual function
Form(string, string)
Form(string,int,int)

//Form(string,int) is not overloaded and I assumed that this is available
// in Child.

So the only way to invoke Form(string,int) in Child is to be specific, like Parent::Form(string, int).

Thanks for your thoughtful discussion.
ahjo

  Re: C++ Inheritance Polymorphism  jinhao at 03:52 on Tuesday, April 28, 2009
 

Yes, it is a standard C++ rule.

Giving a same name with a virtual function of the base class in a child class, this is called overriding. Overriding will hide the name of the base class.

Giving a same name with an existing function and different signature in a same scope, this is called overloading.

E.g.

class B
{
public:
void foo(){}
};

class A: public B
{
public:
void foo(int) //Hides the B::foo
{
foo(); //What is foo?
}
};

foo in class A is not a overloading function, because the 2 functions is not defined in a same scope. So, A::foo hides the B::foo.

Continue.

class B
{
public:
void foo(){}
};

class A: public B
{
public:
using B::foo; //using-declaration

void foo(int) //Hides the B::foo
{
foo(); //What is foo?
}
};

using-declaration can introduce a name into current scope, if current scope has not a such name, name mangling will use the name which is declared by using-declaration.

Now, class A has not defined a void foo(), and foo(int) can invoke the foo() successfully, and it refers to B::foo;

Name hidding is so normal, like this.

int i;
int main()
{
int i;
i = 5; //Which i?
}

Which i? Obviously, the i defined in main. It hides the i in global scope.





  Re: C++ Inheritance Polymorphism  ahjo_nora at 16:55 on Tuesday, April 28, 2009
 


Thanks a lot Jinhao for the answer.
ahjo_nora









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  online dating
•  Recommended Free Spyware Removal Tools
•  Re: File Splitter code in perl
•  Vanishing Excel tab issue (ASP.net)
•  File Splitter code in perl
•  File Splitter code in perl
•  ASP.NET /Excel opening in frame Issue
•  huffman encoding and decoding in C++...
•  huffman encoding and decoding in C++...


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2009