473,473 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using class functions as a struct object

Hello,

I'm having a little trouble here and I'm hoping that somebody might be
able to help me out (win32 console program). First off, I know that I
can use class function inside of my struct as a struct object...but my
issue that I'm having is that my class function is set up so that it
sets 3 variables...

Example of my class object (has the getter(), and setter() functions
below this, but I won't display that here):

classExample(int record, string word = "", string def = "")
{
strWord = word;
strDef = def;
recordNum = record;
}

The way that I originally had it working was that I could just
implement it in my main source file like:

classExample wordOne(1, "Word", "Definition");

and then print it off by using my print function that is in my class by
doing this:

wordOne.printWordDef();
The trouble I'm having now is that I want to be able to use a struct to
contain classExamaple wordOne(); and allow the user to input his own
word and definition and have it automatically assign it a record
number, so they can add their own words and definitions to the program.
I'm not sure how I could go about doing this because every way that
I've tried doesn't seem to be working correctly. Any help would be
appreciated. Thanks in advance!

Jan 25 '07 #1
2 1817
* jo*****@gpinteractive.com:
>
I'm having a little trouble here and I'm hoping that somebody might be
able to help me out (win32 console program). First off, I know that I
can use class function inside of my struct as a struct object...
?

but my
issue that I'm having is that my class function is set up so that it
sets 3 variables...
It seems that by "class function" you here mean "constructor".

Example of my class object (has the getter(), and setter() functions
below this, but I won't display that here):

classExample(int record, string word = "", string def = "")
{
strWord = word;
strDef = def;
recordNum = record;
}
OK, now you call the constructor a "class object".

It's a "constructor".

The way that I originally had it working was that I could just
implement it in my main source file like:

classExample wordOne(1, "Word", "Definition");
It seems that by "implement" you here mean "instantiate".

and then print it off by using my print function that is in my class by
doing this:

wordOne.printWordDef();
The trouble I'm having now is that I want to be able to use a struct to
contain classExamaple wordOne(); and allow the user to input his own
word and definition and have it automatically assign it a record
number, so they can add their own words and definitions to the program.
You can keep a record count in a 'static' class data member, and use
that as record number.

Increment it every time you add a record.

I'm not sure how I could go about doing this because every way that
I've tried doesn't seem to be working correctly. Any help would be
appreciated. Thanks in advance!
See the FAQ about how to post a question about code that doesn't work.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 25 '07 #2
<jo*****@gpinteractive.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
Hello,

I'm having a little trouble here and I'm hoping that somebody might be
able to help me out (win32 console program). First off, I know that I
can use class function inside of my struct as a struct object...but my
issue that I'm having is that my class function is set up so that it
sets 3 variables...

Example of my class object (has the getter(), and setter() functions
below this, but I won't display that here):

classExample(int record, string word = "", string def = "")
{
strWord = word;
strDef = def;
recordNum = record;
}
This is a class constructor taking 3 parameters, 2 optional.
The way that I originally had it working was that I could just
implement it in my main source file like:

classExample wordOne(1, "Word", "Definition");

and then print it off by using my print function that is in my class by
doing this:

wordOne.printWordDef();
The trouble I'm having now is that I want to be able to use a struct to
contain classExamaple wordOne(); and allow the user to input his own
word and definition and have it automatically assign it a record
number, so they can add their own words and definitions to the program.
I'm not sure how I could go about doing this because every way that
I've tried doesn't seem to be working correctly. Any help would be
appreciated. Thanks in advance!
There are a few ways, but the most workable will be for you to give more
constructors. Lets look at one way to do this (with a little reformating of
your construtor). This is a little code to show one way. Look at the two
different constructors and how they assign the recrodNum differently.

#include <iostream>
#include <string>

class classExample
{
public:
// This does the same thing as yours, except
// uses initalizion list instead of assignemnt.
classExample(int record, std::string word = "", std::string def = ""):
strWord( word ), strDef( def ), recordNum( record ) {}
// Now lets give an alternate.
classExample( std::string word = "", std::string def = "" ): strWord(
word ), strDef( def), recordNum( RecNum++ ) {}
void printWordDef() { std::cout << "ID: " << recordNum << " Word: " <<
strWord << " Def: " << strDef << "\n"; }
private:
int recordNum;
std::string strWord;
std::string strDef;
static int RecNum;
};

int classExample::RecNum = 100000;

int main()
{
classExample wordOne( 1, "Car", "Noun" );
classExample wordTwo( 2, "Fast", "Adjective" );

std::string Word;
std::cout << "Type a word: ";
std::cin >Word;

std::string Definition;
std::cout << "Type it's defintion: ";
std::cin >Definition;

classExample wordThree( Word, Definition );

std::cout << "Type another word: ";
std::cin >Word;

std::cout << "Type another defintion: ";
std::cin >Definition;

classExample wordFour( Word, Definition );

wordOne.printWordDef();
wordTwo.printWordDef();
wordThree.printWordDef();
wordFour.printWordDef();

std::string wait;
std::getline( std::cin, wait );
std::getline( std::cin, wait );
}

Output (and intput):

Type a word: Car
Type it's defintion: Vehicle
Type another word: Television
Type another defintion: Object
ID: 1 Word: Car Def: Noun
ID: 2 Word: Fast Def: Adjective
ID: 100000 Word: Car Def: Vehicle
ID: 100001 Word: Television Def: Object

Note: I started the auto numing at 100,000 because I've always found it
easier to seperate my id's from the users. You may have some other scheme.
Also, you can set the RecNum in the constructor by maybe reading from a file
or whatever. In that case I would initialize it to -1, and if it's -1 then
set it to something in the constructor. This is an excercise for the
reader.
Jan 25 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
0
by: Robert Potthast | last post by:
Hello, I want to make my garbage collector more safe. To make it more safe I need to know if an object has been allocated on the stack or on the heap using the operator new. My garbage collector...
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
15
by: bugzilla | last post by:
hi,all, I have a C++ program need to convert to c language to be used in a emabedded system. the problem is that the original code was writtern in C++ language with Parent class and some child...
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
5
by: GCRhoads | last post by:
I have some templated functions and I want to write a call wrapper class for it. I also want to pass the function object from this class to some functions that will then call the function...
11
by: dolphin | last post by:
Hi All! I have a question that how to call a function just using a string. For example There is a .cpp file named a.cpp.There are some functions::fun1() fun2() fun3(). I have another fucntion...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.