You must Sign In to post a response.
  • C++ programming questions To perform type conversion from one class type to another class type


    Finding difficulty in writing C++ program for type conversion and matrix multiplication? Looking out for detailed program solution online? You can scroll thorugh this Ask Expert page and get answers for your query here.

    S.
    To perform type conversion from one class type to another class type.

    Demonstrate the conversion from one class type to another class type with a program by taking two classes namely
    'Distance' (with two data members feet and inch) and 'Length' (with single field inches).

    Concept explanation with proper program logic,program
    documentation and output screenshots.
    Students will practice the basic concepts of Object oriented programming.


    To overload operators by assigning special characters.

    Develop a program to perform addition of two matrices by overloading binary + operator using member function.

    Proper program logic, program documentation and output
    Students can develop applications by using the concepts.
  • Answers

    1 Answers found.
  • Here with I am providing the answer of your second question that is how to overload + operator to add two matrices .
    Basically overloading of existing operators is nothing but to extend the meaning of existing operators. Those functions that we can easily perform on basic data types like int, float etc, with the help of operator overloading we can easily perform same functions on class type(that is on objects). So that to extend the meaning of existing operators from basic type to class type is called as operator overloading.
    Operator overloading is done with a special member function which is generally called as operator op function. ( operator op() ). This function may be simple member function or it may be a friend function.
    For binary operator overloading (like a + b) using simple member function we have to pass only one argument to the function and for friend function we have to pass two arguments to the function. This is because in case of simple member function as per rule left hand side operand of operator must be an object and it's responsibility is to invoke operator function. Right hand side operand of operator is passed as an argument.
    In case of friend function both the operands on left and right hand side of operator must be passed as an argument.
    in this problem we have to overload + operator which is binary operator. I am providing program logic using simple member function as well as using friend function.
    Below are the programs. Please include and header files before class declarations.

    First program is to overload + operator for addition of two matrices using simple member function.

    //USING OPERATOR OVERLOADING SUM OF ARRAY.
    #include
    #include
    class array
    {
    private:
    int num[5];
    public:
    void get(void)
    {
    cout<<"Enter any five integers\n";
    for(int i=0;i<5;i++)
    {
    cin>>num[i];
    }
    }
    void display(void)
    {
    cout<<"\nArray elements are as follows\n";
    for(int i=0;i<5;i++)
    {
    cout< }
    }
    array operator+(array c1)
    {
    array temp;
    for(int i=0;i<5;i++)
    {
    temp.num[i]=num[i]+c1.num[i];
    }
    return(temp);
    }
    };

    void main(void)
    {
    class array a1,a2,a3;
    clrscr();
    a1.get();
    a2.get();
    clrscr();
    cout<<"\nContents of first object are as below\n";
    a1.display();
    cout<<"\nContents of second object are as below\n";
    a2.display();
    a3=a1+a2;
    cout<<"\nContents of third object are as below\n";
    a3.display();
    getch();
    }

    ***********************************************************************************************************************

    Here is the program to overload + operator for addition of two matrices using friend function. Please include and header files before class declaration.

    #include
    #include
    class array
    {
    private:
    int num[5];
    public:
    void get(void)
    {
    cout<<"Enter any five integers\n";
    for(int i=0;i<5;i++)
    {
    cin>>num[i];
    }
    }
    void display(void)
    {
    cout<<"Array elements are as follows\n";
    for(int i=0;i<5;i++)
    {
    cout< }
    cout<<"\n";
    }
    friend array operator+(array c1, array c2)
    {
    array temp;
    for(int i=0;i<5;i++)
    {
    temp.num[i]=c1.num[i]+c2.num[i];
    }
    return(temp);
    }
    };

    void main(void)
    {
    class array a1,a2,a3;
    clrscr();
    a1.get();
    a2.get();
    cout<<"\n==============================================\n\n";
    cout<<"Contents of first object are as below\n";
    a1.display();
    cout<<"Contents of second object are as below\n";
    a2.display();
    a3=a1+a2;
    cout<<"Contents of third object are as below\n";
    a3.display();
    getch();
    }

    output-for-addition-of-two-matrices-using-operator-overloading-frie.docx

    Delete Attachment


  • Sign In to post your comments