Hey,
I have just switched from C#, and in C# there was a way you could create an item (Example), and set parameters.

Example text = new Example();
Example.parameter = value; //etc

I have searched the internet, and could not make any sense of it.
Any help would be greatly appreciated.
Cheers

C++ does not have properties like C#. You can use methods though.

class Example {
public:
    int parameter() const { return param; }
    void parameter(int value) { param = value; }
private:
    int param;
};
Example text;
text.parameter(value);
cout << text.parameter() << '\n';

C++ does not have properties like C#. You can use methods though.

class Example {
public:
    int parameter() const { return param; }
    void parameter(int value) { param = value; }
private:
    int param;
};
Example text;
text.parameter(value);
cout << text.parameter() << '\n';

Thanks a lot.
Just to check, for an array, I would do Example text[number];
Correct?

Edited 6 Years Ago by bbman: n/a

Yes, to declare an array it would be

const int elementCount = 5
dataType varName[elementCount]

I wrote it this way because the old standard required that elementCount be a constant. Some compilers still enforce this.

Newer compilers are starting to allow variables as the elementCount in lieu of constants. I think it's part of the 0x standard(s).

Edited 6 Years Ago by Fbody: n/a

Hey,
I am a complete newbie at this- where do I put the class?
I tried making a new class- I have a constant list on it. Do I add a new class file for each class, or can I do multiple (like in C#)? (I have an item.h, and an item.cpp; I'm using Visual Studio.)
Cheers for the help.

Edited 6 Years Ago by bbman: n/a

Hey,
Do I add a new class file for each class, or can I do multiple (like in C#)? (I have an item.h, and an item.cpp; I'm using Visual Studio.)
Cheers for the help.

Didn't get you.

It should but I think the Item Class should be nested inside the ItemGroup Class & not be declared inside it's constructor which would just make it local to the constructor.

P.S. Next time kindly post the code here. It will be easier for both of us to check.

Edited 6 Years Ago by nbaztec: n/a

Ahh got it. Sorry about not posting the code; I'll do that next time.
Cheers all for your help, it means a lot.

Generally, you'll have a header file (a *.h file) which contains the class definition.
myClass.h:

#ifndef MYCLASS_H
#define MYCLASS_H

class myClass {
 private:
   int myPrvInt; //declare a private member variable
 public:
   myClass(); //declare constructor method
   ~myClass(); //declare destructor method
   void setMyInt(int); //declare setter method
   int getMyInt(); //declare getter method
};
#endif //MYCLASS_H

Then, you'll have a source file (*.cpp) that contains the implementation(s) of the class' member methods.
myClass.cpp:

#include "myClass.h"

myClass::myClass() { //implement constructor method
  myPrvInt = 0;
}

myClass::~myClass() { //implement destructor method
}

void myClass::setMyInt(int newInt) { //implement setter method
  myPrvInt = newInt;
}

int myClass::getMyInt() { //implement getter method
  return myPrvInt;
}

Then, you will have a main file that starts/runs your program.
main.cpp

#include <iostream>
#include "myClass.h"

using namespace std;

int main() {
  const int elementCount = 5; //declare a constant
  myClass arrayOfMyClassObjects[elementCount]; //declare the array

  for (int currentIndex = 0; currentIndex < elementCount; currentIndex++) { //begin for

    //set the value of arrayOfMyClassObjects[currentIndex].myPrvInt
    arrayOfMyClassObjects[currentIndex].setMyInt(currentIndex);

    //display the value of arrayOfMyClassObjects[currentIndex].myPrvInt
    cout << "The value of arrayOfMyClassObjects[" << currentIndex << "].myPrvInt is: "
         << arrayOfMyClassObjects[currentIndex].getMyInt() << endl;

  } //end for

  return 0;
} //end main()

Hopefully that helps and didn't confuse you more. Pay particular attention to the main.cpp file. I did it as an array to demonstrate for you based on a previous question.

Edited 6 Years Ago by Fbody: n/a

This question has already been answered. Start a new discussion instead.