"A" is the answer and here's why...
a - The parent operation can be overridden (replaced, in a sense) but cannot be changed.
#include <iostream>
class A {
public:
A() {}
virtual ~A() {}
virtual void foo() {
std::cout << "Hi, I'm A" << std::endl;
}
};
class B : public A {
public:
B() {}
virtual ~B() {}
virtual void foo() {
std::cout << "Hi, I'm B" << std::endl;
}
};
int main() {
A* a = new B();
a->foo();
return 1;
}
The code above will print...
Hello, I'm B
B::foo() overrode A::foo() but did not change it.
b - Yes, a subclass can use the implementation of a method in a parent class. For example, if we change the B::foo() method in the above code to...
virtual void foo() {
A::foo(); // <--------------------------NOTE the addition of this call
std::cout << "Hi, I'm B" << std::endl;
}
...the program will print...
Hi, I'm A
Hi, I'm B
c - The subclass can use public or protected attributes of the parent class but not private attributes. For example:
#include <iostream>
class A {
public:
A() : pub(1), pro(2), pri(3) {}
virtual ~A() {}
virtual void foo() {
std::cout << "Hi, I'm A" << std::endl;
}
int pub;
protected:
int pro;
private:
int pri;
};
class B : public A {
public:
B() {}
virtual ~B() {}
virtual void foo() {
std::cout << pub << std::endl;
std::cout << pro << std::endl;
std::cout << pri << std::endl; // <----COMPILE ERROR
}
};
int main() {
A* a = new B();
a->foo();
return 1;
}
That code will have a compile error on the line noted above because subclass B is attempting to access a private member of the parent class. There is an exception to this rule if another class is declared to be a friend but you should rarely, if ever, use the friend keyword.
d - Yes, a subclass can have opperations that the parent class does not have. For example:
#include <iostream>
class A {
public:
A() {}
virtual ~A() {}
virtual void foo() {
std::cout << "Hi, I'm A" << std::endl;
}
};
class B : public A {
public:
B() {}
virtual ~B() {}
virtual void foo() {
std::cout << "Hi, I'm B" << std::endl;
}
virtual void otherFunc() {
std::cout << "I'm a function that my parent class doesn't have." << std::endl;
}
};
int main() {
B b;
A& a = b;
a.foo();
a.otherFunc(); // <--- COMPILE ERROR
b.foo();
b.otherFunc(); // <--- OK
return 1;
}
In the example above, otherFunc() was added to subclass B. Attempting to call it from a reference to an A class will cause a compile error. Calling it on a B class works as expected.
The best way to learn and know for sure is to try these things in a compiler. Try the code above. I tested it in Visual Studio 2008. The examples that aren't explicitly labeled with COMPILE ERROR should work.