Class Terminology question

Hello all,

I just got a homework assignment today that has some terminology the book doesn't help me understand.

My homework assignment gives me 2 classes, one that is a base class (a few of the functions are = 0 in the .h file), and one that isn't a base class.

The second function derives from the first function, or I think it does. So as I understand it, if I'm right, that means the second class has everything in the public part of the base class, and has everything defined in the brackets of it on top of that. First and foremost, that's right...right?

Second, I have to create a new class and derive it from the second class. This will make it have everything from the public parts of the first and second classes and then what I put in the brackets correct?

now onto the terminology I'm having issues with. In the directions its saying I have to redefine 3 functions of the second class. See now I'm not sure what that means. Does it mean I have to take them out of the second class and put them into the third class? Does it mean I have to call them in the third class?

The book was no help at all, and I kind of felt embarrassed asking the teacher that, since it was last chapter. Complete strangers on the internet however, no issues whatsoever.
It would be easier to visualize what your plight is by looking at some code. Is there some example code for this assignment that you can post?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Animal
{
public:
//virtual ~Animal(){}
//...
public:
virtual void Eat()=0;
//..
private:
};
class Dog:public Animal
{//...
}

//write Eat() of class Dog;

//in main,write code like this:
//Dog* pDog = new  Dog();
//pDog->Eat(); 
A deriving class has everything from its parent class, public or not. Visibility is an entirely different story.
Hi,

The functions with = 0 are actually called pure virtual functions. They need to be redefined in the derived class.

Redefining a function in a derived class does not mean you take anything out of the base class at all. Rather, it means creating a function with the same header and giving it a different function body (implementation) in the derived class.

Also, when a class inherits members from its parent, it inherits everything, although its access is limited to whether or not which members are public, protected or private.

If you're unsure of anything else, feel free to throw me an email at: sparkprogrammer@gmail.com

Hope this helps,
Joe - C++ tutor
I won't lie, little captain was the one that seemed to be the most help. But LB was a bit of help as well.

If I'm understanding captain, then I need to create functions of the same name in the new class. But change around the interior of the class. Can I still use the old function in the old class?
Yes, C++ has a special syntax to specify which version of an overridden member to use - it involves the scope resolution operator:

1
2
3
myObject.BaseClass::function(); //use base class version
myObject.DerivingClass::function(); //use deriving class version
myObject.function(); //normal: let compiler decide 
You can use the old function from the old class, yes. A more precise example is with pointers.

As you know, a pointer can point to objects of both derived and base classes. So, if you have class B which derives from class A, then a pointer of type A can point to objects of class B as well.

However, this presents a problem. With non-virtual functions, if you have a pointer of type A, then no matter what object it is pointing to (A or B), only class A's method will be called.

Virtual methods change this. When you call a virtual method that has been defined in both class A and B, then even if your pointer is of type A, if the object pointed to is B, then B's version of the method will get called.

Non-virtual Methods:
A::function();
B::function();

A *ptr = new B; //Create an object of B and assign its address to ptr
ptr->function(); //In this case, even though you're pointing to B, A::function
//will get called.

Virtual methods:

virtual void function(); //class A
void function(); //class B (Note that you only need to specify virtual in class A)

A *ptr = new B;
ptr->function(); //B::function will get called because you defined function as
//virtual.

Hope this makes things a bit clearer.

Joe
Topic archived. No new replies allowed.