Virtual functions and derived class

I have a header file with the following class:

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
//MyProduct.h

#ifndef SICT_MYPRODUCT_H__
#define SICT_MYPRODUCT_H__
#include "Product.h"

namespace sict {
	class MyProduct : public Product {
		char text_[10000];
		char sku[4];
		char name_[20];
		int value;
	public:
		MyProduct(const char* name);
		std::fstream& store(std::fstream& file, bool addNewLine)const;
		std::fstream& load(std::fstream& file);
		std::ostream& write(std::ostream& os, bool linear)const;
		std::istream& read(std::istream& is);
		bool operator==(const char*) const;
		double total_cost() const;
		const char* name() const;
		void quantity(int);
		int qtyNeeded() const;
		int quantity() const;
		int operator+=(int);
		bool operator>(const Product&) const;
	};
}
#endif 


And it is implemented like this:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//MyProduct.cpp

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <cstring>
#include "MyProduct.h"
using namespace std;

namespace sict {

	Product* CreateProduct() {
		return new MyProduct("non-perishable");
	}
	Product* CreatePerishable() {
		return new MyProduct("perishable");
	}
	MyProduct::MyProduct(const char* n) {
		value = 0;
		strcpy(sku, "sku");
		text_[0] = char(0);
		strcpy(name_, n);
	}
	fstream& MyProduct::store(std::fstream& file, bool addNewLine)const {
		int i = 0;
		while (text_[i]) {
			file << text_[i];
			i++;
		}
		return file;
	}
	fstream& MyProduct::load(std::fstream& file) {
		int i = 0;
		while (!file.fail()) {
			text_[i++] = file.get();
		}
		file.clear();
		if (i > 0) i--;
		text_[i] = 0;
		return file;
	}
	ostream& MyProduct::write(std::ostream& os, bool linear)const {
		for (int i = 0; text_[i]; i++) {
			if (linear && text_[i] == '\n') {
				os << " ";
			}
			else {
				os << text_[i];
			}
		}
		return os;
	}
	istream& MyProduct::read(std::istream& is) {
		is.getline(text_, 9999, EOF);
		return is;
	}
	std::ostream& operator<<(std::ostream& ostr, const Product& mf) {
		return mf.write(ostr, true);
	}
	std::istream& operator>>(std::istream& istr, Product& mf) {
		return mf.read(istr);
	}
	bool MyProduct::operator==(const char* str) const {
		return !strcmp(sku, str);
	}
	double MyProduct::total_cost() const {
		return 113.50;
	}
	const char* MyProduct::name() const {
		return name_;
	}
	void MyProduct::quantity(int i) {
		value = i;
	}
	int MyProduct::qtyNeeded() const {
		return 45;
	}
	int MyProduct::quantity() const {
		return value;
	}
	int MyProduct::operator+=(int i) {
		value += i;
		return value;
	}
	double operator+=(double& d, const Product& p) {
		d += p.total_cost();
		return d;
	}
	bool MyProduct::operator>(const Product& p) const {
		return !strcmp(name_, p.name());
	}
}


As you can see, MyProduct is a derived class from Product. We are just learning virtual functions, and here is what I came up with for Product.h:

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
//Product.h

#ifndef SICT_PRODUCT_H__
#define SICT_PRODUCT_H__
#include <fstream>

namespace sict {
	class Product {
	public:
		virtual std::fstream& store(std::fstream& file, bool newLine = true) const;
		virtual std::fstream& load(std::fstream& file);
		virtual std::ostream& write(std::ostream& os, bool linear) const;
		virtual std::istream& read(std::istream& is);
		virtual bool operator==(const char*) const;
		virtual double total_cost() const;
		virtual const char* name() const;
		virtual void quantity(int);
		virtual int qtyNeeded() const;
		virtual int quantity() const;
		virtual int operator+=(int);
		virtual bool operator>(const Product&) const;
		Product* CreateProduct();
		Product* CreatePerishable();
		std::ostream& operator<<(std::ostream&, const Product&);
		std::istream& operator>>(std::istream&, Product&);
		double operator+=(double&, const Product&);
	};
}


#endif // !SICT_PRODUCT_H__ 


Am I doing this correctly? I am getting errors, such as unresolved external symbols, and my function prototypes are underlined in green and red in my Product header file.

Please help, I dont know what to do. My professor said all implementation has been provided, I need to just provide/define/create the Product.h class.

TIA
Last edited on
If you do not want to implement a body of a virtual function you need to make it pure virtual, see:

http://www.learncpp.com/cpp-tutorial/126-pure-virtual-functions-abstract-base-classes-and-interface-classes/
Topic archived. No new replies allowed.