Problem Statement: Multiplication of two objects of same class ·1 You are requir
ID: 3619526 • Letter: P
Question
Problem Statement: Multiplication of two objects of same class
·1 You are required to write a class. Class should have an array of 10 integers as its private data member.
Class should have
·1 A default constructor which will initialize all array elements with zero.
·2 Getter and setter functions
·3 An operator overloading function which will overload * operator for this class.
§ You are required to create three objects of the same class.
§ You must assign values to first and second objects through setter functions. You can either take input arrays from user or use hard coded values.
§ Display the values of first and second object through getter functions.
§ Multiply both objects and store the result of multiplication in third object.
§ Multiplication means the 1st element of member array of object1 should be multiplied by 1st element of member array of object2. Similarly, 2nd element should be multiplied with 2nd and so on. In the end, display the result stored in third object.
§ Your program should support the statement like a = b * c, where a, b and c are objects of the same class.
Sample output:
Object 1 :
1
2
3
4
5
6
7
8
9
10
Object 2 :
10
9
8
7
6
5
4
3
2
1
Multiplication of both objects :
10
18
24
28
30
30
28
24
18
10
Explanation / Answer
Hi, Heres the code:- Note: "ALWAYS REMEMBER THAT WHEN AN OBJECT OF A CLASS IS PASSED AS AN ARGUMENT TO A FUNCTION IT IS TREATED LOCAL FOR THAT MEMBER FUNCTION AND IT IS DESTROYED WHEN CONTROL RETURNS TO THE CALLING FUNCTION. #include<iostream.h>class multiply
{
private:
int myarry[10];
public:
//Constructor initializing array for all four objects of class!
multiply()
{
for(int i=0;i<=9;i++)
{
myarry[i]=0;
}
}
//returns values of objects a and b on the screen
void getter(multiply a,multiply b)
{
cout<<"Values for object a are:-"<<endl;
for(int t=0;t<=9;t++)
{
cout<<"Value"<<"."<<t<<" "<<a.myarry[t]<<endl;
}
cout<<"Values for object b are:-"<<endl;
for(int s=0;s<=9;s++)
{
cout<<"Value"<<"."<<s<<" "<<b.myarry[s]<<endl;
}
}
//takes values from user for objects a and b
void setter(multiply x,multiply y,multiply z)
{
cout<<"Enter values in array for object a"<<endl;
for(int m=0;m<=9;m++)
{
cout<<"Value Please?"<<endl;
cin>>x.myarry[m];
}
cout<<"Enter values in array for object b"<<endl;
for(int n=0;n<=9;n++)
{
cout<<"Value Please?"<<endl;
cin>>y.myarry[n];
}
getter(x,y);
calculate(x,y,z);
}
//performs the multiplication
void calculate(multiply a,multiply b,multiply c)
{
for(int bee=0;bee<10;bee++)
{
c.myarry[bee]=a.myarry[bee]*b.myarry[bee];
}
result(c);
}
//outputs result
void result(multiply f)
{
cout<<"Your Result is"<<endl;
for(int h=0;h<10;h++)
{
cout<<h<<"-"<<f.myarry[h]<<endl;
}
}
};
void main()
{
multiply o,a,b,c;
o.setter(a,b,c);
} Hi, Heres the code:- Note: "ALWAYS REMEMBER THAT WHEN AN OBJECT OF A CLASS IS PASSED AS AN ARGUMENT TO A FUNCTION IT IS TREATED LOCAL FOR THAT MEMBER FUNCTION AND IT IS DESTROYED WHEN CONTROL RETURNS TO THE CALLING FUNCTION.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.