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?)

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
	int x;

	: x(0) // Initialize the variable

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";
	return 0;

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.

This article has been dead for over six months. Start a new discussion instead.