Using class functions as a struct object

J

jordanp

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!
 
A

Alf P. Steinbach

* (e-mail address removed):
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...
?


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.
 
J

Jim Langston

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,094
Latest member
IchNar

Latest Threads

Top