home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

C++ Declaring A Class

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1935
Discussions: 200,920, Posts: 2,379,084, Members: 246,289
Old March 31st, 2003, 07:07 PM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Mar 2003
Location: Florida USA
Posts: 18
C++ Declaring A Class

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.
Morphios is offline   Reply With Quote
Old 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
Morphios is offline   Reply With Quote
Old 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.
Martee is offline   Reply With Quote
Old April 1st, 2003, 12:14 AM     #4 (permalink)
ILC
Senior Member
 
ILC's Avatar
 
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
ILC is offline   Reply With Quote
Old April 1st, 2003, 12:26 AM     #5 (permalink)
Senior Member
 
squeech's Avatar
 
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.

squeech is offline   Reply With Quote
Old 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
Morphios is offline   Reply With Quote
Old 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.
Morphios is offline   Reply With Quote
Old April 1st, 2003, 01:07 PM     #8 (permalink)
Senior Member
 
squeech's Avatar
 
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.
squeech is offline   Reply With Quote
Old 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
Morphios is offline   Reply With Quote
Old 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
Morphios is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2852)
Obama the Muslim (13)
Why is Khalid Sheikh Mohammed even .. (9)
Is the PSU I received dead? (10)
windows vista security holes (8)
Foreign voltage (10)
Print spooler problem (13)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (9)
Dept. of HS: NSA 'Helped' Develop V.. (15)
A good PSU? (10)
New Computer wont recognize XP disc (7)
Ideal cheap graph card for PC-Gamin.. (15)
EVGA 9800 gtx help with finding a g.. (8)
Recent Discussions
radeon x850xt platinum & shader 3 (0)
The NTDVM CPU has encountered an ille.. (24)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (34)
Wireless speakers for PC? (11)
Print spooler problem (13)
Help getting around port 80 for camer.. (2)
Display shows 3x5 inch in middle of s.. (3)
windows vista security holes (8)
monitor will not turn on at all, (1)
World's largest Monopoly Game using G.. (331)
Foreign voltage (10)
FiOS modem/router interfering with ne.. (7)
Browsers wont load websites (2)
Virus Doctor Popup? (1)
Dept. of HS: NSA 'Helped' Develop Vis.. (15)
Install XP pro and a Vista laptop ?? (9)
EVGA 9800 gtx help with finding a goo.. (8)
Modern Warfare For the PC (32)
Problem with speed step/turbo boost? (1)
Modern Warfare 2: Who Bought It? (61)
SIS 740 and Widescreen (8)
Baffling Problem with my CPU/MoBo's. .. (0)
HIS HD5770 graphic card question (15)
Best file format to play on Windows H.. (0)
PSP Go bought in Japan (0)


All times are GMT -4. The time now is 09:07 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



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