some inheritance questions

hi, this is my first attempt at inheritance with c++. i was following an online tutorial to help with doing a labsheet for college, thought all was going well until i tried to run the program, could someone point out where i'm going wrong? i thought i had the base class and derived classes set up properly... i'll include what we're supposed to be doing for the labsheet so as to give an idea of what i'm trying to do.

1. Write a class for a Product
− Each Product has a data member which holds the net price, and a
constructor which sets this price.
− Each Product has a method getPrice(), which calculates and
returns the gross price (the gross price includes VAT at 21%)
2. Write 2 classes which inherit from the general Product class, of type
Software and Book
− The gross price for Software includes VAT at 21%,
− Books are free of VAT, so the gross price is unchanged from the net
price, and you will need to re-define the getGrossPrice method
in this class
3. Write a program which does the following:
a. Declare an array of 10 pointers to Product
b. Declare a pointer to a Book and a pointer to Software
c. Ask the user to enter details of the book, and of the software
item, create the two items dynamically and store their
addresses in your pointers.
d. Check your method getGrossPrice works correctly with each
type, then add them as the first 2 pointers in the array of
product.

and my program...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Product.h
#ifndef Product_H
#define Product_H

class Product
{
public:
	Product();
	Product(double);
	virtual double getPrice() const;
protected:
	double netPrice;
}

#endif; 


1
2
3
4
5
6
7
8
9
10
11
//Product.cpp
#include "Product.h"

Product::Product(double np)
	:netPrice(np)
{}

double Product::getPrice() const
{
	return (netPrice * 1.21);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
//Software.h
#ifndef Software_H
#define Software_H
#include "Product.h"

class Software : public Product
{
public:
	Software(double);
	double getPrice() const;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
//Software.cpp
#include "Software.h"

Software::Software(double np)
{
	netPrice = np;
}

double Software::getPrice() const
{
	return (netPrice * 1.21);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
//Book.h
#ifndef Book_H
#define Book_H
#include "Product.h"

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

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
//Book.cpp
#include "Book.h"

Book::Book(double p)
{
	netPrice = p;
}

double Book::getPrice() const
{
	return (Product::getPrice() * .79);
}


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
//Main
#include<iostream>
using namespace std;
#include "Product.h"
#include "Software.h"
#include "Book.h"

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

	cout << "Enter cost of book:" << endl;
	double bookCost;
	cin >> bookCost;
	cout << endl;

	cout << "Enter cost of software:" << endl;
	double swCost;
	cin >> swCost;
	cout << endl;

	Software* pSoftware = new Software(swCost);
	Book* pBook = new Book(bookCost);

	cout << "Cost of the book is: " << pBook->getPrice();
	cout << "Cost of software is: " << pSoftware->getPrice();

	system("pause");
	return 0;
}


All help appreciated
1. If you use new, then you should at some point use delete.

2. If you have ANY virtual member functions in a class, you should always make the destructor virtual (otherwise only the known type's destructor is called and the ones further down the inheritance tree are not)

3. Could you at least tell us what is going wrong?
Last edited on
Topic archived. No new replies allowed.