Thread: Function wont work! inheritence problem ??

  1. #1
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90

    Function wont work! inheritence problem ??

    Code:
    #include <iostream> 
    #include "CompactDisk.h"
    #include "Program.h"
    #include "Music.h"
    
    
    using namespace std;
    
    
    int main() {
        
        CompactDisk* CD[10];
        CD[0] = new Program();
    
    
        cout << CD[0]->getBCode() << endl;
        cout << CD[0]->getLabel() << endl;
        cout << CD[0]->getNofChosers() << endl;
        cout << CD[0]->AviableDiskSize() << endl;
        CD[0]->infoList();
        cout << CD[0]->getOS() << endl;
    
    
    
    
    
    
        return 0;
    }
    Code:
    #ifndef PROGRAM_H
    #define PROGRAM_H
    #include <iostream> 
    #include "CompactDisk.h"
    
    
    using namespace std;
    
    
    class Program : public CompactDisk {
    private:
        char* programList[10];
        char* OS; //operating sys
        int prgCount;
    
    
    public:
        Program();
        ~Program();
    
    
        char* getProgramList() const;
        void setProgramList(char*);
        char* getOS() const;
        void setOS(char*);
    
    
        virtual int AviableDiskSize();
        virtual void infoList();
    
    
    };
    
    
    #endif PROGRAM_H

    I try to use getOS function which belongs to class Program.
    Program inherite from CompactDisk.
    why it wont recognize it in main ??
    CD[0] will use only CompactDisk methods!!

  2. #2
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Ok before I am attacked by someone whom is more refreshed on C++ then I am I just I been using C# and Java ALOT and very little C++. If you do not use it you loose it but this seems to be a very good lesson is OOP.

    Are you having issue accessing the field variables in main? Before I lead you astray try to pass in CompactDisk to Program in the constructor. Now Program will know about CompactDisk. If this works I will explain more in detail but if not I will be quite.

  3. #3
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    what do you mean pass in CompactDisk to Program in the constructor ?

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    Program( CompactDisk disk);
    When you create a new compactDisk into Program in main it is aware of the fields of compactDisk

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by noririco
    CD[0] will use only CompactDisk methods!!
    CD is an array of 10 pointers to CompactDisk. Therefore, CD[0] is a pointer to CompactDisk. As such, you can only access CompactDisk members from CD[0], even though CD[0] actually points to a Program object.

    If you need to access Program specific members, then it is possible that CD should not have been an array of pointers to CompactDisk, or if you really must, you could do a dynamic_cast. That said, it could be that you do not actually need to access Program specific members, but rather you need to make good use of inheritance and polymorphism.

    EDIT:
    Quote Originally Posted by jocdrew21
    Before I lead you astray try to pass in CompactDisk to Program in the constructor. Now Program will know about CompactDisk.
    Unfortunately, that is leading noririco astray: since Program inherits from CompactDisk, it already knows about the non-private members of its CompactDisk base subobject.
    Last edited by laserlight; 01-02-2015 at 08:13 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    it already knows about the non-private members of its CompactDisk base subobject.
    I was not sure if he was trying to access private members. ie if there was a private variable in one of his functions what was suppose to return it value. The cpp file was not posted but I knew eventually someone much smarter then I would come a long and correct us

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-03-2013, 10:03 PM
  2. Replies: 0
    Last Post: 02-16-2012, 11:47 AM
  3. y wont this work this way
    By gamer4life687 in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2005, 09:39 PM
  4. My Pop function wont work - can you help?
    By lindilou in forum C++ Programming
    Replies: 0
    Last Post: 05-04-2003, 02:31 PM
  5. why wont this work???
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-01-2002, 02:25 PM