homework question on class...multiple choice

Reply

Join Date: Nov 2005
Posts: 27
Reputation: jack223 is an unknown quantity at this point 
Solved Threads: 0
jack223 jack223 is offline Offline
Light Poster

homework question on class...multiple choice

 
0
  #1
Mar 23rd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: homework question on class...multiple choice

 
0
  #2
Mar 23rd, 2006
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 482
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 47
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: homework question on class...multiple choice

 
0
  #3
Mar 23rd, 2006
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..?

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: homework question on class...multiple choice

 
0
  #4
Mar 23rd, 2006
Originally Posted by Narue
c, this code causes an error.
Why is that?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 482
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 47
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: homework question on class...multiple choice

 
0
  #5
Mar 24th, 2006
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")
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 27
Reputation: jack223 is an unknown quantity at this point 
Solved Threads: 0
jack223 jack223 is offline Offline
Light Poster

Re: homework question on class...multiple choice

 
0
  #6
Mar 24th, 2006
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;
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: homework question on class...multiple choice

 
0
  #7
Mar 24th, 2006
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 27
Reputation: jack223 is an unknown quantity at this point 
Solved Threads: 0
jack223 jack223 is offline Offline
Light Poster

Re: homework question on class...multiple choice

 
0
  #8
Mar 24th, 2006
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()
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: homework question on class...multiple choice

 
0
  #9
Mar 24th, 2006
>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.
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 27
Reputation: jack223 is an unknown quantity at this point 
Solved Threads: 0
jack223 jack223 is offline Offline
Light Poster

Re: homework question on class...multiple choice

 
0
  #10
Mar 24th, 2006
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??
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC