A little question about classes

This is a discussion on A little question about classes within the C++ Programming forums, part of the General Programming Boards category; Hey guys! As I'm slowly learning C++, some questions arise and I think that this is the perfect place to ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    30

    A little question about classes

    Hey guys! As I'm slowly learning C++, some questions arise and I think that this is the perfect place to ask them. Let's say we have a base class "A" and a derived class "B". To dynamically allocate object of class B we could write this
    Code:
     B *ptr = new B();
    However, I often seen that some people write this
    Code:
    A *ptr = new B();
    So, my question is: Why would you do this? If you create an object of Derived Class with a pointer to a Base Class, then you can only access the derived members of that class, losing access to the rest of the class members. What is the reason for choosing second method over first method? Thank you in advance.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Without virtual members there would prolly not be much use, no.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    That's polymorphism. It's a big subject that I'm not going to explain, so I'll be blunt with my reply.

    Imagine you have a base class that has two derived classes. I'll use the example of dog and cat of the base class pet. Now lets say we have a digital family and they have their digital pet and that digital pet is going to be a dog. Now we can do two things... we can make a space to hold the address of a dog... but let's say at some point we want that digital dog to die, and when that happens the digital family wants to buy a digital cat... but the slot they have is made for a dog... what are they going to do? Another option instead of making space for a dog is to make space for a pet. That pet can hold whatever... a dog... a cat... any other classes derived from it.

    That's one feature of polymorphism, and if you google that term you can learn a lot more.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    30
    Well what benefits would second method have if it had virtual members? By the way, Base class A contains virtual destructor.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    30
    Thank you Sly, that's just what I've been thinking about! But tell me if I'm wrong - If you use second method, you can only access the inherited members of the class. not the new ones you specify, right?

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Any scenario where you need a common interface.

    Just making this example up, may be better ones. It can create and print a binary tree. The CNode class stores the subtrees and can call the Print method the same way whether it is a node or a leaf.
    Code:
    #include <iostream>
    
    class CBase
    {
      public:
        virtual void Print() = 0;
    };
    
    class CNode : public CBase
    {
      public:
        CNode(CBase* NewLeft, CBase* NewRight)
        {
          Left = NewLeft;
          Right = NewRight;
        }
    
        virtual void Print()
        {
          std::cout << "(";
          Left->Print();
          std::cout << ", ";
          Right->Print();
          std::cout << ")";
        }
    
      protected:
        CBase* Left;
        CBase* Right;
    };
    
    class CLeaf : public CBase
    {
      public:
        CLeaf(float NewValue)
        {
          Value = NewValue;
        }
    
        virtual void Print()
        {
          std::cout << Value;
        }
    
      protected:
        float Value;
    };
    
    int main()
    {
      CBase* BinaryTree = new CNode(new CLeaf(3.0f), new CNode(new CLeaf(1.5f), new CLeaf(9.2f)));
      BinaryTree->Print();
      return 0;
    }
    Haven't tested it so it may contain errors (and does contain memory leaks).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    30
    I understand you Magos, thanks for an example. One qustion though, you declare BinaryTree with 3 arguments instead of two. Is it your mistake or I just didn't get it. OH, nevermind you did it right
    Last edited by Newbeee; 06-15-2006 at 04:33 PM.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    It might be hard to see in "linear" text code, but it has a tree shape (CNode has exactly 2 arguments and CLeaf has exactly 1). This indentation might make it clearer:
    Code:
    CBase* BinaryTree = new CNode
                            (
                              new CLeaf(3.0f),
                              new CNode
                              (
                                new CLeaf(1.5f),
                                new CLeaf(9.2f)
                              )
                            );
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    30
    Yeah, that's how i mistaken it, because it was all in one line. But that's a great example to demonstrate polymorphism!

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    Here's a link to an article I've written. It's an article about the STL (it's not finished yet, though) but the std::vector<> part has an example of polymorphism.

    http://code-dynasty.net/articles/C_A...Programming/80

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes question...
    By Raigne in forum C++ Programming
    Replies: 24
    Last Post: 09-12-2006, 02:46 AM
  2. Replies: 2
    Last Post: 07-28-2006, 03:59 PM
  3. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 03:40 AM
  4. Classes and Win32 API, and another Question
    By philvaira in forum Windows Programming
    Replies: 10
    Last Post: 04-10-2004, 08:21 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 11:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21