Thread: How to redefine a constructor from parent class

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    122

    How to redefine a constructor from parent class

    Hello guys.
    Firstly I don't really know if this is possible.

    This is my Class Diagram:
    https://dl.dropboxusercontent.com/u/...0diagramV4.png

    github:
    https://github.com/CreativeSoftware/DiamondQTv3

    I want to redefine the price object of the Book Class.
    However price is defined at Products Class.

    I want the price value change according to the marker value, which is a Book attribute.
    If the marker is blue, price gets a value of 10 (e.g.), if it has another value, price is equal to 20.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,630
    Also posted here.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by jimblumberg View Post
    Also posted here.

    Jim
    Yes, I post in both places, without answers
    Thanks !

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    I do not see what is so difficult that you have to wait 10 hours for an answer on two forums when an hour or less of thinking should have led you to an answer to try.

    Looking at your class diagrams: the price member is protected and there is a public member function to change it. Therefore, you have two ways of setting the price in a Book class constructor.
    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
    Nov 2010
    Posts
    122
    Quote Originally Posted by laserlight View Post
    I do not see what is so difficult that you have to wait 10 hours for an answer on two forums when an hour or less of thinking should have led you to an answer to try.

    Looking at your class diagrams: the price member is protected and there is a public member function to change it. Therefore, you have two ways of setting the price in a Book class constructor.
    I'm learning C++, last years I used procedural languages (fortran, C) and Matlab. Everything is difficult until I know how to do.

    I'll use your "miyagi tip" to try to figure out how it works.
    I've tried first. I spend 2 hours after posting. To me it is not easy chaging programming paradigms, it's a different way of think.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by marcoesteves View Post
    I'm learning C++, last years I used procedural languages (fortran, C) and Matlab. Everything is difficult until I know how to do.

    I'll use your "miyagi tip" to try to figure out how it works.
    I've tried first. I spend 2 hours after posting. To me it is not easy chaging programming paradigms, it's a different way of think.
    I think that I got it.

    Code:
    Book l1("1231", "title", "nice Author", "Nice Publisher", 1, "blue", 1021, 10);
    
    l1.setPrice(100);
    
    string str1 = "blue";
    
         if (str1.compare(l1.marker()) == 0)
         {  cout << "the marker is blue" << "\n";
             l1.setPrice(200);
         }
         else
             l1.setPrice(400);

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Hey OP, is this what you were trying to go for?
    Code:
    #include <iostream>
    
    
    class A
    {
        private :
    
    
            int x;
    
    
        public :
        
            A( void )
            {
                x = -1;
                std::cout << "A's constructor ( " << x << " )\n";
            };
    
    
            A( int y )
            {
                x = y;
                std::cout << "New x : " << x << "\n";
            };
    };
    
    
    class B : public A
    {
    
    
        public :
    
    
            B( void )
            {
                std::cout << "B's constructor\n";
            }
    };
    
    
    int main( void )
    {
        B test;
        B::A( 15 );
    
    
        return 0;
    }

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Hey OP, is this what you were trying to go for?
    O_o

    That code almost certainly does not do what you think.

    The `B' constructur is not redifined by nor overloaded as the `A' construct. The `B' constructor just calls the `A' constructor.

    The `B::A(15);' line has nothing whatsoever to do with the `test' object which is of type `B'. The `B::A(15);' line just creates a temporary of type `A'.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Yeah, I figured the only way to change a private variable would be to do it that way. Otherwise, I think you need the member variable you're trying to change as public. I figure you could just redefine B::B to change the price assuming it's public. If it's not, then I don't think there's any to change it except by calling the class directly.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Yeah, I figured the only way to change a private variable would be to do it that way.
    O_o

    I don't understand what you are saying.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  11. #11
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Oh sorry, I thought this problem was about changing the price value in the Product class using the Book constructor. I admit, my example was off XD

    I think I'll just slink back into the shadows. I didn't really read the topic that well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cast a parent class to child
    By snowcore in forum C# Programming
    Replies: 12
    Last Post: 03-04-2011, 10:02 AM
  2. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 08:42 AM
  3. Replies: 5
    Last Post: 07-25-2008, 04:37 AM
  4. Logical parameter in parent constructor
    By g4j31a5 in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2006, 04:20 AM
  5. Accessing members of parent class
    By Snip in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2006, 01:28 PM

Tags for this Thread