Thread: Problem with subclass

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    66

    Problem with subclass

    Hello there, I have a problem with this:

    Code:
    #include <iostream>
    #include <string>
    
    class Monitor
    {
          class Panel
          {
          public:
                 Panel (int initialPrice);
                 ~Panel();
                 
                 // The GetFunction() section
                 int GetPrice() const { return itsPrice; }
                 void GetModel() const { std::cout << itsModel; }
                 int GetBrightness() const { return itsBrightness; }
                 int GetContrast() const { return itsContrast; }
                 int GetTimeResponse() const { return itsTimeResponse; }
                 void GetMaxRes() const { std::cout << itsMaxRes; }
                 
                 // The SetFunctions()
                 int SetPrice(int price) { itsPrice = price; }
                 void SetModel(std::string model) { itsModel = model; }
                 int SetBrightness(int brightness) { itsBrightness = brightness; }
                 int SetContrast(int contrast) { itsContrast = contrast; }
                 int SetTimeResponse(int timeResponse) { itsTimeResponse = timeResponse; }
                 int SetMaxRes(int maxRes) { itsMaxRes = maxRes; }
                 
                 
          private:
                  int itsPrice;
                  std::string itsModel;
                  int itsBrightness;
                  int itsContrast;
                  int itsTimeResponse;
                  int itsMaxRes;
          };
          
          class Frequency
          {
                public:
                       // The GetFunctions
                       int GetHFreq()const { return itsHFreq; }
                       int GetVFreq()const { return itsVFreq; }
                       
                       // The SetFunctions
                       int SetHFreq(int HFreq) { itsHFreq = HFreq; }
                       int SetVFreq(int VFreq) { itsVFreq = VFreq; }
                       
                private:
                        int itsHFreq;
                        int itsVFreq;
          };
    };
    
    Monitor::Panel::Panel(int initialPrice) { itsPrice = initialPrice; }
    Monitor::Panel::~Panel() {}
    
    int main()
    {
        // Set the Values
        
        Monitor Samsung;
        Samsung.Panel.SetPrice(350);
        
        return 0;
    }
    I get:
    int main()':
    7 line `class Monitor::Panel' is private
    63 line within this context
    63 line invalid use of `class Monitor::Panel'


    What I have to do that it can fix this error ? What I am doing wrong ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are using nested classes, not subclasses. I admit that I am not all that familiar with nested classes, but your first problem is that Panel is in the private scope of Monitor. Remember that class members have private access by default.

    Another problem is that Samsung.Panel is invalid syntax. Samsung is an object, but Panel is a class. If Monitor had a Panel object, then Samsung could access that Panel object. However, it looks like Monitor is nothing more than a namespace, so you should make it a namespace instead of a class.
    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

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    Well I manage to fix the private area, so now it looks like:

    Code:
    #include <iostream>
    #include <string>
    
    class Monitor
    {
          public:
                 
          class Panel
          {
          public:
                 Panel (int initialPrice);
                 ~Panel();
                 
                 // The GetFunction() section
                 int GetPrice() const { return itsPrice; }
                 void GetModel() const { std::cout << itsModel; }
                 int GetBrightness() const { return itsBrightness; }
                 int GetContrast() const { return itsContrast; }
                 int GetTimeResponse() const { return itsTimeResponse; }
                 void GetMaxRes() const { std::cout << itsMaxRes; }
                 
                 // The SetFunctions()
                 int SetPrice(int price) { itsPrice = price; }
                 void SetModel(std::string model) { itsModel = model; }
                 int SetBrightness(int brightness) { itsBrightness = brightness; }
                 int SetContrast(int contrast) { itsContrast = contrast; }
                 int SetTimeResponse(int timeResponse) { itsTimeResponse = timeResponse; }
                 int SetMaxRes(int maxRes) { itsMaxRes = maxRes; }
                 
                 
          private:
                  int itsPrice;
                  std::string itsModel;
                  int itsBrightness;
                  int itsContrast;
                  int itsTimeResponse;
                  int itsMaxRes;
          };
          
          class Frequency
          {
                public:
                       // The GetFunctions
                       int GetHFreq()const { return itsHFreq; }
                       int GetVFreq()const { return itsVFreq; }
                       
                       // The SetFunctions
                       int SetHFreq(int HFreq) { itsHFreq = HFreq; }
                       int SetVFreq(int VFreq) { itsVFreq = VFreq; }
                       
                private:
                        int itsHFreq;
                        int itsVFreq;
          };
    };
    
    Monitor::Panel::Panel(int initialPrice) { itsPrice = initialPrice; }
    Monitor::Panel::~Panel() {}
    
    int main()
    {
        // Set the Values
        
        Monitor Samsung;
        Samsung.SetPrice(50);
        
        
        return 0;
    }
    But I do not know things about nested class... So ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But I do not know things about nested class... So ?
    Change Monitor to a namespace. Since a Panel has a Frequency, make a Frequency object a member of Panel.

    Alternatively, pull out Panel and Frequency from Monitor, then make Monitor have a Panel member variable, and Panel have a Frequency member variable.
    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

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    Could you please post the source ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This would be an example of the first possibility:
    Code:
    namespace Monitor
    {
        class Frequency
        {
        public:
            // The GetFunctions
            int GetHFreq() const { return itsHFreq; }
            int GetVFreq() const { return itsVFreq; }
    
            // The SetFunctions
            int SetHFreq(int HFreq) { itsHFreq = HFreq; }
            int SetVFreq(int VFreq) { itsVFreq = VFreq; }
    
        private:
            int itsHFreq;
            int itsVFreq;
        };
    
        class Panel
        {
        public:
            explicit Panel(int initialPrice) : itsPrice(initialPrice) {}
    
            // The GetFunction() section
            int GetPrice() const { return itsPrice; }
            void GetModel() const { std::cout << itsModel; }
            int GetBrightness() const { return itsBrightness; }
            int GetContrast() const { return itsContrast; }
            int GetTimeResponse() const { return itsTimeResponse; }
            void GetMaxRes() const { std::cout << itsMaxRes; }
    
            // The SetFunctions()
            int SetPrice(int price) { itsPrice = price; }
            void SetModel(std::string model) { itsModel = model; }
            int SetBrightness(int brightness) { itsBrightness = brightness; }
            int SetContrast(int contrast) { itsContrast = contrast; }
            int SetTimeResponse(int timeResponse) { itsTimeResponse = timeResponse; }
            int SetMaxRes(int maxRes) { itsMaxRes = maxRes; }
    
        private:
            int itsPrice;
            std::string itsModel;
            int itsBrightness;
            int itsContrast;
            int itsTimeResponse;
            int itsMaxRes;
            Frequency itsFrequency;
        };
    }
    
    int main()
    {
        // Set the Values
    
        Monitor::Panel Samsung(350);
    }
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM