Jump to content

Classes?


CoolMaster

Recommended Posts

SO we've been assigned a new lab report. about classes, and boy is it confusing, basically i have to take my old lab which ill upload in a sec

and convert it to a class. Now I understand what a class is supposed to do. but not how you convert it to a class? can anyone explain kinda what syntax u have to convert it too?

 

 

 

/*Matt Smith
Lab 1
Organizing Vinyl Records and Comparing
Section 2
9-11-2007
*/


#include <iostream>
#include <string>

using namespace std;
#include "vinylRecord.h"
void printRecord(vinylRecord R);
vinylRecord readRecord();
void ModifyPrice(vinylRecord &R,float percent);
int compareRecords(vinylRecord temp1,vinylRecord temp2,int num);





int main()
{
vinylRecord temp1, temp2;

temp1 = readRecord();
cout<<endl<<endl;
temp2 = readRecord();

printRecord(temp1);
cout<<endl<<endl;
printRecord(temp2);

cout<<"\nCompare by purchase date: "<<compareRecords(temp1, temp2, 1);
cout<<"\nCompare by purchase price: "<<compareRecords(temp1, temp2, 2);
cout<<"\nCompare by condition: "<<compareRecords(temp1, temp2, 3);
cout<<"\nCompare by sale price: "<<compareRecords(temp1, temp2, 4);

cout<<"\nReducing price of record 1 by 50%";
ModifyPrice(temp1, -50);
cout<<"\nIncreasing price of record 2 by 50%" << '\n' << '\n';
ModifyPrice(temp2, 50);

printRecord(temp1);
cout<<endl<<endl;
printRecord(temp2);

return 0;
}














vinylRecord readRecord()
{
vinylRecord temprec;
cout << '\n';
cout << "--------------------Entering Data--------------------";
cout << '\n';
cout << "Please Enter a Nine-Digit ID for This Record: ";
cin >> temprec.ID;
cout << "Please Enter the Purchase Date for This Record (MM DD YYYY): ";
cin >> temprec.purmonth >> temprec.purday >> temprec.puryear;
cout << "Please Enter the Purchase Price for This Record: ";
cin >> temprec.price;
cout << "Please Enter the Band/Artist Name: ";
cin.ignore(500, '\n');
getline  (cin,temprec.name);
cout << "Please Enter the Album Name: ";

getline  (cin,temprec.album);
cout << "Please Enter the Album Release Date (MM YYYY): ";
cin >> temprec.relmonth >> temprec.relyear;
cout << "Please Enter a Short Description of the Album Cover: ";
cin.ignore(500, '\n');
getline  (cin,temprec.talk);
cout << '\n' << "Please Select one of the Following Condition Ratings for This Record: " << '\n';
cout << "		0-unacceptable  1-poor  2-average	   3-good " << '\n';
cout << "		4-very good	 5-mint  6-unopened" << '\n';
cin >> temprec.rating;
cout << "Please Enter the Sale Price for This Record: ";
cin >> temprec.saleprice;
cout << '\n';
cout << "-----------------------------------------------------";
cout << '\n';
cout << '\n';

return temprec;
}


void printRecord(vinylRecord R)
{
cout << "--------------------Printing Data--------------------" << '\n';

cout << "ID: " <<R.ID;
cout << '\n';
cout << "Purchase Date: " <<R.purmonth<< " " << R.purday<< " " << R.puryear;
cout << '\n';
cout << "Purchase Price: " <<R.price;
cout << '\n';
cout << "Band/Artist: "<<R.name;
cout << '\n';
cout << "Album: "<<R.album;
cout << '\n';
cout << "Release Date: "<<R.relmonth<< " " << R.relyear;
cout << '\n';
cout << "Description of the Album Cover: " <<R.talk;
cout << '\n';
cout << "Condition Rating: ";
if(R.rating == 0)
{
cout << "unacceptable ";
}
if(R.rating == 1)
{
cout << "poor ";
}
if(R.rating == 2)
{
cout << "average ";
}
if(R.rating == 3)
{
cout << "good ";
}
if(R.rating == 4)
{
cout << "very good ";
}
if(R.rating == 5)
{
cout << "mint ";
}
if(R.rating == 6)
{
cout << "unopened ";
}



cout << '\n';
cout << "Sale Price: "<<R.saleprice;
cout << '\n';
cout << "-----------------------------------------------------";
cout << '\n';


}



void ModifyPrice(vinylRecord &R, float percent)

{
R.saleprice = R.saleprice + (percent/100)*R.saleprice;


}

