Need help with classes

Hey all,

I can get the out put for the child classes to display but not the parent class.

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
32
33
34
35
36
37
  #ifndef PRODUCT_H//This builds a library of the Product class so that it can be called on. 
#define PRODUCT_H
#include <iostream>
#include <string>
using namespace std;

class Product
{
protected:
	int id_code;
	string product_name;
	double price;
	int in_stock;

public:
	//constructors
	Product();
	Product(int c, string n, double p, int s);

	//public getters ( accessors )
	int get_id_code() { return id_code; }
	string get_product_name() { return product_name; }
	double get_price() { return price; }
	int get_in_stock() { return in_stock; }

	// public setters ( mutators )
	void set_id_code(int id) { id_code = id; }
	void set_product_name(string name) { product_name = name; }
	void set_price(double current_price) { price = current_price; }
	void set_in_stock(int stock) { in_stock = stock; }

	friend ostream &operator << (ostream &output, const Product &my_product);
	friend istream &operator >> (istream &input, Product &my_product);

	void print_info();
};
#endif 
Last edited on
Product.cpp
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

#include <iostream>
#include <string>
#include "Product.h"
using namespace std;

Product::Product()//Constructor variables are initialized 
{
	id_code = 0;
	product_name;
	price = 0;
	in_stock = 0;
}
Product::Product(int c, string n, double p, int s)//Secondary constructor variables are defined
{
	id_code = c;
	product_name = n;
	price = p;
	in_stock = s;
}
void Product::print_info()
{
	cout << "Product ID: " << id_code << endl
		<< "Product Name: " << product_name << endl
		<< "Product Price: " << price << endl
		<< "Number in stock: " << in_stock << endl;
}

ostream &operator << (ostream &output, const Product &my_product)
{
	output << "ID Code:" << my_product.id_code << endl << "Product Name:" << my_product.product_name << endl << "Price:" << my_product.price << endl << "In Stock:" << my_product.in_stock << endl;
	return output;
}

istream &operator >> (istream &input, Product &my_product)
{
	input >> my_product.id_code >> my_product.product_name >> my_product.price >> my_product.in_stock;
	return input;
}
Dvd.cpp

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

#include <iostream>
#include <string>
#include "Dvd.h"
using namespace std;

Dvd::Dvd() :Product() //Constructor variables are initialized 
{
	studio;
	male_lead;
	female_lead;
}
Dvd::Dvd(int c, string n, double p, int s, string st, string m, string f) :Product() //Secondary constructor variables are defined
{
	c;
	n;
	p;
	s;
	studio = st;
	male_lead = m;
	female_lead = f;
}
void Dvd::print_info()
{
	Product::print_info();
	cout << "Studio: " << studio << endl
		<< "Male Lead: " << male_lead << endl
		<< "Female Lead: " << female_lead << endl;
}
main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>// iostream is needed for cout
#include <string>// string is needed for string variables 
#include "Product.h"//includes the file Product.h that contains the class used
#include "Dvd.h"
#include "Book.h"
using namespace std;

void main()
{
	Dvd my_dvd(1234, "Walking Dead", 33.99, 4, "AMC", "Andrew Lincoln", "Sarah Wayne Callies");
	my_dvd.print_info();
	Book my_book(4321, "Slaughterhouse Five", 9.99, 6, "Dial Press", "Kurt Vonnegut", 123456);
	my_book.print_info();
}
oh god, please next time put everything in one post... just under eachother.

I dont see you calling the print function for the parent class anywhere?
It wouldn't let me.... it kept giving an error and crashing on the site saying sorry ect.....

I called it in the Dvd.cpp under the function itself. It prints out the text that I entered by says everything is zero.
Im fairly sure your error was not "sorry". please edit the main and show us how you did it with the parent class, and copy paste the errors :)

Edit: Jeezus Im dumb af. The site said sorry well alright then :D. But yeh, show us the errors and edit it so the call for the parent class thingy is included.
Last edited on
I found it. Think ive been staring at it for too long lol
Topic archived. No new replies allowed.