<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.newlc.com" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>NewLC - C++ Inheritance - Comments</title>
 <link>http://www.newlc.com/en/C-Inheritance.html</link>
 <description>Comments for &quot;C++ Inheritance&quot;</description>
 <language>en</language>
<item>
 <title>Base *bptr;
Derived d;
b =</title>
 <link>http://www.newlc.com/en/C-Inheritance.html#comment-2826</link>
 <description>&lt;p&gt;&lt;pre class=&quot;bb-code-block&quot;&gt;
Base *bptr;
Derived d;
b = &amp;d; // valid(  line 1)
&lt;/pre&gt;&lt;br /&gt;
You can see the derivation as a IS-A relationship. So in your example, &quot;derived&quot; IS-A &quot;base&quot; with added feature. So whenever you can use a &quot;base&quot; object, you can use a &quot;Derived&quot; object since it has all the required feature. In the snippet above, &lt;span style=&quot;font-weight:bold&quot;&gt;d&lt;/span&gt; IS-A Derived so IS-A base as well. Thus the &lt;span style=&quot;font-weight:bold&quot;&gt;bptr=&amp;d;&lt;/span&gt; expression is valid.&lt;/p&gt;

&lt;p&gt;&lt;pre class=&quot;bb-code-block&quot;&gt;
Derived *dptr;
Base b;
dptr =  &amp;b; //invalid (line 2);
&lt;/pre&gt;&lt;br /&gt;
The reverse is however not true. Derived IS-A base but Base is not a Derived since it does not implement all new functions introduced by Derived (nor it has the same data member). So here, you are trying to affect the dptr pointer with something with is just &quot;half a derived&quot;. &lt;/p&gt;

&lt;p&gt;You can try to use &quot;reinterpret_cast&quot; primitive to force the affectation but this will be very dangerous in your case.&lt;br /&gt;
&lt;/p&gt;</description>
 <pubDate>Mon, 23 Apr 2007 09:50:16 +0200</pubDate>
 <dc:creator>eric</dc:creator>
 <guid isPermaLink="false">comment 2826 at http://www.newlc.com</guid>
</item>
<item>
 <title>I have a doubt</title>
 <link>http://www.newlc.com/en/C-Inheritance.html#comment-2825</link>
 <description>&lt;p&gt;Suppose there is a base class &quot;Base&quot; and a derived &quot;Derived&quot;;&lt;/p&gt;

&lt;p&gt;And there are some casting expressions such as,&lt;br /&gt;
&lt;pre class=&quot;bb-code-block&quot;&gt;
Base *bptr;
Derived d;
b = &amp;d // valid( line 1)

//or

Derived *dptr;
Base b;
dptr =  &amp;b; //invalid (line 2);
&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;Even if the derived class has all (almost) the properties of the base class, why the pointer of its type cannot be used to hold the object of Base class??&lt;/p&gt;</description>
 <pubDate>Mon, 23 Apr 2007 05:27:55 +0200</pubDate>
 <dc:creator>santhosh.bhat</dc:creator>
 <guid isPermaLink="false">comment 2825 at http://www.newlc.com</guid>
</item>
<item>
 <title>C++ Inheritance</title>
 <link>http://www.newlc.com/en/C-Inheritance.html#comment-2548</link>
 <description>&lt;div class=&quot;spip-content&quot;&gt;&lt;p class=&quot;spip&quot;&gt;Hi,&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;I have been looking for rules or recommendations for this issue: which type of classes are you allowed to inherit from which types of classes? E.g., is it OK to inherit an R class from a T class? I understand M classes are supposed to be used for multiple inheritance and C classes always derive from CBase. But are there any other rules existing, for other class types?&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;Regards,
Pete&lt;/p&gt;&lt;/div&gt;</description>
 <pubDate>Tue, 05 Dec 2006 15:55:07 +0100</pubDate>
 <dc:creator>Anonymous</dc:creator>
 <guid isPermaLink="false">comment 2548 at http://www.newlc.com</guid>
</item>
<item>
 <title>C++ Inheritance</title>
 <link>http://www.newlc.com/en/C-Inheritance.html#comment-2547</link>
 <description>&lt;div class=&quot;spip-content&quot;&gt;&lt;p class=&quot;spip&quot;&gt;I have a basic question.&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;Instead of the following code:&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;if(shape == circle) &lt;i class=&quot;spip&quot;&gt;
        draw_circle();
&lt;/i&gt;
else if(shape == triangle) &lt;i class=&quot;spip&quot;&gt;
        draw_triangle();
&lt;/i&gt;
else if(shape == rectangle) &lt;i class=&quot;spip&quot;&gt;
        draw_rectangle();
&lt;/i&gt;
else if(shape == line) &lt;i class=&quot;spip&quot;&gt;
        draw_line();
&lt;/i&gt;&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;Would it not be better to have a virtual function draw(), and the above if/else code could be replaced with a single call to the draw() function that is implemented in the derived class.&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;If some if/else logic is required based upon the object type, can the typeid() keyword be used in Symbian OS to find out the type of the object?&lt;/p&gt;&lt;/div&gt;</description>
 <pubDate>Tue, 29 Aug 2006 00:57:41 +0200</pubDate>
 <dc:creator>Anonymous</dc:creator>
 <guid isPermaLink="false">comment 2547 at http://www.newlc.com</guid>
