10 Replies - 1594 Views - Last Post: 18 April 2011 - 02:12 AM Rate Topic: -----

#1 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

classes , pointers , polymorphism

Posted 16 April 2011 - 05:43 AM

Hi guys,
I have a question regarding Polymorphism , Inheritance , pointers and References .
If I have the following code :
Class A {.}
Class B : public A {.}
Class C: public B {..}

In addition I have a class called Menu , that manages all the connections between those three classes above , and more (no need to say what is it , just another branch of my task) .
1 .Can you please explain the difference between :
Menu *mainMenu  = new Menu () ;
to
Menu mainMenu; 


2 . If I need to use polymorphism , what shall I use , pointers or something else ?
Meaning is ,if I want to use abstract methods or classes , how can I decide if I need to use pointers or something else (maybe ,no pointers at all ? )
I can do the following :
A *a1 = new A();
A *a2 = new B();
A *a3 = new C();

Yeah ,of course this is the use of pointers , however , in order to use Polymorphism , must I use pointers ?
How can I do it with references ?

Thanks in advance , Ron

Is This A Good Question/Topic? 0
  • +

Replies To: classes , pointers , polymorphism

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: classes , pointers , polymorphism

Posted 16 April 2011 - 06:39 AM

You could do that, and it seems that most people do. Many of those people run into memory issues though.

I'd use vectors. Why? It's impossible to have a memory leak using them and I don't like pointers.
vector<A> a1;
vector<B> a2;
vector<C>a3;
A initiala1;
B initiala2;
C initiala3;
a1.push_back(initiala1);
a2.push_back(initiala2);
a3.push_back(initiala3);

Now if I want to get rid of an object I simply tell the vector to erase it.
Was This Post Helpful? 0
  • +
  • -

#3 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: classes , pointers , polymorphism

Posted 16 April 2011 - 06:39 AM

Menu mainMenu;


This is allocating memory on the stack. Therefore it will be destroyed when its scope ends. E.G

{
    Menu mainMenu;
}
mainMenu.show();


This is illegal because mainMenu no longer exists.


However, the other example allocated memory on the heap, and it is not freed until you say delete. E.G.

Menu * mainMenu = 0;
{
    mainMenu = new Menu();
}
mainMenu->show();
delete mainMenu;



So that is legal (but you must remember to delete it once you are done with it)
Was This Post Helpful? 0
  • +
  • -

#4 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: classes , pointers , polymorphism

Posted 16 April 2011 - 06:51 AM

Quote

however , in order to use Polymorphism , must I use pointers ?
How can I do it with references ?


exactly the same way :)
void show(A & obj)
{
    obj.show();
}

int main()
{
    A a;
    B b;
    C c;
    show(a);
    show(B)/>;
    show(c);
    return 0;
}


You will get the same results as you would with a pointer :)

But remember that you cannot pass by value, otherwise you will only get the base class.
Was This Post Helpful? 0
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: classes , pointers , polymorphism

Posted 16 April 2011 - 06:57 AM

You can't perform polymorphism without pointers.
BaseClass * b = new DerivedClass(); //correct
BY reference
DerivedClass d;
BaseClass * b = &d; //note we still are using the pointers but passing the address of d to it

This post has been edited by codeprada: 16 April 2011 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

#6 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: classes , pointers , polymorphism

Posted 16 April 2011 - 08:06 AM

Hi guys, first:thank you all !
Second , I'd be frank you : my professor gave me a big assignment on Inheritance&Polymorphism , and wrote that :
[b]USE CONST and REFERENCE any place you can !!![/b]
.

I'm okay with pointers ,and I prefer to implement the entire methods using pointers , but I'm sure he won't like it would hurt my grade .

I need to do something like Amazon , a virtual shop , that consists on : Movies/Music/Books .

Movie has 2 sons : (action movies / thriller movies)
Book has 2 sons : children books / study books
Music has no sons , but one biggy linked list :)) attached to it .

So, pointers or references ?
10x,Ron
Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: classes , pointers , polymorphism

Posted 16 April 2011 - 02:43 PM

View Postcodeprada, on 16 April 2011 - 02:57 PM, said:

You can't perform polymorphism without pointers.
Actually polymorphism works just fine using references.

struct base
{
    virtual void foo()
    {
        std::cout << "base::foo()" << std::endl;
    }
};

