Click here to Skip to main content
15,894,825 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Text Length Pin
Chris Losinger4-Jul-06 15:34
professionalChris Losinger4-Jul-06 15:34 
QuestionSyntax related question [modified] Pin
Jay034-Jul-06 11:37
Jay034-Jul-06 11:37 
AnswerRe: Syntax related question Pin
Jörgen Sigvardsson4-Jul-06 11:47
Jörgen Sigvardsson4-Jul-06 11:47 
Questionvirtual destructors [modified] Pin
Jay034-Jul-06 10:59
Jay034-Jul-06 10:59 
AnswerRe: virtual destructors Pin
Joe Woodbury4-Jul-06 11:13
professionalJoe Woodbury4-Jul-06 11:13 
AnswerRe: virtual destructors Pin
Jörgen Sigvardsson4-Jul-06 12:02
Jörgen Sigvardsson4-Jul-06 12:02 
GeneralRe: virtual destructors Pin
Jay034-Jul-06 12:39
Jay034-Jul-06 12:39 
GeneralRe: virtual destructors Pin
Jörgen Sigvardsson4-Jul-06 13:39
Jörgen Sigvardsson4-Jul-06 13:39 
Jay03 wrote:
A* a = new B();

how can a Pointer to an A object create a new B object?

its like saying

int *a = new double;


No, not really. A double doesn't inherit from int. With your A and B, the story is different.

When you declareclass B : public A you state a relationship between class A and B. That relationship is "B is an A". This relationship means that wherever you can put an A*, you can put a B*. For instance, suppose you have the function
void function(A* a) { ... } 
it means that you can do this:
B* b = ...;
function(b);
. Why? Because we've stated that B is an A. This is the essence of inheritance. You use these relationships to make your programs more modular and interchangeable.

For example: Suppose you have the class Instrument. It has the virtual method PlayNote(char* note). Suppose note is a string with a "C", "A", "B#", or any other playable note. The class Guitarr, Flute and Organ all derive from Instrument. Their versions of PlayNote produces the corresponding sound. With this inheritance setup, you can do things like this:
Instrument* instrument = new Guitarr();
instrument->PlayNote("C");
instrument->PlayNote("F");
instrument->PlayNote("G");
This will make each call to PlayNote produce guitarr sounds. Should you ever want to switch to an organ, just do this instead:
Instrument* instrument = new Organ();
instrument->PlayNote("C");
instrument->PlayNote("F");
instrument->PlayNote("G");
One word switched, and the program now behaves very differently (and hopefully sounds a lot more different too!)

Now, using inheritance, you can make your own orchestra. How? Well, check this out:
Instrument* orchestra[3];
orchestra[0] = new Guitarr();
orchestra[1] = new Flute();
orchestra[2] = new Organ();
 
for(int i = 0; i < 3; ++i) {
   instrument->PlayNote("C");
   instrument->PlayNote("F");
   instrument->PlayNote("G");
}
See how easy that was? All we had to do was to declare an array to hold 3 pointers to Instrument. Then we stuffed each entry with an various instruments. The reason we can do this, is that because of the relationship "Guitarr is an Instrument", "Flute is an Instrument", and "Organ is an Instrument".

This is just one tiny portion of what you can do with inheritance. The tricky part is how you express it in the C++ language. You have already faced one learning obstacle in the language: virtual and non-virtual methods and destructors. For this example to work, PlayNote must be a virtual method. Why? Well, had the method not been virtual, and you have calls like this:
Instrument* instrument = new Flute();
instrument->PlayNote("C");
the compiler would have generated a call to the version of PlayNote that is declared in Instrument. For all non-virtual methods (and destructors), the compiler looks at the type of the variable to determine which method to call. In this case, the type of the variable is Instrument*. From that the compiler deduces that the method to be called is Instrument::PlayNote. If you make the method virtual, the compiler won't be so "dumb" about it, and call the correct method (in this case Flute::PlayNote). Why on earth is the compiler behaving this dumb by default you might ask? The reason is purely technical, although very valid in some scenarios, and certainly was valid back in the days when C++ was designed. When you declare a virtual method, the compiler adds extra data to the object (typically 4 bytes per object in a 32 bit environment), which it uses to determine which method to call. This overhead is unbearable in some scenarios, as internal memory may be a scarce resource.

