Using classes

Pages: 123
closed account (48T7M4Gy)
Okay so I can basically just remodify your code to fit what I need it to do?

Yes and no. Read it critically and preferably don't just copy it underoathed.

1
2
3
4
5
6
bool artwork::make_bid(double, int)
{
if (make_bid < currentHighBid)
return bidderID;
return make_bid;
} 


The killer with C++ is you can't have 2 returns the way you have shown.

You could however do something like
1
2
3
4
5
6
7
8
9
10
11
bool Artwork::make_bid(double aBid, int aBidderID)
{
	if (aBid < currentHighBid)
		return false;
	else
	{
		currentHighBid = aBid;
		bidderID= aBidderID; // should be maxBid??
		return true;
	}
}


And that would return an answer to a question of the type "is this bid by bidder no. the highest bid?"
Last edited on
Okay here is the bool code that I have so far. It doesn't return any errors and executes. Is this syntax correct? Like will it still work like I think it would?


bool artwork::make_bid(double aBid, int bidderID)
{
if (aBid < currentHighBid && aBid >= minBid)
return currentHighBid;
else
{
currentHighBid = aBid;
return currentHighBid;
}
}
Last edited on
closed account (48T7M4Gy)
Not quite! Maybe you should test it with some real data via a few lines as I did in main and you'll see the answers don't make sense.

(There was a small blooper I've fixed with bidderID above. Might make it a bit clearer. Sorry about that - 'mille pardon')
Last edited on
Hmm you are right that won't work. I don't quite get where I'm going wrong here.
closed account (48T7M4Gy)
Can I suggest you forget for a moment the actual code and write down some pseudocode . But only do that after you know what the question is you are trying to answer by sending data to the Artwork object.

Just going back for a second, the scenario might be:
1. I have an Artwork for sale, say picasso. (created in main)
2. I have just received a bid (in main) $201.50 say, from bidderID 2047 for arguments sake. (in main)
3. I want to know if this is the highest bid and if so I need to record who the bidder was.
4. so I send a message from main to picasso as follows,
4a bool answer = picasso.make_bid(201.50, 2047);
4b while at it if the bid is the highest so far, then update both records for picasso and report back (answer = true) that it was the highest bid so far
4c if it's not then report back that (answer = false) (ie not the highest)
5. So, back at main I know answer = true or false whether the bids are rising and a whether a low bid needs to be ignored

Long-winded but I hope that helps. See also above revision listing. (it's equivalent to a sequence diagram IIRC)
Last edited on
As you are returning you could also write the make_bid() method like this.

1
2
3
4
5
6
7
8
9
bool Artwork::make_bid(double aBid, int aBidderID)
{
	if (aBid < currentHighBid)
		return false;

	currentHighBid = aBid;
	bidderID= aBidderID; // should be maxBid??
	return true;
}


I prefer this form as it's a little less cluttered. Of course, you only skip the else when you're returning.
Last edited on
Okay so I got the cpp file and the header file figured out. I'm having an issue with using the show function to display information. The information is supposed to be derived from this:

A client submits a work to be auctioned. The company assigns the new item the code 3398. The owner id is 2758. The artist is Andrew Miller. The title is Snowy Day. The minimum bid the client will accept is $75,000 (The second constructor). Use the show member function to display the object.

Here is my code

Project2_main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream>
#include "project2_Artwork.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	
	artwork Art1(9979, 2252);
	Art1.show();
	
	cout << "\n" << endl;
	
	artwork Art2(3398, 2758, "Andrew Miller", "Snowy Day", 75,000)
	Art2.show();


	return 0;
}
closed account (48T7M4Gy)
Just before I have a look is there any particular reason for using parameters in main
int main(int argc, char** argv)
There's nothing wrong with doing it but are you planning to use them?
closed account (48T7M4Gy)
You need to give a bit more detail on what the trouble is with the show method.

The one I have, and you don't have to copy it, takes the current Artist (your artist) and prepares a string based on that objects properties and returns that string.
1
2
3
4
5
6
7
8
string Artwork::show()
{
	string output = "**" + firstName + " ";
	output += lastName + " ";
	output += "Art No.: " + to_string(artworkID) + " ";
	output += "Own No.: " + to_string(ownerID) + " ";
	return output;
}


You can then print out the string or do whatever you want to do with it.

If you adopt that way of doing it your code would be:

1
2
artwork Art2(3398, 2758, "Andrew Miller", "Snowy Day", 75,000)
	cout << Art2.show() << endl;
for arguments sake.

or you could have:
1
2
string somestuff = Art2.show();
blah blah uppercase etc 
Last edited on
The issue is I don't think I'm using the syntax correctly to show the 2nd artwork input.

The syntax is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>
#include "project2_Artwork.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	
	artwork Art1(9979, 2252);
	Art1.show();
	
	cout << "\n" << endl;
	
	artwork Art2(3398, 2758, "Andrew", "Miller", 75000);
	Art2.show();


	return 0;
}


