Inheritance/Polymorphism

Hey i'm trying to figure out inheritance/polymorphism and i'm a bit confused

Given the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Item
{
	public:
		void display();
	protected:
		string seller_name;
		int price;
};



class painting_record: public Item
{
	public:
		painting record(string seller_name, int price, int medium);
		void display();


	private:
		int medium;
 		const string[] med;


};


Does this mean if i call the painting_record constructor that its instance will contain seller_name and price as well as medium and array med?

Also if i wanted to create different methods for void display() in derived classes can i simply just write different ones in the class-specific method definitions?
Last edited on
yes calling painting_record also contain the members of Item and painting_record

yes you can write void display() on every derived classes to override base method
yes you can write void display() on every derived classes to override base method


But you'd have to include the virtual keyword in the declaration of display() in the base class.

EDIT: Or to be more specific, you'd have to use the virtual keyword only if you want to access the different implementations of display() while using pointers to the base class. If you plan to use objects directly rather than pointers, or you use pointers to the derived classes, then you do not need the virtual keyword.
Last edited on
Could you explain this more to me? This is something I didn't quite pick up in class.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class ClassA
{
public:
// construct/destruct

    void print(){ cout << "Hello"; }
};

class ClassB
{
public:
// construct/destruct

    void print(){ cout << "World"; }
};

class ClassC : public ClassA, public ClassB  // I don't even know how to make two.
{
public:
// construct/destruct

    void print(){ cout << "Nice to meet you"; }
};


What means (if any) do I have to access all three prints?
You have access to all three if you take advantage of polymorphism and pointers. But if you use objects of ClassC directly, you only have access to the implementation of print() in ClassC:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

class ClassA
{
public:
    void print(){ cout << "Hello\n\n"; }
};

class ClassB
{
public:
    void print(){ cout << "World\n\n"; }
};

class ClassC : public ClassA, public ClassB
{
public:
    void print(){ cout << "Nice to meet you\n\n"; }
}c;

int main()
{
    ClassA * pc1 = new ClassC; //Pointer to base class ClassA that 
//references an object of classC. So *pc1 inherits the members from ClassA.
    ClassB * pc2 = new ClassC; //Pointer to base class ClassB that 
//references an object of classC. So *pc2 inherits the members from ClassB.
    c.print(); //Accessing the implementation of display() in ClassC directly without using pointers.
    pc1->print();
    pc2->print();
    return 0;
}


Output:

Nice to meet you

Hello

World



Read the tutorials on this site:

http://cplusplus.com/doc/tutorial/inheritance/

http://cplusplus.com/doc/tutorial/polymorphism/

EDIT: As a test, try putting the virtual keyword before the declarations of display() in ClassA or ClassB and see how it changes the output.
Last edited on
Topic archived. No new replies allowed.