944,047 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6879
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 23rd, 2006
0

homework question on class...multiple choice

Expand Post »
I have no clue what the answer is.

Which of the following dynamically allocates an object of type ourClass?

a) ourClass ourPtr;
ourPtr = new ourClass;

b) ourClass ourClassObj = new ourClass;

c) typedef ourClass* ourClassPtrType;
ourClassPtrType ourClassPtr;
ourClassPtr = new ourClassPtrType;

d) typedef ourClass* ourClassPtrType;
ourClassPtrType ourClassPtr;
ourClassPtr = new ourClassPtrType;


The class Derived is publicly derived from a class Base. The Base class has a public member function mem() that is not virtural. The member function mem() is redefined in class Derived.

Base *dPtr = new Derived;
dptr ->mem();

In the second line, which version of mem() is accessed?

I think the aswer is "b" but i'm not sure.

a) Base::mem()
b) Derived::mem()
c) This code causes an error
d) Both Base::mem() and Derived::mem()

any help will be appreciated.
Reputation Points: 10
Solved Threads: 0
Light Poster
jack223 is offline Offline
27 posts
since Nov 2005
Mar 23rd, 2006
0

Re: homework question on class...multiple choice

>Which of the following dynamically allocates an object of type ourClass?
e, none of the above.

>In the second line, which version of mem() is accessed?
c, this code causes an error.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 23rd, 2006
0

Re: homework question on class...multiple choice

Compile this code.. then try changing bits of it... what happens when you modify it to some of the scenarios given in your homework assignment..?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class duck
  5. {
  6. public:
  7. virtual void speak() { cout << "quack"; }
  8. };
  9.  
  10. class swan : public duck
  11. {
  12. public:
  13. void speak() { cout << "This is a swan"; }
  14. };
  15.  
  16. class heron : public duck
  17. {
  18. public:
  19. void speak() { cout << "This is a heron"; }
  20. };
  21.  
  22. int main()
  23. {
  24. duck* myDuck = new swan;
  25. myDuck->speak();
  26. cin.get();
  27. return 0;
  28. }
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Mar 23rd, 2006
0

Re: homework question on class...multiple choice

Quote originally posted by Narue ...
c, this code causes an error.
Why is that?
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Mar 24th, 2006
0

Re: homework question on class...multiple choice

Quote originally posted by sunnypalsingh ...
Why is that?
I think Narue is nitpicking a bit... nevertheless its the right answer according to the text in the post - The OP has probably made a typo (assuming he copied it from a book or sheet).. it took me a few reads to work out what the error was
(Compiler error would go something like "dptr: undeclared variable")
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Mar 24th, 2006
0

Re: homework question on class...multiple choice

oops~ sorry.

i made a typo on the first question....is that mean the answer is "C"

c) typedef ourClass* ourClassPtrType;
ourClassPtrType ourClassPtr;
ourClassPtr = new ourClass;

d) typedef ourClass* ourClassPtrType;
ourClassPtrType ourClassPtr;
ourClassPtr = new ourClassPtrType;
Reputation Points: 10
Solved Threads: 0
Light Poster
jack223 is offline Offline
27 posts
since Nov 2005
Mar 24th, 2006
0

Re: homework question on class...multiple choice

>i made a typo on the first question
You made a typo on both questions.

>is that mean the answer is "C"
Yes, that's more like it.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 24th, 2006
0

Re: homework question on class...multiple choice

no, i don't think i made any mis typo on the second question...i double checked with my homework sheet...maybe the professor made a mistake.

Base *dPtr = new Derived;
dPtr -> mem();

In the second line, which version of mem() is accessed?

a) Base::mem()
b) Derived::mem()
c) This code causes an error.
d) Both Base::mem() and Derived::mem()
Reputation Points: 10
Solved Threads: 0
Light Poster
jack223 is offline Offline
27 posts
since Nov 2005
Mar 24th, 2006
0

Re: homework question on class...multiple choice

>i don't think i made any mis typo on the second question
Then the worksheet had a typo. dptr is not the same as dPtr, and the example would fail to compile.
C++ Syntax (Toggle Plain Text)
  1. Base *dPtr = new Derived;
  2. dPtr -> mem();
A pointer has a real type and a virtual type. The real type of dPtr is Base (the type it was declared as), and the virtual type is Derived (the actual object type pointed to). Unless a function is declared as virtual, the real type will be used, so dPtr->mem() will call Base::mem since mem isn't virtual.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 24th, 2006
0

Re: homework question on class...multiple choice

if you look at my original question, i stated that "The Base class has a public member function mem() that is not virtural. The member function mem() is redefined in class Derived."

the base class mem() is not virtual so dPtr -> mem(); will call Base::mem??
Reputation Points: 10
Solved Threads: 0
Light Poster
jack223 is offline Offline
27 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Tiffiney Needs Help!! PLEASE! WITH C++
Next Thread in C++ Forum Timeline: Can you help my hw???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC