Home > Software design >  what happened in A and B what I commented in the program ? I am really confused about pointers [clos
what happened in A and B what I commented in the program ? I am really confused about pointers [clos

Time:09-29

#include<iostream>
#include<string>

using namespace std;

class Enemy
{
protected :
    int attack_power;
public :
    void SetAttackPower(int a)
    {
        attack_power=a;
    }
};

class Ninja : public Enemy
{
public:
    void attack()
    {
        cout<<"Ninja! - "<<attack_power<<endl;
    }  
};

class Monster:public Enemy
{
public :
    void attack()
    {
        cout<<"Monster "<<attack_power<<endl;
    }
};


int main()
{
    Ninja n;
    Monster m;
    Enemy e1=n;
    Enemy *e2=&m;

    e1.SetAttackPower(20);  //A
    e2->SetAttackPower(80);  //B

    n.attack();
    m.attack();     
}

output:

 Ninja! - 468
 Monster! 80

**Hi I am practicing C and I came across a program regarding polymorphism here I have created two objects named e1 and *e2 and I have assigned with n and &m respectively which I have already created , what happened in the line of code A and B what I commented below. and what is the use of pointers instead of using non-pointers like '.' **

CodePudding user response:

With the following:

Enemy e1=n;

you are declaring e1 as an independent object which is the copy of n. When you later set the value to e1, the value of n remains untouched. What you get in output is the uninitialized value of n.

CodePudding user response:

Let's first have a look at your Enemy base class:

class Enemy
{
protected :
    // this value is unitialized across all Enemy instances 
    // and can then have therefore every value
    int attack_power; 
public :
    void SetAttackPower(int a)
    {
        attack_power=a;
    }
};

attack_power is not initialized on creation and it depends on that it is set from outside. Of course this is possible but many see this as a bad behavior and it can lead to problems, like it is doing it for you.

Now we have a look at your main:

int main()
{
    Ninja n;        // create a ninja object
    Monster m;      // create a monster object
    Enemy e1=n;     // Copy the ninja into a NEW enemy object, e1 now has nothing to do with n anymore!
    Enemy *e2=&m;   // set a pointer to monster and hold it by an enemy pointer

    e1.SetAttackPower(20);  // you use the copied object, to set the attack power
    e2->SetAttackPower(80); // set the attack power through the pointer, which indirectly affects monster m

    n.attack(); // ninjas attacks power is never set, because you made a deep copy
    m.attack(); // monsters attacks power is set to 80 through the pointer     
}

What you see in the output is the uninitialized attack_power of n and the correct 80 of m and e2 which is set through the e2 pointer.

CodePudding user response:

Line Enemy e1=n; makes a copy. When you modify e1, n is not modified. When you modify the pointer e2, it modifies the original m.

Consider this code:

int a = 1;
int b = 2;
int c = a; // Copy
int* d = b; // Pointer

c = 3; // Modifies the copy
*d = 4; // Modifies the original

// Prints 1434
cout << a << b << c << *d;
  • Related