Hello,
I defined a list in a class and want to do a class containing get/set functions. How can I set or get a data struct (list element) that has variables of different type and each variable is taken separably as an input from the user (I do the input part in the main function, right?)
Thanks

Recommended Answers

All 2 Replies

I'm sorry, I'm having trouble understanding exactly what you are asking. If I am presuming correct, you want a data set within the class to be set by something (maybe std::cin function?) and then you can recall it later? You are going to want to make this data set "public." I wrote up a simple example below doing this with a simple example.

#include <iostream>

class Example_Class
{
public:
	Example_Class();
	~Example_Class();
	int x;
};

Example_Class::Example_Class()
	: x(0) // Initialize the variable
{}
Example_Class::~Example_Class(){}

int main()
{
	Example_Class ex;

	// Before the update
	std::cout<< "Before: "<< ex.x << std::endl << std::endl;

	// The update
	ex.x = 1;

	// After the update
	std::cout<< "After: "<< ex.x << std::endl << std::endl << "Please hit enter to exit";
	std::getchar();
	return 0;
}

Regards,
Dennis M.

I'm not sure if this is to what you are referring but a struct can have a constructor just like a class does (the only difference between the two is the default access of the variables, struct everything is public by default and class everything is private):

struct element
{
   int var1;
   char var2;

   element(int varone,char vartwo) : var1(varone),var2(vartwo) {}
   //this uses an initialization list but doesn't have to
};

So in main you can take the input into temporary variables and then pass them all into the constructor when you create your object of type element.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.