My concern is what is inside the () of Art2. All this code is do to is take the input inside () and assign it to the variables in the class. Do I need to post all of my code?
closed account (48T7M4Gy)
Do I need to post all of my code?


No not really because it might be easier than you think to fix it. Let me have a look at the constructor and your show() method.
Okay here is the show method:

1
2
3
4
5
6
7
8
9
10
11
12

void artwork::show()
{
	cout << "The artwork ID is: " << artworkID << endl;
	cout << "The owner ID is:" << ownerID << endl;
	cout << "The artist name is: " << artist_fname + ' ' + artist_lname << endl;
	cout << "The Title is:" << title << endl;
	cout << "The miminum bid is:" << minBid << endl;
	cout << "The current high bid is:" << currentHighBid << endl;
	cout << "The bidder ID is:" << bidderID << endl;
}
closed account (48T7M4Gy)
If you look at the constructor in your OP header file you have
artwork(int, int, string, string, string, double) but you haven't got a corresponding definition in the cpp file. Maybe you have in your latest, I don't know. Same for other configurations you may or may not have for the constructor. If you don't have one specifically for the one you have on line 14 immediately above then that may be where it's going wrong or it's not clear to you.

You must have a constructor that follows that prototype and sets the attributes or set them with set methods and that's the hard way and probably going to lose you marks. (unless you have defaults but put that aspect to one side for another time :) )

Your show() method you just posted won't cout the attributes unless you have the appropriate constructor as I mentioned or if you set them with the appropriate set method.

Okay here is my updated code

Project2_Artwork.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 PROJECT2_ARTWORK_H
#define PROJECT2_ARTWORK_H
#include <string>
#include <iostream>

using namespace std;


class artwork
{
	private:
		int artworkID;
		int ownerID;
		string artist_fname;
		string artist_lname;
		string title;
		double minBid;
		double currentHighBid;
		int bidderID;
	public:
		void set_artist(string, string);
		void set_title(string);
		void set_minBid(double);
		int get_artworkID();
		int get_ownerID();
		string get_artist();
		string get_title();
		double get_minBid();
		void show();
		bool make_bid (double, int);
		artwork(int, int); 
		artwork(int, int, string, string, string, double);
		
};
#endif


Project2_Artwork.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
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
93
94
95
96
97
98
99
100
101

#include "project2_Artwork.h"
#include <string>
#include <iostream>

using namespace std;

void artwork::set_artist (string fname, string lname)
{
	artist_fname = fname;
	artist_lname = lname;
}

void artwork::set_title (string T)
{
	title = T;
}

void artwork::set_minBid(double mBid)
{
	minBid = mBid;
}

int artwork::get_artworkID()
{
	return artworkID;
}

int artwork::get_ownerID()
{
	return ownerID;
}

string artwork::get_artist()
{
	return artist_fname + ' ' + artist_lname;
}

string artwork::get_title()
{
	return title;
}

double artwork::get_minBid()
{
	return minBid;
}

