Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Inheritance- how to access base class member from derived class member?

Inheritance- how to access base class member from derived class member?

Scheduled Pinned Locked Moved C / C++ / MFC
oophelptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike the Red
    wrote on last edited by
    #1

    I'm trying to take an existing class and add a member function to it. Something like this:

    class baseclass {
    public:
    baseclass();
    baseclass(int i);
    ~baseclass();
    public:
    int a() { //Impl...};
    int b() { //Impl...};
    }
    class derived : public baseclass {
    public:
    derived() : base();
    derived(int i) : base(i);
    public:
    int sum() { return baseclass->a() + baseclass->b(); }; // I DO know it's not done like this...
    }

    I know I have to explicitly declare the constructors, but I'm under the impression that a() and b() are inherited without further effort. Which means if I create an object derived d, then int i = d.a(); is a valid expression. Do I have to "explicitly inherit" a() and b() in order to use them in sum() ? I've been all over the documentation and the articles here, but can't seem to track this down. More nooB questions, I know - any help is appreciated. MZR

    S D M 3 Replies Last reply
    0
    • M Mike the Red

      I'm trying to take an existing class and add a member function to it. Something like this:

      class baseclass {
      public:
      baseclass();
      baseclass(int i);
      ~baseclass();
      public:
      int a() { //Impl...};
      int b() { //Impl...};
      }
      class derived : public baseclass {
      public:
      derived() : base();
      derived(int i) : base(i);
      public:
      int sum() { return baseclass->a() + baseclass->b(); }; // I DO know it's not done like this...
      }

      I know I have to explicitly declare the constructors, but I'm under the impression that a() and b() are inherited without further effort. Which means if I create an object derived d, then int i = d.a(); is a valid expression. Do I have to "explicitly inherit" a() and b() in order to use them in sum() ? I've been all over the documentation and the articles here, but can't seem to track this down. More nooB questions, I know - any help is appreciated. MZR

      S Offline
      S Offline
      Sarath C
      wrote on last edited by
      #2

      no need to any further steps. as you've publically inherited from base class, the public and protected attributes and functions are part of derived class. <blockquote class="FQ"><div class="FQA">Mike the Red wrote:</div>int sum() { return baseclass->a() + baseclass->b(); }; // I DO know it's not done like this...</blockquote> It should be int sum() { return baseclass::a() + baseclass::b(); }; is it really not possible for you to call d.a()? what's the problem you're facing?

      -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

      My blog - Sharing My Thoughts

      1 Reply Last reply
      0
      • M Mike the Red

        I'm trying to take an existing class and add a member function to it. Something like this:

        class baseclass {
        public:
        baseclass();
        baseclass(int i);
        ~baseclass();
        public:
        int a() { //Impl...};
        int b() { //Impl...};
        }
        class derived : public baseclass {
        public:
        derived() : base();
        derived(int i) : base(i);
        public:
        int sum() { return baseclass->a() + baseclass->b(); }; // I DO know it's not done like this...
        }

        I know I have to explicitly declare the constructors, but I'm under the impression that a() and b() are inherited without further effort. Which means if I create an object derived d, then int i = d.a(); is a valid expression. Do I have to "explicitly inherit" a() and b() in order to use them in sum() ? I've been all over the documentation and the articles here, but can't seem to track this down. More nooB questions, I know - any help is appreciated. MZR

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Mike the Red wrote:

        Do I have to "explicitly inherit" a() and b() in order to use them in sum() ? I've been all over the documentation and the articles here, but can't seem to track this down.

        What's wrong with:

        class baseclass
        {
        public:
        baseclass(){}
        baseclass(int i){}
        ~baseclass(){}
        public:
        int a() {return 1;}
        int b() {return 2;}
        };

        class derived : public baseclass
        {
        public:
        derived(){}
        derived(int i):baseclass(i){}
        public:
        int sum()
        {
        return a() + b();
        }
        };

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        1 Reply Last reply
        0
        • M Mike the Red

          I'm trying to take an existing class and add a member function to it. Something like this:

          class baseclass {
          public:
          baseclass();
          baseclass(int i);
          ~baseclass();
          public:
          int a() { //Impl...};
          int b() { //Impl...};
          }
          class derived : public baseclass {
          public:
          derived() : base();
          derived(int i) : base(i);
          public:
          int sum() { return baseclass->a() + baseclass->b(); }; // I DO know it's not done like this...
          }

          I know I have to explicitly declare the constructors, but I'm under the impression that a() and b() are inherited without further effort. Which means if I create an object derived d, then int i = d.a(); is a valid expression. Do I have to "explicitly inherit" a() and b() in order to use them in sum() ? I've been all over the documentation and the articles here, but can't seem to track this down. More nooB questions, I know - any help is appreciated. MZR

          M Offline
          M Offline
          Mike the Red
          wrote on last edited by
          #4

          ...Error Between Keyboard and Chair... I get a lot of these... :doh: Thanks, guys!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups