Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 04-21-2005, 09:47 PM   #1 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
Question Classes started with C++ classes and now I'm lost...HELP

I'm supposed to make my first C++ Program using classes and I've barely learned how to use functions.....I've went from having to make one file (.cpp) to having to make THREE (.cpp .cpp .h)

If anyone can point me in the right direction with how to do this I'd appreciate it. I don't think what the instructor is teaching us is sinking in very well (I take these classes online, not in a classroom, maybe that's the problem...or I'm just not getting it...probably more likely)

This is the current assignment, I don't expect anyone to do it for me just need some idea if the code I've written below is on the right track....please don't add anymore complexity then needed as I'm not even comprehending what I'm using already. lol

Quote:
THE ASSIGNMENT:

Project 1: Clothing
The class Clothing represents a washable article of clothing. Implement the class Clothing so
that each instance of Clothing has a string kind and two boolean values which mark whether
the article is clean or dirty, wet or dry. As the sample driver shows, the wash( ), dry( )
and wear( ) methods of Clothing cannot be called twice or more times in succession. Read
the driver code and sample output carefully to understand the ordering of the method calls and
the appropriate output statements your class should generate.
The point behind this assignment is to realize that not all methods are created equal. In fact,
very often, there is a logical sequence to order in which certain methods can be called. It is the
class itself that enforces this ordering. Note the various errors that are printed when driver code
doesn't use the Clothing class correctly, like when it tries to dry clothes that are not wet, or
when it tries to re-wash clothing that has already been washed and is waiting to be dried. As
we learn more complex classes, this kind of sequence to the way methods are called is quite
typical.
Following the class diagrams shown below, implement the Clothing class. Embed your class in
a suitable test program that provides the sample dialogue shown below. You may choose to
create any number of additional methods, but you are required to implement all of the public
methods shown below. You are free to ignore the private parts of the class I suggest below.

Implementation Details

Sample CLASS
(the Clothing Class to make?)

Clothing
Clothing( string kind );
void wash( );
void dry( );
void wear( );
bool isClean( );
bool isWet( );
bool isDirty( );
String my_Kind;
bool my_isClean;
bool my_isWet;



SAMPLE DRIVER
Clothing shirt( “t-shirt” );
shirt.wear();
if (shirt.isDirty()) {
shirt.wash();
if (shirt.isWet()) {
shirt.dry();
}
else {
cout << “shirt should be
wet!” << endl;
}
}
shirt.wear();
shirt.dry();
shirt.wash();
shirt.wash();
shirt.dry();
shirt.dry();