Note that this "is a"-relationship only holds in one direction. Suppose we have class B : A again. Given this, we cannot write code like
B* b = new A();
. Why? Because the relationship does not say "A is a B". To elaborate on why this is correct, see here:
class A { 
public:
   virtual void Method() { cout << "A::Method called"; }
};
 
class B : public A {
public:
   virtual void Method2() { cout << "B::Method2 called"; }
};
 
B* b = new A();
b->Method();  // Calls A::Method
b->Method2(); // Calls B::Method2...?
Generally speaking, whatever you declare in A is also declared in B automatically. That is to say that not only can you call Method2 through B, you can also call Method. The same does not hold for A - whatever's declared in B is not declared in A. With this in mind, what method will be called here: b->Method2()? The variable b is of type B*. From this, the compiler deduces that there is indeed a method named Method2() available. But.. keep in mind that we didn't create an object of class B, we created one of class A. A doesn't have the method Method2. Should you try, the C++ compiler catch the error, emit an error message and stop the compilation. (So there's no need to worry about it! Smile | :) )

Without a whiteboard and a lab, my pedagogical skills stretches only this far (although, I hope I made some sense). I advise you to pick up the book C++ Primer (4th Edition)[^] at the bookstore or your local library (any edition will do, as the basics of C++ hasn't changed much over the years). It'll give you a far better foundation that any of us could give you on a web forum. Also, make sure you experiment a lot. If in doubt; write it and test it - that methodology and armed with a good book is the best way to learn in my opinion.

By the way, in C++, it's good manner to put the asterisk adjacent to the type, rather than the variable. Like this: Type* pointer. I could go on as to why that's the norm, but I feel I would be giving you information that you don't need right now. Only old timers (former C programmers) and stubborn people put the asterisk adjacent to the variable name. Wink | ;)


--
[LIVE] From Omicron Persei 8
GeneralRe: virtual destructors [modified] Pin
Jay034-Jul-06 14:25
Jay034-Jul-06 14:25 
GeneralRe: virtual destructors [modified*2] Pin
Jörgen Sigvardsson4-Jul-06 14:27
Jörgen Sigvardsson4-Jul-06 14:27 
GeneralRe: virtual destructors [modified*2] Pin
Jay034-Jul-06 14:50
Jay034-Jul-06 14:50 
GeneralRe: virtual destructors Pin
Jörgen Sigvardsson4-Jul-06 14:53
Jörgen Sigvardsson4-Jul-06 14:53 
QuestionAdding scripting language to existing application ? Pin
Maximilien4-Jul-06 7:57
Maximilien4-Jul-06 7:57 
AnswerRe: Adding scripting language to existing application ? Pin
Chris Losinger4-Jul-06 8:09
professionalChris Losinger4-Jul-06 8:09 
Questionnamespace across multiple include files in library Pin
Joel Becker4-Jul-06 7:53
Joel Becker4-Jul-06 7:53 
AnswerRe: namespace across multiple include files in library Pin
Jun Du4-Jul-06 9:44
Jun Du4-Jul-06 9:44 
GeneralRe: namespace across multiple include files in library Pin
Joel Becker4-Jul-06 10:16
Joel Becker4-Jul-06 10:16 
GeneralRe: namespace across multiple include files in library Pin
Jun Du4-Jul-06 14:48
Jun Du4-Jul-06 14:48 
AnswerRe: namespace across multiple include files in library Pin
Joel Becker4-Jul-06 10:13
Joel Becker4-Jul-06 10:13 
Question.hh extension header files [modified] Pin
Jay034-Jul-06 7:09
Jay034-Jul-06 7:09 
AnswerRe: .hh extension header files Pin
toxcct4-Jul-06 7:31
toxcct4-Jul-06 7:31 
GeneralRe: .hh extension header files [modified] Pin
Jay034-Jul-06 7:42
Jay034-Jul-06 7:42 
GeneralRe: .hh extension header files Pin
toxcct4-Jul-06 7:43
toxcct4-Jul-06 7:43 
GeneralRe: .hh extension header files Pin
Jay034-Jul-06 8:04
Jay034-Jul-06 8:04 
GeneralRe: .hh extension header files Pin
toxcct4-Jul-06 8:07
toxcct4-Jul-06 8:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.