3

This is one of those things where i just know im doing it wrong. My assignment is simple.

Create 3 classes in c++,

product ,software ,book. product is super, book and software are product. then make an array of pointers and fill the array with software and books.

so i've done the following

int main()
{
 Product *productList[10];          


 Book *pBook;                       
 Book q(5);
 pBook = &q;
 pBook->getPrice();

 Software *pSoftware;
 Software g(5);
 pSoftware = &g;
 pSoftware ->getPrice();


 productList[0] = pSoftware; // fill it with software, cannot do this.

Is there any way of inserting a subclass into a super classes array. Or should i define the array of pointers as something else.

class definitions below

class Product
{
public:

double price;

double getPrice();

Product::Product(double price){};
};


class Book: public Product
{
public:
Book::Book(double price)
    :Product(price)
{
}
double getPrice();
};

class Software: public Product
{
public:
Software::Software(double price)
    :Product(price)                 // equivalent of super in java?
{
}                                   // code of constructor goes here.
double getPrice();
};
9
  • 1
    Is this C or C++? I guess this is obviously C++. Use a std::vector instead of an array Commented Feb 7, 2011 at 17:03
  • 1
    Can you show us the definitions of the Product, Book, and Software classes? You should be able to put all three types of pointers into a Product * array if Book and Software are all subtypes of Product. Commented Feb 7, 2011 at 17:03
  • @Falmarri it says c++ in the title. Commented Feb 7, 2011 at 17:13
  • @OVERTONE: Yes, but you're using arrays instead of std::vector, which is why I asked. Commented Feb 7, 2011 at 17:17
  • because that was the assignment. I've onyl barely touched on vecotrs, this is only an introduction to inheritance. if it helps ill add c++ to the question. Commented Feb 7, 2011 at 17:22

4 Answers 4

4

You should use public inheritance:

class Book : public Product {
...
};

[edit]

You should also declare getPrice() as virtual if you want to implement it differently in the child classes. This will make compiler call getPrice() of the right child class when you call getPrice() for a pointer to a Product:

virtual double getPrice();
Sign up to request clarification or add additional context in comments.

Comments

1

As the array is of type Product, you should declare pSoftware as a pointer to Product:

Product *pSoftware = new Software(5);
// ...
productList[0] = pSoftware;

3 Comments

This should be functionally identical to the OP's original code if Software inherits publicly from Product.
conversion to inaccesible base class product is not allowed. never seen this one before. gonna take a look online.
@Alan: Not quite. This is actually allocating space for the product objects that won't be destroyed when they're out of scope. OP is asking for trouble by assigning references to local variables.
0

It's been a while, but what's the default inheritance type in C++? Should

class Book:Product
{

be

class Book: public Product
{

It's always a good idea to be explicit anyway.

1 Comment

im here to learn :D thanks for that. im used to using extends in java, didnt know they had to be public. thanks for that
0

Can't you just cast the software* to a product* to put it in your array? productList[0] = (Product*)pSoftware;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.