none
class problem

    Question

  • suppose I have cin 10 datas
    I want to transfer these 10 datas  to a private variable(user) of a class.
    and then use the class's public function to treat the 10 datas.
    can U teach me how to do it?
    thank you
    Thursday, November 19, 2009 5:37 AM

Answers

  • Example:

    class CMyClass
    {
    private:
    int nValue;
    public:
    CMyClass() { nValue = 0; }
    void ValueSetter(int nValue_param) { nValue = nValue_param; } // For value setting
    int ValueGetter() { return nValue; } // For value getting
    };

    ...

    CMyClass *mc = new CMyClass();

    int Value = 0;
    cin >> Value;

    mc -> ValueSetter(Value); // Setting

    cout << "Value " << mc -> ValueGetter(); // Getting

    Try to read some books about C++ programming, as I've already said.
    • Marked as answer by Nancy Shao Wednesday, November 25, 2009 8:30 AM
    Thursday, November 19, 2009 8:37 AM

All replies

  • Example:

    class CMyClass
    {
    private:
    int nValue;
    public:
    CMyClass() { nValue = 0; }
    void ValueSetter(int nValue_param) { nValue = nValue_param; } // For value setting
    int ValueGetter() { return nValue; } // For value getting
    };

    ...

    CMyClass *mc = new CMyClass();

    int Value = 0;
    cin >> Value;

    mc -> ValueSetter(Value); // Setting

    cout << "Value " << mc -> ValueGetter(); // Getting

    Try to read some books about C++ programming, as I've already said.
    • Marked as answer by Nancy Shao Wednesday, November 25, 2009 8:30 AM
    Thursday, November 19, 2009 8:37 AM
  • Hi Harry,

    I have wrote a sample too, please refer to it:

    #include <iostream>
    using namespace std;
    
    class MyClass
    {
    public:
    	MyClass()
    	{
    		//You can intial your data here...
    	}
             void GetNumbers()
    	{
    		cout<<"The numbers are"<<endl;
    		for(int i=0;i<10;i++) cout<<number[i]<<endl;
    	}
    	void SetNumbers()
    	{   cout<<"Input ten numbers:"<<endl;
    		for(int i=0;i<10;i++) cin>>number[i];
    	}
    private:
    	int number[10];
    };
    
    int main( void )
    {
      	MyClass *mc= new MyClass();
    	mc->SetNumbers();
    	mc->GetNumbers();
    }


    And as Nikita's suggestion, you'd better read more books about C++ programming. I would suggest you some books here:

    C++ Primer
    Effective C++

    For Visual C++, you can start from Ivor Horton's Beginning Visual C++ 2008.

    Best Regards,
    Nancy


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, November 23, 2009 10:51 AM