struct derived : public base
{
    void foo()
    {
        std::cout << "derived::foo()" << std::endl;
    }
};

void call_foo(base& obj)
{
    obj.foo();
}

int main()
{
    derived obj;
    call_foo(obj);
} 
The code above produces derived::foo() as its output, even though call_foo() accepts a 'base' by-reference.

View Postpasser_by, on 16 April 2011 - 04:06 PM, said:

Second , I'd be frank you : my professor gave me a big assignment on Inheritance&Polymorphism , and wrote that :
[b]USE CONST and REFERENCE any place you can !!![/b]
.
Good advice there. You should prefer to use references instead if pointers where possible. Using const is a good habit to learn as well.

View Postpasser_by, on 16 April 2011 - 04:06 PM, said:

So, pointers or references ?
In general, use references where you can, and use pointers where you have to. You 'may' be stuck with pointers for some things if you're compelled to allocate memory with new, but otherwise you can avoid them almost entirely with a bit of careful thought.

This post has been edited by Bench: 16 April 2011 - 02:44 PM

Was This Post Helpful? 1
  • +
  • -

#8 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: classes , pointers , polymorphism

Posted 17 April 2011 - 10:03 PM

View PostBench, on 16 April 2011 - 02:43 PM, said:

View Postcodeprada, on 16 April 2011 - 02:57 PM, said:

You can't perform polymorphism without pointers.
Actually polymorphism works just fine using references.

View Postpasser_by, on 16 April 2011 - 04:06 PM, said:

So, pointers or references ?
In general, use references where you can, and use pointers where you have to. You 'may' be stuck with pointers for some things if you're compelled to allocate memory with new, but otherwise you can avoid them almost entirely with a bit of careful thought.

But how can I use references ,if I need to use abstract classes ?
for example , assuming the Base class has a "print()" method , and I don't want to make a use
of that method at all , like :

Class Base 
{... 
 virtual void print()=0   //  I won't implement this method , only my sons would !!!
}

Class Derived : public Base // derived class 
{
 void print () {..... implementation  ....} 
}


so if :
Base *ptr = new Base();
ptr->print();

how do I do this with reference ?
thanks ,Ron
Was This Post Helpful? 0
  • +
  • -

#9 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: classes , pointers , polymorphism

Posted 18 April 2011 - 01:10 AM

you could do something along the lines of
struct Base 
{
	virtual void print()  {printf("BASE");}   
};

struct Derived : public Base // derived class 
{
 void print () {printf("derived");}
};

 int main()
 {
   Derived d;
	
   Base &ptr = d;
   ptr.print();

	
   return -1;
 }


Was This Post Helpful? 0
  • +
  • -

#10 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: classes , pointers , polymorphism

Posted 18 April 2011 - 01:42 AM

View Postmuballitmitte, on 18 April 2011 - 01:10 AM, said:

you could do something along the lines of
struct Base 
{
	virtual void print()  {printf("BASE");}   
};

struct Derived : public Base // derived class 
{
 void print () {printf("derived");}
};

 int main()
 {
   Derived d;
	
   Base &ptr = d;
   ptr.print();

	
   return -1;
 }



1.Just let me understand something , when I do Derived d; , basically , I create an object however
I don't allocate memory to that object ?

2.Taking it one step forward ,when working with references ,we don't use the "new" command ?
3.so how exactly the Polymorphism concept is being used with the help of References ? because I cannot do for instance , this :
A a = new B(); 

if A is the father and B is the son .


thanks !

This post has been edited by passer_by: 18 April 2011 - 01:53 AM

Was This Post Helpful? 0
  • +
  • -

#11 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: classes , pointers , polymorphism

Posted 18 April 2011 - 02:12 AM

When you declare a variable like this

Derived d;



the memory needed is automatically allocated on the stack for you. Similar to a pointer a reference holds the address of an object with the difference that it cannot be changed and syntactically you would use it as you would an object. You can declare d as a pointer too nobody`s stopping you, but it that case you would need to use the dereference operator to get the object

Derived* d = new Derived();
	
Base &ptr = *d;
ptr.print();



edit: in response to (3)
Polymorphism will work in the same way with vtables. Read more on references here http://www.cprogramm...references.html and polymorphism http://www.dreaminco...phism-tutorial/ here

This post has been edited by muballitmitte: 18 April 2011 - 02:34 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1