int compareRecords(vinylRecord temp1,vinylRecord temp2,int num)

{


 if(num==1)
{
	if(temp1.puryear<temp2.puryear)
	{
	return -1;
	}

	if(temp1.puryear>temp2.puryear)
	{
	return 1;
	}

	if (temp1.puryear==temp2.puryear && temp1.purmonth<temp2.purmonth)
	{
	return -1;
	}

	if (temp1.puryear==temp2.puryear && temp1.purmonth>temp2.purmonth)
	{
	return 1;
	}

	if (temp1.puryear==temp2.puryear && temp1.purmonth==temp2.purmonth && temp1.purday < temp2.purday)
	{
	return -1;
	}

	if (temp1.puryear==temp2.puryear && temp1.purmonth==temp2.purmonth && temp1.purday > temp2.purday)
	{
	return 1;
	}

	if(temp1.puryear==temp2.price && temp1.purmonth==temp2.purmonth && temp1.purday==temp2.purday)
	{
	return 0;
	}


}


if(num==2)
{
	if(temp1.price>temp2.price)
	{
		return -1;
	}
	if(temp1.price==temp2.price)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

 if(num==4)
{
	if(temp1.saleprice>temp2.saleprice)
	{
		return 1;
	}
	if(temp1.saleprice==temp2.saleprice)
	{
		return 0;
	}
	else
	{
		return -1;
	}
}





  if(num==3)
{
	if(temp1.rating>temp2.rating)
	{
		return 1;
	}
	if(temp1.rating==temp2.rating)
	{
		return 0;
	}
	else
	{
		return -1;
	}
}


cout << '\n' << '\n';

}

 

and header file

 

struct vinylRecord{
int ID,purmonth,purday,puryear,relmonth,relday,relyear,rating; 
float price,saleprice;
string name,album,talk;
};

 

 

their's my code, it's like a cd record store program, it's pretty kewl i guess

we have to add 2 more things to it, but thats not what i need help with, I mean how exactly would u make that a class?

i looked at my teachers powerpoints again, but the example he uses just doesnt explain it clearly (he's a grad student so.....ya know)

 

but any help would be appreciative. im sure converting it shouldnt be too bad once I figure out how

Share this post


Link to post
Share on other sites

  • Replies 50
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

It seems like if you really understood classes then this assignment would be pretty cake. I don't mean to be condescending, but basically all you're doing is taking the work you already did and making it into a class object. Surely your book has examples of how to declare a class...

 

It's really straightforward:

 

class RecordStore {

int ID,purmonth,purday,puryear,relmonth,relday,relyear,rating;
float price,saleprice;
string name,album,talk;

vinylRecord readRecord();
void printRecord(vinylRecord R);
void ModifyPrice(vinylRecord &R, float percent);

} // end RecordStore class




vinylRecord RecordStore::readRecord(){

//readRecord function code goes here

}


vinylRecord RecordStore::printRecord(){

//printRecord function code goes here

}


vinylRecord RecordStore::modifyRecord(){

//modifyRecord function code goes here

}

 

Then it's just a question of changing your main driver code to call the functions for the class object, which should also be pretty easy.

Share this post


Link to post
Share on other sites

It would be cake if we had a BOOK, seriously and Im being dead serious....we do not have a book, nor a recommended book. he gives us powerpoints......and he's a grad student so needless to say he doesnt know what he's doing. and trust me Im not the only one having questions, all of us got together and none of us understood it really. but thank you for the code. i figured it'd be easy once I understood setting it up

Share this post


Link to post
Share on other sites

It would be cake if we had a BOOK, seriously and Im being dead serious....we do not have a book, nor a recommended book. he gives us powerpoints......and he's a grad student so needless to say he doesnt know what he's doing. and trust me Im not the only one having questions, all of us got together and none of us understood it really. but thank you for the code. i figured it'd be easy once I understood setting it up

Then you, my friend, need to work on your google skills. :P

 

I'll be honest. I didn't know the info I just posted. I've coded too many languages now, and I don't recall the exact syntax for class declarations in 10 different languages. That's just not how my brain works :)

 

But I typed in "C++ class declaration" into google, and the first result yielded the syntax you see in my post. Find some websites with some examples. If you're like most people, an example is the best way to learn. It is for me! :lol:

 

Google is better than a book anyways. Books are EXPENSIVE! Google is free :)

 

when you say "read record function code goes here" im assuming u meant the original code that was from my original program?

Yes, the actual code that makes those functions work. That's the stuff you've already written, so you basically just cut/paste it into the new location. You will need to make minor changes now since the variables are not in a struct anymore.

 

After all, a class is really just an object that holds data variables and functions. So you just take all the old declarations you already had, and put them inside a class declaration. The class just acts like a shell to hold all the variables and functions that make that object do what you want it to do.

 

and is what you listed above go in the header file, or below the main?

Actually, it shouldn't matter. It all gets compiled the same way anyways. So really, either will work.

 

However, the standard is generally to put a class file in its own header. Basically, you take ALL the info that makes that class work, and you put it in a header. In a small project like this, it doesn't really matter. But the reason for doing that is that if you develop something that's really useful to you, you'll use it over and over. So the idea is that you can just save that header, and include it in any project you make, and that class is ready to go.

Share this post


Link to post
Share on other sites

we do not have a book, nor a recommended book.

just buy a book... here's one for $16.75: http://www.amazon.com/C-Programming-Absolu...8253&sr=1-2

 

it's C++ for a beginner, no previous C knowledge required, and the code is interesting stuff like simple games...

 

other stuff from Pohl, and Stroustrup, would be good, if you were solid in C already, but it looks like you've only just started...

Share this post


Link to post
Share on other sites

I know c++ fine it's just functions always confused me, and in all honestly our teachers really suck, im one of the many who doesnt get half the stuff our professor explains..

 

and I see the hinting yall are getting at so...ill try and make this the last time

Edited by CoolMaster

Share this post


Link to post
Share on other sites

if you don't even know how to declare a function or create a class, you can't "know C++ fine"... I'd strongly recommend a book, it makes a lot more sense than powerpoint slides and will have loads of examples and better explanations...

 

some teachers are rubbish

Share this post


Link to post
Share on other sites

if you don't even know how to declare a function or create a class, you can't "know C++ fine"...

:withstupid: That's kind of what I was trying to say too. It actually concerns me a bit because if you don't get functions, you can't really get classes. It's like building blocks, and if you don't understand step #1, how can you begin step #2?

 

Either a book, or a good tutorial website. It doesn't matter, but it would REALLY be worth your time to try to get a grip on functions. If you're serious about learning this stuff, you need to really understand that. If you don't, you're going to be playing catch-up for the rest of the semester. I bet a few good examples are all you need to really get it. You're probably just not getting that from the power point junk.

 

and I see the hinting yall are getting at so...ill try and make this the last time

I'm not trying to hint at anything. :) I really don't mind helping. I just want you to know that a lot of this syntax stuff is all over the web. Rather than having to wait for us to reply, a google search can get you what you need right now.

 

Plus, there's certain stuff we just can't help you with here. I'd really like to type you out a few great examples of functions and classes, but I'm just too lazy :) That's a lot of typing. But there's examples out there already, and they're probably better than what I'd give anyways :)

