Member Avatar for GreenDay2001

Suppose I have created a class new with arguments.I have declared this as object new1 in the main.
new new1(arguments).
Now what should i do if i want to change the values of new1 object's arguments.

Suppose I have created a class new with arguments.

You can't do this. new is a C++ keyword. You can't create classes or variables with keywords as names.

I have declared this as object new1 in the main.
new new1(arguments). Now what should i do if i want to change the values of new1 object's arguments.

If the data members are public, you can change them directly. If they are private you will have to write set functions that set the data member values.

If you don't understand my reply, go read a C++ tutorial on classes.

Member Avatar for GreenDay2001

i know those keyword concept. this case i used it to explain my prob.

Since you didn't specify whether you understood, I'll try to explain it briefly. Say you have the following code:

class MyClass
{
public:
   int x, y;
   MyClass(int a = 0, int b = 0)
   {
      x = a; y = b;
   }
};

You can change the values with the following:

MyClass* m = new MyClass();
    m->x = 5;
    m->y = 4;

However, if x and y are private, you'll need something like the following:

class MyClass
{
    int x, y;
public:
    MyClass(int a = 0, int b = 0)
    {
        x = a; y = b;
    }
    void setX(int n)
    {
        x = n;
    }
    void setY(int n)
    {
        y = n;
    }
};

    // in code somewhere...
    MyClass* m = new MyClass();
    m->setX(5);
    m->setY(4);
Member Avatar for GreenDay2001

thanx for help, now i have understood and got my answer
.

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.