void artwork::show()
{
	cout << "The artwork ID is: " << artworkID << endl;
	cout << "The owner ID is:" << ownerID << endl;
	cout << "The artist name is: " << artist_fname + ' ' + artist_lname << endl;
	cout << "The Title is:" << title << endl;
	cout << "The miminum bid is:" << minBid << endl;
	cout << "The current high bid is:" << currentHighBid << endl;
	cout << "The bidder ID is:" << bidderID << endl;
}

bool artwork::make_bid(double aBid, int bidID)
{
	bool found = true;
	if(aBid >= minBid && aBid > currentHighBid)
	{
		currentHighBid = aBid;
		bidderID = bidID;
		found = true;
		return found;
	}
	else
	{ 
		found = false;
		return found;
	}
}


artwork::artwork(int aw, int oi)
{
	artworkID = aw;
	ownerID = oi;
	artist_fname = "";
	artist_lname = "";
	title = "";
	minBid = 0.0;
	currentHighBid = 0.0;
	bidderID = 0;
}
artwork::artwork(int aw, int oi, string z, string x, string u, double b)
{
	artworkID = aw;
	ownerID = oi;
	artist_fname = z;
	artist_lname = x;
	title = u;
	minBid = b;
	currentHighBid = 0.0;
	bidderID = 0;
	
}


Project2_main.coo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <iostream>
#include "project2_Artwork.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	
	artwork Art1(9979, 2252);
	Art1.show();
		
	artwork Art2(3398, 2758, "Andrew", "Miller", 75000);
	Art2.show();


	return 0;
}

closed account (48T7M4Gy)
Where is the missing parameter ( a string) i.e the title in line 12 of main?
@kemort thanks for pointing that out. It works like a charm now. Now I'm having an issue with another portion of the code

Here is what is supposed to happen:

• A bid has been made on artwork 9979. Use the make_bid member function to bid $60,000. Display a message indicating whether or not the bid was successful. If the bid is successful, show the updated object reflecting the updated current bid.

I'm having a moment and can't remember how to use the make_bid member function to do this.

Here is my code for this portion

1
2

	artwork Art4(0, 9979, "", "", "", 0)


Here is the make_bid code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

bool artwork::make_bid(double aBid, int bidID)
{
	bool found = true;
	if(aBid >= minBid && aBid > currentHighBid)
	{
		currentHighBid = aBid;
		bidderID = bidID;
		found = true;
		return found;
	}
	else
	{ 
		found = false;
		return found;
	}
}
closed account (48T7M4Gy)
I thought I had a sample of how to handle that and expand on the 'sequence diagram' stuff I wrote. It appears to have disappeared. Stand by.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	Artwork picasso(2, 207, "Bob", "Smith");
	cout << picasso.show() << endl;

	double amount = 109.80;
	
	picasso.make_bid(amount, 1234);
	cout << picasso.getBidderID() << " has bid $" << amount << endl;

	if (picasso.make_bid(201.50, 2047) )
		cout << "Bingo " << picasso.getBidderID() << " you're now the highest bidder\n";
	else
		cout << "Sorry " << picasso.getBidderID() << " you bid is invalid\n";

	return 0;
}


You'll probably need to (easily) adapt it a bit but the essence is there.

bool found could be a more meaningful name don't you think. To me it implies something has been lost or found in a search ...
closed account (48T7M4Gy)
1
2
3
4
5
6
7
bool found;
/*
...

*/
found = true;
return found;


You can overcome my naming criticism by :
1
2
3
4
5
6
7
bool found;
/*
...

*/
found = true;
return truefound;

I understand what you are saying @kemort. However our instructor just wants us to get the syntax down. I don't agree with how this course is laid out. I'm just out to understand the material. I know we are supposed to name stuff more meaningful and easy to remember. However, it's the way the instructor has it laid out unfortunately. Also, thank you for the suggestion earlier. I will adapt that to my code and update you tomorrow. :)
Pages: 123