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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

abstract classes 1

Status
Not open for further replies.

kenguru

Programmer
May 14, 2004
173
RO
Hello,

Maybe this question isn't proper here, but i hope that someone will answer.
The example is:
Class A { //abstract class
}
class B:public A{
}

B line;
What is the difference between the following ones, and what they mean, with "english word":
A* pp=&line;
A& rr=line;

Please write,
Thank You,
Kenguru

 
Since class B derives from class A, any instance of B is an instance of A. This is the basis of polymorphism. So you can take a pointer or reference for an A type and point it at a B instance, and then you can call the public methods of A. If any of the methods of A are virtual and overridden in B, then the instance will use the B version because the object being pointed to is still a B object even if the pointer or reference thinks its an A.

The difference between the two lines is just the difference between a pointer and a reference. They will work the same when it comes to polymorphism. They are generally useful when you have a function or collection that takes an A reference or pointer. The code in that function should always work no matter how many classes you derive from A and no matter which type of object you actually send to the function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top