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?
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.
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:
#include <iostream>
usingnamespace 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;
}