Thread: Last Minute Reviewing

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    31

    Last Minute Reviewing

    I'm studying for an exam, and I had some quick questions. What does this code output?
    Code:
     
    class ExampleClass {
    private: int i;
    public:
        ExampleClass(int j) { i=j; }
        int get_i() { return i; }
    };
    
    int main() {
        ExampleClass objects[4] = {1,2,3,4};
        ExampleClass *p;
        p = &objects; // get address of objects
        cout << p->get_i() << endl; // use -> to call get_i()
        p = p +2;
        cout << p->get_i() << endl; // which object is output?
        return 0;
    }
    I'm really tempted to say ExampleClass 3, but the way the professor asked makes it seem like a trick question.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What makes you think it should NOT be 3?
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    ExampleClass 3. But like I said, that doesn't seem right.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by arrgh View Post
    ExampleClass 3. But like I said, that doesn't seem right.
    Sorry, edited my reply after re-reading your post and realizing you already said that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    There was a side note mentioning "be careful with pointer arithmetic" and 3 seems like the obvious answer, so...

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so after fixing the pointer assignment (&objects is a pointer to an array, so p = objects; is the correct assignment), and including the relevant headers, using namespace std, it outputs 1 and 3.

    However, as it stands it won't compile correctly, as it's got a funny pointer assignment [I sort of thought so]. Is it expected that you should spot stupid typo's by the teacher, or are programs (unless specifically noted) supposed to be correct code that can run?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Nope, it's just testing whether you can understand the code. Just know general output and ignore compile errors. If ExampleClass 3 is printed out, then what exactly do you have to watch out for when using pointer arithmetic with base class pointers?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by arrgh View Post
    Nope, it's just testing whether you can understand the code. Just know general output and ignore compile errors. If ExampleClass 3 is printed out, then what exactly do you have to watch out for when using pointer arithmetic with base class pointers?
    Not sure - pointer arithmetics is usually straightforward. But talking about base-classes makes me think that you are referring to inheritance, and I expect if we rewrite your example a bit:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Base
    {
    private:
      int x;
    public:
      Base() { x = 0; }
      virtual int get_i() { return x; }
    };
    
    class ExampleClass: public Base {
    private: 
      int i;
    public:
        ExampleClass(int j) { i=j; }
        virtual int get_i() { return i; }
    };
    
    int main() {
        ExampleClass objects[4] = {1,2,3,4};
        Base *p;
        p = objects; // get address of objects
        cout << p->get_i() << endl; // use -> to call get_i()
        p = p +2;
        cout << p->get_i() << endl; // which object is output?
        return 0;
    }
    then things go horribly wrong when we try to do the second get_i() call [NULL-pointer dereference, but that's just in this particular example].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The thing that you have to watch out here is probably that you see +2 in the code, so you might be tempted to think that you are now referring to the second item. But you didn't fall for it.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    then things go horribly wrong when we try to do the second get_i() call [NULL-pointer dereference, but that's just in this particular example].
    Is there a way to fix it? Or is it better to not use base pointers in derived classes?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by arrgh View Post
    Is there a way to fix it? Or is it better to not use base pointers in derived classes?
    If you have an array of pointers, you'd be fine. But if your pointer is pointing to an array of objects, then you're stuffed... The only solution is to not use a base-pointer-to-array solution...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    I see. Inheritance might have been the issue. I remember going over that in class, but that was after I got lost over the process of designing a software program to be used in the "real world", so nothing really sank in. Anyways, thanks for clearing that up!

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Meh, matsp beat me to pointing out the assignment error.

    But it would probably sneak through, because VC++ actually accepts this code. (Or used to, at least.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, 2008 sure as heck doesn't (first code).
    error C2440: '=' : cannot convert from 'ExampleClass (*)[4]' to 'ExampleClass *'
    Second code, unfortunately, does compile, but produces an access violation, as mats seems to hint.

    However, fixing the little typo problem:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class ExampleClass {
    private: int i;
    public:
    	ExampleClass(int j) { i=j; }
    	int get_i() { return i; }
    };
    
    int main() {
    	ExampleClass objects[4] = {1,2,3,4};
    	ExampleClass *p;
    	p = objects; // get address of objects (NOT &objects - don't try to take the address of an array!)
    	cout << p->get_i() << endl; // use -> to call get_i()
    	p = p +2;
    	cout << p->get_i() << endl; // which object is output?
    	return 0;
    }
    It runs fine and outputs 1 and 3. As it should.
    Last edited by Elysia; 05-07-2008 at 05:08 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    65
    Code:
    ExampleClass objects[4] = {1,2,3,4};
    Is initializing objects in that array only possible because the constructor only takes one argument? Is the [4] necessary?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wait just a minute here!!!
    By Kennedy in forum C Programming
    Replies: 7
    Last Post: 04-13-2009, 09:36 AM
  2. Printing System time and 30 minute alarm
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-17-2006, 05:10 AM
  3. Replies: 4
    Last Post: 05-13-2003, 04:54 PM
  4. Replies: 3
    Last Post: 12-16-2002, 09:55 AM