Create a class IntClass with integer array as data member. Create a class CharClass with Char array as
data member. Convert CharClass array to IntClass. Overload + operator to add the converted data
member with the existing IntClass data member. Addition should be done based on index of the arrays.
Display the sum values.
#include<iostream>
using namespace std;
class IntClass{
public:
int arr[10];
IntClass(){
}
IntClass(int a[10]){
for(int i=0; i<10; i++){
arr[i] = a[i];
}
}
IntClass operator +(IntClass &obj){
IntClass box;
for(int i=0; i<10; i++){
box.arr[i] = this->arr[i] + obj.arr[i];
}
return box;
}
void display(){
for(int i=0; i<10; i++){
cout<<arr[i]<<"\t";
}
}
};
class CharClass{
private:
char arr1[10];
public:
CharClass (){
}
CharClass(char a[10]){
for(int i=0; i<10; i++){
arr1[i] = a[i];
}
}
IntClass Converter(CharClass &obj){
IntClass box;
for(int i=0; i<10; i++){
box.arr[i] = (int) obj.arr1[i];
}
return box;
}
};
int main(){
IntClass a;
cout<<"Code wisely\n";
int arr[10] = {4,6,7,8,9,19,1,2,4,5};
char arr1[10] = {'1','1','1','1','1','1','1','1','1','1'};
CharClass t(arr1),f;
a = f.Converter(t);//Converting data member of class CharClass to IntClass using Converter function
IntClass b(arr),c;
c= a + b;
c.display();
}
Comments
Leave a comment