Array Addition using Class Function in C++


Example Program:

Here is the simple program for printing the array addition using class function in C++.

Source Code

#include<iostream>
using namespace std;
class Add
{
  public:
    void sum(int r,int c)
    {
      int m1[r][c],m2[r][c],s[r][c];
      cout<<"\nEnter the elements of  First matrix: ";
      for(int i=0;i<r;i++)
      {
        for (int j=0;j<c;j++)
        {
          cin>>m1[i][j];
        }
      }
      cout<<"\nEnter the elements of Second matrix: ";
      for(int i=0;i<r;i++)
      {
        for(int j=0;j<c;j++)
        {
           cin>>m2[i][j];
        }
      }
      cout<<"Output: ";
      for (int i=0;i<r;i++)
      {
        for (int j=0;j<c;j++)
        {
           s[i][j]=m1[i][j]+m2[i][j];
           cout<<s[i][j]<<" ";
        }
      }
    }
};
int main()
{
  int row,col;
  cout<<"\nEnter the number of rows: ";
  cin>>row;
  cout<<"\nEnter the number of column: ";
  cin>>col;
  Add obj;
  obj.sum(row, col);
  return 0;
}
To download raw file Click Here

Output

Enter the number of rows: 2
Enter the number of column: 2
Enter the elements of  First matrix: 1
2
3
4
Enter the elements of Second matrix: 5
6
7
8
Output: 6 8 10 12

Basic Programs


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples



Learn All in Tamil © Designed & Developed By Tutor Joes