C++ Declaring A Class  | | |
March 31st, 2003, 07:07 PM
|
#1 (permalink)
| | Junior Member
Join Date: Mar 2003 Location: Florida USA
Posts: 18
|  Hi Everyone
I am trying to write code for class BarrierType, using all eight listed methods.
I know I have to start with: (well I think I do)
void BarrierType: :SetCode(AnsiString NewCode)
but for the life of me I do not know where to start. I wondered if anyone could get me started, please don't say read a book, because I have until my eyes are seeing double. So as usual this is my last port of call. Please Help, I am not taking the p**s or trying to. I have been looking at it for days and still the light has not come on!!! How do I get the ball rolling.
Any help will be appreciated.
Morphios 
Last edited by Morphios : July 22nd, 2003 at 06:34 PM.
|
| |
March 31st, 2003, 08:16 PM
|
#2 (permalink)
| | Junior Member
Join Date: Mar 2003 Location: Florida USA
Posts: 18
|
Can I write the functions for this code like this, or have I completly gone in the wrong direction.  I am having serious problems writing these functions.....Help!! Code: void BarrierType::SetCode(AnsiString NewCode)
{
Code = NewCode;
if NewCode == '4';
NewCode = false;
else
NewCode = true;
}
Once again any help appreciated.
Morphios  |
| |
March 31st, 2003, 11:48 PM
|
#3 (permalink)
| | Member
Join Date: Feb 2002
Posts: 161
| Code: void BarrierType::SetCode(AnsiString NewCode)
{
Code = NewCode;
if NewCode == '4';
NewCode = false;
else
NewCode = true;
} Your if statement is incorrect. You need to use parentheses ( ) , and you need to remove the semicolon at the end. Also, the NewCode == '4' comparison is most likely wrong, although it's impossible to tell for sure without seing the definition of the AnsiString class. Your code should probably look something like this: Code: void BarrierType::SetCode(AnsiString NewCode)
{
Code = NewCode;
if(NewCode == "4")
NewCode = false;
else
NewCode = true;
} Hopefully that should give you some hints as to how to write the functions. |
| |
April 1st, 2003, 12:14 AM
|
#4 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Eastern Shore
Posts: 701
|
Quite a bit goes into writing classes. You have been pretty vague…could you please specify a little better what exactly it is that you need help with?
I assume that the header file was given to you? Furthermore, I assume that you must define all of the functions within a set of given parameters? Do you know that you are going to need a class.h file and a class.cpp file?
Alright, here is a quickie (let us know if you need further help or a specific question):
Using the syntax you provided for us as the .h file, here would be the supplemental .cpp file: Code: #include “BarrierType.h”
BarrierType::BarrierType(void)
{
//insert function code here
//initialization function
}
void BarrierType::BarrierOpen(void)
{
//insert code
}
void BarrierType::BarrierClosed(void)
{
//insert code
}
void BarrierType::SwitchMode(void)
{
//insert code
}
bool BarrierType::Open(void)
{
//insert code
}
char BarrierType::GetMode(void)
{
//insert code
}
bool BarrierType::SetCode(AnsiString NewCode)
{
//insert code
}
bool BarrierType::Correct(AnsiString Attempt)
{
//insert code
} Let us know what it is you need help with.
ILC |
| |
April 1st, 2003, 12:26 AM
|
#5 (permalink)
| | Senior Member
Join Date: May 2002 Location: Rocky Mountain High
Posts: 613
|
Your header file looks great. Note: the SetCode and Correct functions do not REQUIRE you to declare the variable within the class definition, but it doesn't do any harm there either.
Example: "bool SetCode(AnsiString NewCode)" can be put as "bool SetCode(AnsiString)" in the class, as long as you declare that string in the function definition (like in Martee's response).
ILC gave a pretty good cut and dry layout of what your implementation file for the header should look like (that's the .cpp file that does along with the one you gave us).
Your example function def looked like this: Code: void BarrierType::SetCode(AnsiString NewCode)
{
Code = NewCode;
if NewCode == '4';
NewCode = false;
else
NewCode = true;
} One thing that I wanted to point out is that in your header file you protoyped the function SetCode with a Return type of bool, and not void.
If indeed you do define it as bool, then you need to return a boolean value to the function call. Like this: Code: bool BarrierType::SetCode(AnsiString NewCode)
{
// Necessary code in here, including declaration of a
// variable of type bool to return later
return <variable name here>;
} I'm sure you probably know this, but all functions not of type "void" must return a value.  |
| |
April 1st, 2003, 04:29 AM
|
#6 (permalink)
| | Junior Member
Join Date: Mar 2003 Location: Florida USA
Posts: 18
|
Thanks Guys
You have definitly started me off and given my food for thought, I must now go away and write the functions.
I thought C++ was going to be fun?
Thanks again and I'm sure 'I'LL BE BACK!!'.
Morphios |
| |
April 1st, 2003, 09:43 AM
|
#7 (permalink)
| | Junior Member
Join Date: Mar 2003 Location: Florida USA
Posts: 18
|
Hi All
Once again wondered if someone could look at what has been done and tell me if I am going in the right direction, or tell me where I am going wrong. Code:
BarrierType::BarrierType(void)
{
State.BarrierType = true;
Mode.BarrierType = 'P';
Code.BarrierType = '0000';
}
Question 1: Can I do this to initialize Class BarrierType
Question 2: Am I barking up the wrong tree again.
Once again any help greatfully appreciated.
Morphios
Last edited by Morphios : April 1st, 2003 at 10:02 AM.
|
| |
April 1st, 2003, 01:07 PM
|
#8 (permalink)
| | Senior Member
Join Date: May 2002 Location: Rocky Mountain High
Posts: 613
|
One thing to remember: The default constructor (or initialization function as I think you called it) is a member function of the class BarrierType. This means that it has access to the private member variables without use of the dot operator.
Also, that void does not have to be in there
Try this: Code: BarrierType::BarrierType()
{
State = true;
Mode = 'P';
Code = "0000";
} edit~A paramaterized constructor follows the same logic, except you enter paramaters in the parentheses and set your three values equal to them. 
__________________
Talking in numbers doesn't make you smarter.
|
| |
April 1st, 2003, 02:18 PM
|
#9 (permalink)
| | Junior Member
Join Date: Mar 2003 Location: Florida USA
Posts: 18
|
squeech
Thank you, Thank you.
I now understand that you only need to use the dot notation outside the class.
see you all later, only have 7 more methods to write, that should take me till around midnight tomorrow.
Thanks again
Morphios |
| |
April 1st, 2003, 03:11 PM
|
#10 (permalink)
| | Junior Member
Join Date: Mar 2003 Location: Florida USA
Posts: 18
|
Quick question
Can I write the other 7 methods in a similar vain or should I be thinking of if and else statements e.g: Code:
//These methods set the State to open and closed respectively.
void BarrierOpen(void);
{
BarrierOpen = State;
}
void BarrierClosed(void);
{
BarrierClosed = State;
}
I am trying to write the code file but am unsure if the methods need anything harder.
Guidance appreciated
Morphios |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |