How to print certain members of class array? *new to classes*

I need to print a table of data from a class type array. I am just unsure how to format the print line. I am looking back at a program I made using structures and I thought the print would be similar but I have hit a wall. I will put the class header code and the main program.
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
#pragma once
#include "stdafx.h"
#include<iostream>
#include <string>

using namespace std;

class Invoice
{
public:
	Invoice(int num, string desc, int quan, double p);
	int getPartNum() const;
	string getPartDes() const;
	int getQuantity() const;
	double getPrice() const;
	Invoice();
	~Invoice();


private:
	int partNum;
	string partDes;
	int quantity;
	double price;
};


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
#include "stdafx.h"
#include<iostream>
#include <string>
#include "Invoice.h"


using namespace std;


Invoice::Invoice()
{
}


Invoice::~Invoice()
{
}

Invoice::Invoice(int num, string desc, int quan, double p)

{
	num = partNum;
}

int Invoice::getPartNum(int num)
{
	partNum = num;
}

string Invoice::getPartDes() const
{
	return partDes;
}

int Invoice::getQuantity() const
{
	return quantity;
}

double Invoice::getPrice() const
{
	return price;
}

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
// Unit 14.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include "Invoice.h"
#include <string>

using namespace std;


int main()
{


	Invoice invoices[8] = 
	{
		Invoice(83, "Electric sander", 7, 57.98),
		Invoice(24, "Power saw", 18, 99.99),
		Invoice(7, "Sledge hammer", 11, 21.5),
		Invoice(77, "Hammer", 76, 11.99),
		Invoice(39, "Lawn mower", 3, 79.5),
		Invoice(68, "Screwdriver", 106, 6.99),
		Invoice(56, "Jig saw", 21, 11.00),
		Invoice(3, "Wrench", 34, 7.5)
	};

	for (int i = 0; i < 9; i++)
	{
		cout << invoices[i].getPartNum() << endl;
	}

	return 0;
}


Last edited on
First, how many elements are there in array invoices?
How many elements do you print?

Can you explain this constructor?
1
2
3
4
Invoice::Invoice( int num, string desc, int quan, double p )
{
	num = partNum;
}



You should use member initializer list in constructor. An example:
1
2
3
4
Invoice::Invoice( int num,  string desc )
 : partNum( num ), partDes( desc )
{
}



How does this function differ from the other Invoice::get* functions?
1
2
3
4
int Invoice::getPartNum(int num)
{
	partNum = num;
}

Does your compiler or linker mention any warnings or errors? It should.
Each invoice should have four elements, as listed under my private access within my class. Could you explain member initializers or link me something that I can read?
Basically a duplicate http://www.cplusplus.com/forum/beginner/236369/
Even though this one came first.
Last edited on
Topic archived. No new replies allowed.