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.

Recommended Answers

All 11 Replies

>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.

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..?

#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;
}

c, this code causes an error.

Why is that?

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")

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;

>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.

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()

>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.

Base *dPtr = new Derived;
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.

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??

>if you look at my original question, i stated that
Yes, I read it, but thanks for the reminder anyway.

>the base class mem() is not virtual so dPtr -> mem(); will call Base::mem??
Seeing as how this is a variation of what I said:

so dPtr->mem() will call Base::mem since mem isn't virtual.

It seems like you understand. The real type of dPtr is Base because it was declared as a pointer to Base. mem isn't a polymorphic function, and dPtr has no way of knowing that it actually points to an object of Derived. Therefore, Base::mem is used instead of Derived::mem.

Thanks for your help Narue...you have a great weekend.
thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.