SAMPLE OUTPUT
Wearing this t-shirt
Washing this t-shirt
Drying this t-shirt
Wearing this t-shirt
You cannot dry this t-shirt because it isn’t
wet!
Washing this t-shirt
You cannot wash this t-shirt because it has
already been washed and needs to be dried!
Drying this t-shirt
You cannot dry this t-shirt because it has
already been dried and needs to be worn!
My work so far (it doesn't do anything yet...)....probably totally messed up anyways....

MY CLOTHING.H file

Code:
#ifndef CLOTHING_H
#define CLOTHING_H
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;


class Clothing {
public:
	Clothing ( );
	Clothing(string Kind);
	string my_Kind( );
	void wash( );
	void setWash( );
	void dry( );
	void setDry( );
	void wear( );
	void getWear( );
	bool isClean( );
	bool isWet( );
	bool isDirty( );


private:
	bool my_isClean;
	bool my_isWet;

};

#endif


MY CLOTHING.CPP file

Code:
#include <iostream>
#include <cstdlib>
#include <string>

#include "Clothing.h"

using namespace std;



Clothing::Clothing() {


}







void Clothing::wear( ) {

}

void Clothing::wash( ) {


}

void Clothing::dry( ) {


}

void Clothing::getWear( ){


}
void Clothing::setWash( ) {


}

void Clothing::setDry( ) {


}


bool isClean( ){

}

bool isWet( ){

}

bool isDirty( ){

}

bool my_isClean( ){{

}


}

MY DRIVER FILE (driver.cpp)

Code:
#include <iostream>
#include <cstdlib>
#include <string>

#include "Clothing.h"

using namespace std;

int main()
{
string shirt;
string my_Kind;

cout << "Wearing this t-shirt\n";
cout << "Washing this t-shirt\n";
cout << "Drying this t-shirt\n";
cout << "Wearing this t-shirt\n";
cout << "You cannot dry this t-shirt because it isn’t wet!\n";
cout << "Washing this t-shirt\n";
cout << "You cannot wash this t-shirt because it has\n";
cout << "already been washed and needs to be dried!\n";
cout << "Drying this t-shirt\n";
cout << "You cannot dry this t-shirt because it has\n";
cout << "already been dried and needs to be worn!\n";

return 0;

}
I know I've practically done nothing, but I'm not sure how to progress...help anyone? Thanks in advance.
cleverest is offline   Reply With Quote
Old 04-21-2005, 11:09 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,692
redhead is on a distinguished road
Quote:
shirt.wear();
shirt.dry();
shirt.wash();
shirt.wash();
shirt.dry();
shirt.dry();
SAMPLE OUTPUT
Wearing this t-shirt
Washing this t-shirt
Drying this t-shirt
Wearing this t-shirt
You cannot dry this t-shirt because it isn’t
wet!
Washing this t-shirt
You cannot wash this t-shirt because it has
already been washed and needs to be dried!
Drying this t-shirt
You cannot dry this t-shirt because it has
already been dried and needs to be worn!
These are the crucial parts, telling you what your functions must do, when combining them in a small virtual run of the program it makes more sence. The order in which the functions are called is crucial in this program, which is what the output tries to tell you.
shirt.wash(); -> "Washing this t-shirt"
shirt.wash(); -> "You cannot wash this t-shirt because it has already been washed and needs to be dried!"
shirt.dry(); -> "Drying this t-shirt"
shirt.dry(); -> "You cannot dry this t-shirt because it has already been dried and needs to be worn!"
shirt.wear(); -> "Wearing this t-shirt"
shirt.dry(); -> "You cannot dry this t-shirt because it isn’t
wet!"

This means, when you wear a Clothing item, it checks the washed and dried boolean, if they are true, the washed boolean will be set to false, and you wear the item, if the washed is false, it needs to be washed, if the dried is false, it needs to be dried.

When you wash a Clothing item, it sees if the washed boolean is false, if it is, set it to true and set the dried boolean to false and just wash it, if not tell the user theres no need to wash it again.

The dry function, sees if the dried boolean is set to false and washed boolean is true, is this the case, set dried to true and dry the item, if washed is false, tell the thing has been worn and it needs to be washed. Else tell theres no need to dry the item.

Does this make it more clear how these three functions work together ?

For the constructor, you just need to set washed and dried to false, and if theres a lable parsed to the constructor use this as the string name of the item, somethign like this:
Code:
class Clothing {
public:
	Clothing ( );
	Clothing(string Kind);
	void wash( );
	void dry( );
	void wear( );
private:
	bool dried;
	bool washed;
        string kind;
};
Code:
Clothing::Clothing() {
    cout << "It is impossible to create a Clothing item, which isn't named anything" << endl;
}
Clothing::Clothing(string Kind) {
    kind = Kind;
    washed = false;
    dried = false;
}
void Clothing::wear( ) {
    if(washed && dried)
    {
        washed = false;
        cout << "Wearing this " << kind << endl;
    }
    else
    {
        cout << "This " << kind;
        if(washed)
            cout <<" has been washed and needs to be dried!" << endl;
        else
            cout << " has been worn and needs to be washed" << endl;
}
// and so forth
Code:
int main()
{
    Clothing item ("t-shirt");
    item.wear();
    item.wash();
    item.dry();
    // and so forth
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-22-2005, 01:47 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
This is a nice one:
Code:
Clothing::Clothing() {
    cout << "It is impossible to create a Clothing item, which isn't named anything" << endl;
}
We might not be interested in such an object at all. Let's use a trick:
Declare the default constructor private. And don't implement it. so only:
Code:
class Clothing
{
public:
   //...
private:
   Clothing();
};

int main()
{
   Clothing MyClothes; //illegal
}
__________________
Valmont is offline   Reply With Quote
Old 04-22-2005, 01:58 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
I took another look

Quote:
Sample CLASS
(the Clothing Class to make?)

Clothing
Clothing( string kind );
void wash( );
void dry( );
void wear( );
bool isClean( );
bool isWet( );
bool isDirty( );
String my_Kind;
bool my_isClean;
bool my_isWet;
Did the teacher tell you that? If so then once again it proves half of them don't know what they are talking about.

Let me please explain.
This class is able to wash. And to dry. And to wear.
Think about it. Look at your socks. Do they know how to wash themselves? You see them walking to the washing machine? Do they make you wearing them? How do they do that?
Quote:
Wear me or I'll make me smelly!
Do they make themselves dirty? How?

This teacher has interesting clothes man .

In real life, you'll need a class Clothes which only hold their state:
- Am I clean/dirty?
- Am I dry/wet?
- Am I in a washing machine?
- Am I in a dryer?
- Am I being worn?
- Am I stored on a file?
- Can I be used as a fire extinguisher in case of a an emergency?

The last one is important to show that a class Clothes doesn't have to know why it exists in the first place! This is Object Orientation.
And in real life, you'll create a class WashingMachine:
- Wash()
- HalfDry()
- Notify(Event* event) //Washing ready, HalfDryReady, TimerDone etc.
- DoorIsOpen() // safety!
- DoorIsLocked //So door is closed AND locked. Safety, machine runs!
- HasWeight() //No clothes in it burns the motor!

See what I mean?
__________________
Valmont is offline   Reply With Quote
Old 04-22-2005, 06:08 AM   #5 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 188
cheawick is on a distinguished road
Hmm... Kudos to redhead, but I think Valmont has the full point here. As I have much problem with classes and pointers, the rethink approach solidly applies to the metaphysical aspect of object oriented programming. Gah!
FDISK /MBR!
And let that be a lesson to ya!
FORMAT C:\

Goodbye active logical drive.

Too bad we don't have a game programmers forum here, discussions could get very interesting. Then again, that would just mean more "Do my homework hounds" would pop up.

Geez. Um. Brain dump. 15 hour shifts are not good for the mind. Sorry, but trust Valmont and rethink the REAL properties of your arguments.
cheawick is offline   Reply With Quote
Old 04-22-2005, 08:22 AM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
It doesn't work like that cheawick. Redhead sticks to the assignment. I don't .
__________________
Valmont is offline   Reply With Quote
Old 04-22-2005, 12:49 PM   #7 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
I think you are all awesome and hilarious (when you attempt being funny that is) and I think that seeing things from multiple angles is the only thing that's getting me through some of these assignments....so I appreciate all the diversity in your explanations... but as long as you guys can remember that I'm pretty much C++ retarted and so please try NOT complicate the code for me... that's REALLY appreciated when possible...baby steps....I'll tackle it tonight and see what I can do.

Last edited by cleverest; 04-22-2005 at 01:13 PM.
cleverest is offline   Reply With Quote
Old 04-22-2005, 12:51 PM   #8 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
Thumbs up

valmont, I'm just curious how long it took you to get to the level of EASE that you appear to have when helping others with their C++ code and problems? How many years until you were more or less comfortable with it?

Last edited by Valmont; 04-24-2005 at 08:16 AM.
cleverest is offline   Reply With Quote
Old 04-22-2005, 12:53 PM   #9 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
Nice....thanks redhead, I appreciate the tips...it does make better sense, but I'll know if I can figure out tonight....
cleverest is offline   Reply With Quote
Old 04-22-2005, 01:08 PM   #10 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
Quote:
Originally Posted by Valmont
I took another look


Did the teacher tell you that? If so then once again it proves half of them don't know what they are talking about.
Yeah he gave us all that I typed above in the original post (top 3 codings) to work from.....Although I'm the confused person that typed the: (the Clothing Class to make?) underneath the CLOTHING CLASS header but he is the one that gave us all the variables, functions, and the original description in the first post, etc....

Thanks.
cleverest is offline   Reply With Quote
Old 04-22-2005, 08:47 PM   #11 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 188
cheawick is on a distinguished road
Heh... Sorry about that. But at least you got a good laugh. Getting only three hours of sleep a day leaves me kinda punch drunk. I actually managed to sneak in four hours of rest this time though. The freaky part is when you wake up in a daze wearing a gas mask and chemical suit as your buddy keeps hitting you on yer helmet with his canteen. You can imagine the moments of utter stupification as your brain is saying- "Dude! What the hell is going on here!"

Oops, I digressed again. Guess I should have tried for five hours of sleep.

Lovely... Now I forgot what the heck I was going to say originally. Something about headers I think. Blast. Ah well, I won't delete this as someone might get a kick outta it.

Brain farts FOREVER!!!
cheawick is offline   Reply With Quote
Old 04-24-2005, 08:16 AM   #12 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Quote:
Originally Posted by cleverest
valmont, I'm just curious how long it took you to get to the level of EASE that you appear to have when helping others with their C++ code and problems? How many years until you were more or less comfortable with it?
I missed this reply. I am sorry.

Coding wise, it took me 3 weeks.
However, I do have certain advantages:
I have my BSc of Electrical Engineering. I am certified and well trained in formal construction and verification, and I have the needed math skills in general thanks to Hewlett Packard.
So algorithms-wise, there aren't that many obstacles for me.

As it comes to design, and a minor amount of analisys (not my job), it comes with time, and experiance. More important is having a vision. Every person who would like to perform well, would like to have something to hold on to. I was looking actively for long term insights. And I always failed. And then one day, I suddenly truly saw it. My vision is part of my signature. That makes things a whole lot easier .

But I have many things to improve. To stay with this forum, without comments from the students, I can't do squat. But I am sure I should have done things here and there better. True that!
__________________
Valmont is offline   Reply With Quote
Old 04-24-2005, 08:21 AM   #13 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Quote:
Originally Posted by cleverest
Yeah he gave us all that I typed above in the original post (top 3 codings) to work from.....Although I'm the confused person that typed the: (the Clothing Class to make?) underneath the CLOTHING CLASS header but he is the one that gave us all the variables, functions, and the original description in the first post, etc....

Thanks.
Looking at the driver he provided, he wants you to create class Cloth that can wash itself and such.
__________________
Valmont is offline   Reply With Quote
Old 04-24-2005, 02:19 PM   #14 (permalink)
rogue
Registered User
 
Join Date: Apr 2005
Posts: 9
rogue is on a distinguished road
I can't help you but i just wanted to say I'm as lost as you Thanks god for valmont and redhead. They are really great to help newbies like us. I understand this code more after just reading 1 post from redhead&valmont then i can from myprofessor.
~VALerie
rogue is offline   Reply With Quote
Old 04-24-2005, 03:01 PM   #15 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Basically, redhead takes care of C, while I do C++. One thing is sure, I can't help you with C.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 09:10 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting