In: Computer Science
**** IN C++ *****
1.Given the class alpha and the main function, modify the class alpha so the main function is working properly.
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class alpha
{
private:
int data;
public:
//YOUR CODE
};
////////////////////////////////////////////////////////////////
int main()
{
alpha a1(37);
alpha a2;
a2 = a1;
cout << "\na2=";
a2.display(); //display a2
alpha a3(a1); //invoke copy constructor
cout << "\na3=";
a3.display(); //display a3
alpha a4 = a1;
cout << "\na4=";
a4.display();
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
class alpha
{
private:
int data;
public:
//this is for alpha a2; this object creation if
we remove it will raise an error.It is default constructor.
alpha(){;}
//this if for alpha a1(37); and it is a
parameterized constructor with one parameter as integer type.
alpha(int d){
//assigning value to object.
data=d;
}
//copy constructor. And this is for alpha
a3(a1); and alpha
a4=a1; these two lines.
alpha(const alpha &a){
data=a.data;
}
//display function to display value of objects.
void display(){
//displaying data value of particular object invoked from
main()
cout<<data;
}
};
int main()
{
alpha a1(37); //invoking parameterized constructor
alpha a2; //invoke default constructor
a2=a1; //invoke copy constructor
cout << "\na2=";
a2.display(); //display a2
alpha a3(a1); //invoke copy constructor
cout << "\na3=";
a3.display(); //display a3
alpha a4 = a1; //invoke copy constructor
cout << "\na4=";
a4.display(); //display a4
cout << endl;
return 0;
}
output:-
a2=37
a3=37
a4=37