Share this post


Link to post
Share on other sites

if you don't even know how to declare a function or create a class, you can't "know C++ fine"... I'd strongly recommend a book, it makes a lot more sense than powerpoint slides and will have loads of examples and better explanations...

 

some teachers are rubbish

"some teachers are rubbish"

 

too true, you should've seen my cs 115 teacher, if U asked for help she looked at you like you were stupid, and then sent you to one of the non-english speaking ta's........yeah. lol

 

 

I might go to joseph beth bookstore and see what books they have and study up on functions, when people show me them im like OH OK i get it, but then when i try to do it myself im like......help meeeeeeee lol

Edited by CoolMaster

Share this post


Link to post
Share on other sites

It seems like if you really understood classes then this assignment would be pretty cake. I don't mean to be condescending, but basically all you're doing is taking the work you already did and making it into a class object. Surely your book has examples of how to declare a class...

 

It's really straightforward:

 

class RecordStore {

int ID,purmonth,purday,puryear,relmonth,relday,relyear,rating;
float price,saleprice;
string name,album,talk;

vinylRecord readRecord();
void printRecord(vinylRecord R);
void ModifyPrice(vinylRecord &R, float percent);

} // end RecordStore class
vinylRecord RecordStore::readRecord(){

//readRecord function code goes here

}
vinylRecord RecordStore::printRecord(){

//printRecord function code goes here

}
vinylRecord RecordStore::modifyRecord(){

//modifyRecord function code goes here

}

 

Then it's just a question of changing your main driver code to call the functions for the class object, which should also be pretty easy.

 

wait with this would it say vinylRecord is undefined?

since...it's never like declared?

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×
×
  • Create New...