Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,357 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,155 people online right now. Registration is fast and FREE... Join Now!




my Inheritance errors or not?

 
Reply to this topicStart new topic

my Inheritance errors or not?, my Inheritance errors or not?

khunmk
post 30 Aug, 2008 - 12:13 PM
Post #1


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 2

Hi all, I'm a beginner in C++. I got a problem with some of my codes. Like this
CODE

#include<iostream.h>

class Base
{
     public:
         void show() {  //that will do my some works }
};

class Inher1
{
     public:
         void show() {  //this one also will do my some works }
};

int main()
{
    Inher1 hr1;         //object of my Inher1
    Base *ptr;

    ptr = &hr1;
    ptr->show();      //my problem is here...
    return 0;
}

My problem is on ptr->show().
why it always display the answer from the base class. cause of my function error?
I'm beginner. Pls teach to me. what i'm wrong.
User is offlineProfile CardPM

Go to the top of the page

KYA
post 30 Aug, 2008 - 01:20 PM
Post #2


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,205



Thanked 50 times

Dream Kudos: 1150
My Contributions


You have not set up inheritance and you created a pointer to the base class. Here is an example of what you may be trying to do:

cpp

#include<iostream.h>

class Base
{
public:
void show() { //that will do my some works }
};

class Inher1 : public Base // inher derives from base class
{
public:
void show() { //this one also will do my some works }
};

int main()
{
Base* ptr = new Inher1(); //declare a new object, why not just make it an inher1 pointer
//I would need to know your intentions here, for above

ptr->show(); //my problem is here...
delete ptr;
return 0;
}


That's the general idea. Was this post helpful? wink2.gif
User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 30 Aug, 2008 - 05:21 PM
Post #3


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 502



Thanked 44 times

Dream Kudos: 25
My Contributions


Should make the show method virtual.
cpp
#include<iostream.h> 

class Base
{
public:
virtual void show() { std::cout << "Show in Base" << std::endl; }
};

class Inher1 : public Base // inher derives from base class
{
public:
virtual void show() { std::cout << "Show in Inher1" << std::endl; }
};

int main()
{
Base* ptr = new Inher1(); //declare a new object, why not just make it an inher1 pointer
//I would need to know your intentions here, for above

ptr->show(); //my problem is here...
delete ptr;
return 0;
}
User is offlineProfile CardPM

Go to the top of the page

KYA
post 30 Aug, 2008 - 05:46 PM
Post #4


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,205



Thanked 50 times

Dream Kudos: 1150
My Contributions


Only the base class method needs to be virtual so that it knows to override with the inherited class method when called.
User is offlineProfile CardPM

Go to the top of the page

khunmk
post 30 Aug, 2008 - 07:12 PM
Post #5


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 2

Wooo ! Great.. all !
I can solve my problem with all your method. Than a million to you all.

Sorry for I forgot this class Inher1 : public Base
I want to ask again.
I create a pointer to the Base. But I insert the address Hr1 to the pointer. Why it show the error.
User is offlineProfile CardPM

Go to the top of the page

KYA
post 30 Aug, 2008 - 07:22 PM
Post #6


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,205



Thanked 50 times

Dream Kudos: 1150
My Contributions


You need to create a 'new' object, not just assign memory address

This post has been edited by KYA: 30 Aug, 2008 - 07:23 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 04:12AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month