Thread: CHALLENGE: Classes =P

  1. #1
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68

    CHALLENGE: Classes =P

    Was burning time at work so might as well code something. Below are 4 classes. Each class has functions that print either a positive output or a negative output.

    This is mostly for the intermediate to advance level c++ coders since it requires a good knowledge of Inheritance and Abstract Classes.

    GOAL
    With just one line of code get a positive output which is in this case CORRECT OUTPUT.
    YOU CANNOT CHANGE ANY CODE only add one line where indicated in the main.
    A line of code ends with a ';'.

    I only came up with 2 possible ways to get the desired output. Good luck
    CODE
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class A
    {
    protected:
        string result;
    public:
        A() {
            result = "CORRECT OUTPUT";
        }
    
        virtual void print() = 0;
        void print2()
        {
            print();
        }
    };
    
    class B : public A
    {
    private:
    public:
        B(){} ~B()
        {
            result = "NO NO NO!!!";
        }
        virtual void print2()
        {
            cout << result << endl;
        }
    };
    
    class C : public B
    {
    private:
        string result;
    public:
        C() : B() {
            result = "CORRECT OUTPUT";
        }
    
        virtual void print() = 0;
        void print2()
        {
            print();
        }
    };
    
    class ABC : public A, public C
    {
    private:
        string result;
    public:
        ABC() : A(), C() {
            result = "DON'T OUTPUT ME!!!";
        }
    
        void print()
        {
            cout << result << endl;
        }
    
    };
    
    
    int main(int argc, char **arg)
    {
        ABC * abc = new ABC();
        //one line of code here 
        return 0;
    }
    Last edited by codeprada; 02-14-2011 at 05:05 PM.
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    delete ((std::cout << "CORRECT OUTPUT"), abc);

  3. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Quote Originally Posted by adeyblue View Post
    delete ((std::cout << "CORRECT OUTPUT"), abc);
    i'm sorry but that doesn't count. why?
    Code:
    delete ((std::cout << "I CAN OUTPUT ANYTHING I WANT"), abc);
    just by changing the string it doesn't work anymore. it's just another way of writing to the screen. you use the functions in the classes above to get your output.

    Don't give up
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
     abc->B::print2();
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    This is mostly for the intermediate to a advance level c++ coders since it requires a good knowledge of Inheritance and Abstract Classes.
    O_o

    Yea. Let's take a moment to see why that isn't the case.

    Class A is abstract by definition because of the abstract `void A:rint(void)' method.

    Class B is abstract by definition because of the inherited abstract `void A:rint(void)' method.

    Class C is abstract by definition because of the abstract `void C:rint(void)' method.

    Class C is convoluted because it and its base class, inherited through class B which inherits class A, both define a `std::string result' member.

    Class ABC is convoluted because of the ambiguous base A, of which it gets two copies.

    Class ABC is convoluted because it and both of its base classes, inherited through class C which inherits class B which inherits class A, all define a `std::string result' member.

    Now, with that in mind,

    `void A:rint(void)' can't be used because it is ambiguous.
    `void A:rint2(void)' can't be used because it is ambiguous.
    `void B:rint(void)' can't be used because it has never been defined.
    `void C:rint(void)' can't be used because it has never been defined.
    `void ABC:rint2(void)' can't be used because it is ambiguous.

    Of the tree remaining possible invocations, two use `std::string ABC::result', one directly and one indirectly.

    The last remaining possible invocation produces the correct output because of a coding practice that would never pass inspection by any decent standard.




    So, yea, this is not an intermediate level thing and certainly not an advance level thing. This is a piece of garbage that only serves to illustrate all the mistakes a newbie can make.

    Soma

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    just by changing the string it doesn't work anymore
    O_o

    Agreed. It wouldn't produce the correct output if you also changed the string in the class constructors. Of course, if you are going around changing code, you may as well correct all of the code.

    Soma

  7. #7
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
    std::cout << "CORRECT OUTPUT\n";
    Edit: couldn't help it.
    Last edited by nimitzhunter; 02-14-2011 at 06:21 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  8. #8
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Quote Originally Posted by phantomotap View Post
    So, yea, this is not an intermediate level thing and certainly not an advance level thing. This is a piece of garbage that only serves to illustrate all the mistakes a newbie can make.

    Soma
    umm dude... no one codes like this OBVIOUSLY that's why is a FREAKING CHALLENGE to get your ass thinking. i deliberately made it that way so if you don't know what you're doing then you're screwed *sigh i hate trolls
    Last edited by codeprada; 02-14-2011 at 06:31 PM.
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  9. #9
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Quote Originally Posted by nimitzhunter View Post
    Code:
     abc->B::print2();
    congratz u saw through all the mumbo jumbo i placed in there =D.
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  10. #10
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Quote Originally Posted by phantomotap View Post
    O_o

    Agreed. It wouldn't produce the correct output if you also changed the string in the class constructors. Of course, if you are going around changing code, you may as well correct all of the code.

    Soma
    i meant the string in here:
    Code:
    delete ((std::cout << "CORRECT OUTPUT"), abc);
    if u have nothing proper to say then don't post please.
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    what prevents you from just saying
    Code:
    cout << "CORRECT OUTPUT";
    ?

  12. #12
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Quote Originally Posted by bobknows View Post
    what prevents you from just saying
    Code:
    cout << "CORRECT OUTPUT";
    ?
    -__-

    it defeats the purpose of the whole thread. note the heading says classes. use a class to output the right string.

    i didn't think i had to specify that
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    i deliberately made it that way so if you don't know what you're doing then you're screwed
    You'd almost have an argument if not for the fact that one could completely correct the posted code fixing all of those problems and still have the exact same challenge. The challenge, as stated, is understanding the inheritance chain and virtual responsibilities; it has nothing to do with fixing broken code.

    I honestly doubt that anyone else would have even noticed the ambiguity and missing function definitions. Actually, I'm rather certain you didn't which is probably why you have "2 possible ways".

    i meant the string in here
    Obviously. And you'd still be changing his code to make it invalid. If you are going to change any code that attempts to solve your challenge, change the parameters of the challenge, or post invalid code (unless fixing that code is the challenge) then why bother?

    i hate trolls
    o_O

    As it stands, a strict compiler and linker set will not even produce an executable for the posted code because it isn't complete making your challenge impossible. Troll much?

    Soma

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    note the heading says classes. use a class to output the right string.
    LMAO! The `std::cout' object is an instance of a class.

    Soma (Now that was me trolling.)

  15. #15
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    @phantomotap

    i coded, inherited and abstracted what i did for a reason. 2 ways because (*abc). and abc-> are the same thing.

    Code:
    (*abc).C::B::print2();
    Code:
    (*abc).B::print2();
    Code:
    abc->B::print2();
    Code:
    abc->C::B::print2();
    if you are so pro then make one.
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The correct way to organize source code into classes
    By MathFan in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2010, 01:25 PM
  2. Designing classes and a grievance
    By tenchu in forum C++ Programming
    Replies: 7
    Last Post: 12-03-2010, 12:42 AM
  3. Multiple classes question
    By TriKri in forum C++ Programming
    Replies: 20
    Last Post: 06-11-2010, 04:03 PM
  4. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  5. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM