| | |
homework question on class...multiple choice
![]() |
•
•
Join Date: Nov 2005
Posts: 27
Reputation:
Solved Threads: 0
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.
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.
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)
#include <iostream> using namespace std; class duck { public: virtual void speak() { cout << "quack"; } }; class swan : public duck { public: void speak() { cout << "This is a swan"; } }; class heron : public duck { public: void speak() { cout << "This is a heron"; } }; int main() { duck* myDuck = new swan; myDuck->speak(); cin.get(); return 0; }
•
•
•
•
Originally Posted by sunnypalsingh
Why is that?

(Compiler error would go something like "dptr: undeclared variable")
•
•
Join Date: Nov 2005
Posts: 27
Reputation:
Solved Threads: 0
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()
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()
>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.
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.
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)
Base *dPtr = new Derived; dPtr -> mem();
I'm here to prove you wrong.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Tiffiney Needs Help!! PLEASE! WITH C++
- Next Thread: Can you help my hw???
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