</item>
<item>
 <title>C++ Inheritance</title>
 <link>http://www.newlc.com/en/C-Inheritance.html#comment-2546</link>
 <description>&lt;div class=&quot;spip-content&quot;&gt;&lt;p class=&quot;spip&quot;&gt;&quot;Inheritance is not for code re-use; it&#039;s for flexibility. It is composition, which is for code re-use&quot;&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;To explain it in very simple words - When we have composition (Class B having object of Class A), now B can use the functions of class A. That is re-using the functionality of A for some other purpose with in Class B.&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;But, it makes inheritance more meeningful when we derive from (lets say class A) to give a different implementation in derived class. We can use the object of derived class as if its object of base class and we can get the different behavior also. Thats what the flexibility is. 
For getting more complex picture, you can read more about some of the design principles like Dependency Inversion Principle (DIP), Liskov Substitution Principle (LSP).&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;Regards
Girish&lt;/p&gt;&lt;/div&gt;</description>
 <pubDate>Wed, 31 May 2006 13:38:09 +0200</pubDate>
 <dc:creator>girishshetty</dc:creator>
 <guid isPermaLink="false">comment 2546 at http://www.newlc.com</guid>
</item>
<item>
 <title>C++ Inheritance</title>
 <link>http://www.newlc.com/en/C-Inheritance.html#comment-2545</link>
 <description>&lt;div class=&quot;spip-content&quot;&gt;&lt;p class=&quot;spip&quot;&gt;HI Girish,&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;Could you explain point 1. more elaborately with sample code?&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;Thanks in Advance
SAhu&lt;/p&gt;&lt;/div&gt;</description>
 <pubDate>Fri, 26 May 2006 08:16:48 +0200</pubDate>
 <dc:creator>Anonymous</dc:creator>
 <guid isPermaLink="false">comment 2545 at http://www.newlc.com</guid>
</item>
<item>
 <title>C++ Inheritance</title>
 <link>http://www.newlc.com/en/C-Inheritance.html#comment-2544</link>
 <description>&lt;div class=&quot;spip-content&quot;&gt;&lt;p class=&quot;spip&quot;&gt;Hi,&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;&gt;&gt; Can you please explain Reason No. 2 in &quot;Reasons to use inheritance&quot;. What is the connection between using if/else/ switch statements and using inheritance&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;Its like this, when we have multiple decisions to check and perform different operation depending on the condition (decision), then we can think of doing the same thing with Inheritance. Have one &lt;strong class=&quot;spip&quot;&gt;base class&lt;/strong&gt; with a method for &lt;strong class=&quot;spip&quot;&gt;operation&lt;/strong&gt; and as many as number of conditions (decisions) of &lt;strong class=&quot;spip&quot;&gt;Derived&lt;/strong&gt; class deriving from that base class and having its own implementation for that &lt;strong class=&quot;spip&quot;&gt;operation&lt;/strong&gt; declared in base class.&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;I can give you one example like this, eventhougth this one is bit specific example&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;&lt;tt&gt;&lt;div align=&#039;left&#039; class=&#039;spip_code&#039; dir=&#039;ltr&#039;&gt;if(shape == circle) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; draw_circle();&lt;br /&gt;
}&lt;br /&gt;
else if(shape == triangle) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; draw_triangle();&lt;br /&gt;
}&lt;br /&gt;
else if(shape == rectangle) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; draw_rectangle();&lt;br /&gt;
}&lt;br /&gt;
else if(shape == line) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; draw_line();&lt;br /&gt;
}&lt;/div&gt;&lt;/tt&gt;&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;In the above scenario, we can design our requirement as below.
Have a base class as Shape with Draw as virtual fun.
Implement Circle, Triangle, Rectangle, Line derived from Shape with its own implementation for Shape.&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;Regards
Girish&lt;/p&gt;&lt;/div&gt;</description>
 <pubDate>Wed, 12 Apr 2006 11:02:59 +0200</pubDate>
 <dc:creator>girishshetty</dc:creator>
 <guid isPermaLink="false">comment 2544 at http://www.newlc.com</guid>
</item>
<item>
 <title>C++ Inheritance</title>
 <link>http://www.newlc.com/en/C-Inheritance.html#comment-2543</link>
 <description>&lt;div class=&quot;spip-content&quot;&gt;&lt;p class=&quot;spip&quot;&gt;Hi Girish,
I like the fact that your articles explain a lot of programming concepts as opposed to most of the articles which are basically &quot;How-Tos&quot;.&lt;/p&gt;

&lt;p class=&quot;spip&quot;&gt;Can you please explain Reason No. 2 in &quot;Reasons to use inheritance&quot;. What is the connection between using if/else/ switch statements and using inheritance&lt;/p&gt;&lt;/div&gt;</description>
 <pubDate>Wed, 12 Apr 2006 04:35:27 +0200</pubDate>
 <dc:creator>SiriusBlack</dc:creator>
 <guid isPermaLink="false">comment 2543 at http://www.newlc.com</guid>
</item>
<item>
 <title>C++ Inheritance</title>
 <link>http://www.newlc.com/en/C-Inheritance.html</link>
 <description>&lt;div class=&quot;spip-content&quot;&gt;This article explain deep details about class inheritance, object slicing, function overloading, polymorphism, multiple and virtual inheritance. This is a must-read if you are new to C++ and want to understand one of the basic and powerful feature of this language.&lt;/div&gt;&lt;p&gt;&lt;a href=&quot;http://www.newlc.com/en/C-Inheritance.html&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://www.newlc.com/en/C-Inheritance.html#comments</comments>
 <category domain="http://www.newlc.com/en/taxonomy/term/1">Basics</category>
 <pubDate>Tue, 11 Apr 2006 15:01:41 +0200</pubDate>
 <dc:creator>girishshetty</dc:creator>
 <guid isPermaLink="false">827 at http://www.newlc.com</guid>
</item>
</channel>
</rss>
