Output:
Beyonce : 15
Stevie Wonder : 5
Elton John : 5
#include <iostream>
using namespace std;
class Spaceship
{
public:
Spaceship(const string &initialName,int initialSize):name(initialName),size(initialSize)
{ }
void print()
{
cout<<name <<” : “<<size<<endl;
}
string name;
int size;
};
Spaceship functionA(const Spaceship& otherSpaceship)
{
Spaceship newSpaceship(otherSpaceship);
newSpaceship.name = “Stevie Wonder”;
return newSpaceship;
}
int functionB(Spaceship& otherSpaceship)
{
otherSpaceship.size = 5;
return otherSpaceship.size;
}
void functionC(Spaceship *otherSpaceship)
{
otherSpaceship->name = “Beyonce”;
}
int main()
{
Spaceship s1(“Paul McCartney”,15); //argument constructor is invoked
Spaceship s2 = functionA(s1); //call to function sending the object s1 where name of spaceship is set to Stevie Wonder
Spaceship *s3 = new Spaceship(“Elton John”,6);
s3->size = functionB(s2); //set size of s2 object to size of s3 ie 5
functionC(&s1);//set name of s1 to Beyonce
s1.print(); //Beyonce 15 , size remains 15
s2.print(); //Stevie Wonder 5
s3->print();// Elton John 5
return 0;
}