Coding Discussions
Would you like to react to this message? Create an account in a few clicks or log in to continue.

constructor and destructor assignment solutions

Go down

constructor and destructor assignment solutions Empty constructor and destructor assignment solutions

Post by akarshsomani Fri Apr 21, 2017 9:03 pm

please share constructor and destructor assignment solutions along with the question number in the comment section.

akarshsomani

Posts : 21
Join date : 2017-04-04

Back to top Go down

constructor and destructor assignment solutions Empty Re: constructor and destructor assignment solutions

Post by akarshsomani Fri Apr 21, 2017 9:04 pm

cd1:
Code:
#include<iostream>
using namespace std;
class Array
{
    int *arr;
    int n;
public:
    Array(int n1)
    {
        n=n1;
        arr=new int[n];
        for(int i=0;i<n;i++)
            cin>>arr[i];
    }
    Array()
    {
        n=0;
    }
    void show()
    {
        cout<<"\n";
        for(int i=0;i<n;i++)
            cout<<arr[i]<<"\t";
            cout<<"\nlargest among all is "<<largest()<<"\n";
    }
    int largest()
    {
        int ga=arr[0];
        for(int i=1;i<n;i++)
            if(ga<arr[i])
            {
                ga=arr[i];
            }
            return ga;
    }
};
main()
{
    Array a1;
    a1.show();
    Array a2(10);
    a2.show();
}

akarshsomani

Posts : 21
Join date : 2017-04-04

Back to top Go down

constructor and destructor assignment solutions Empty Re: constructor and destructor assignment solutions

Post by akarshsomani Mon Apr 24, 2017 8:09 am

cd2:
First two destructor is called to delete the memory of objects passed in add function as parameters,rest three are for main method objects.
Code:
#include<iostream>
using namespace std;
class matrices
{
    int **a;
    int n;
public:
    matrices(int n1)
    {
        n=n1;
        a=new int*[n];
        for(int i=0;i<n;i++)
        {
            a[i]=new int[n];
        }
    }
    ~matrices()
    {
        cout<<"memory cleared\n";
    }
    void get()
    {
        cout<<"enter elements\n";
       for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
               cin>>a[i][j];
            }
        }
    }
    void add(matrices m1,matrices m2)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
               a[i][j]=m1.a[i][j]+m2.a[i][j];
            }
        }

    }
    void show()
    {
        cout<<"\nMatrix\n";
         for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                cout<<a[i][j]<<"\t";
            }
            cout<<"\n";
        }
    }
};
main()
{
    int k;
    cout<<"enter order of matrix: ";
    cin>>k;
    matrices m1(k),m2(k),m3(k);
    m1.get();
    m2.get()
    m1.show();
    m2.show();
    m3.add(m1,m2);
    cout<<"After addition\n";
    m3.show();

}

constructor and destructor assignment solutions Output11


Last edited by akarshsomani on Mon Apr 24, 2017 8:20 am; edited 1 time in total

akarshsomani

Posts : 21
Join date : 2017-04-04

Back to top Go down

constructor and destructor assignment solutions Empty Re: constructor and destructor assignment solutions

Post by akarshsomani Mon Apr 24, 2017 8:10 am

cd4:
Code:
#include<iostream>
using namespace std;
class Array
{
    int *a;
    int n;
public:
    Array(int n1)
    {
        n=n1;
        a=new int[n];
    }
    ~Array()
    {
        cout<<"memory cleared\n";
    }
    void get()
    {
        cout<<"enter elements\n";
      for(int i=0;i<n;i++)
        {
          cin>>a[i];
        }
    }
    void ascending(Array m1,Array m2)
    {
            for(int j=0;j<n;j++)
            {
                if(m1.n>j)
                {
                    a[j]=m1.a[j];
                }
                else
                {
                    a[j]=m2.a[j-m1.n];
                }
            }
          for(int i=0;i<n;i++)
        {
            for(int j=0;j<n-i-1;j++)
            {
                if(a[j]>a[j+1])
                {
                    int temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
    }
    void show()
    {
        cout<<"\nMatrix\n";
        for(int i=0;i<n;i++)
        {

                cout<<a[i]<<"\t";

        }
    }
};
main()
{
    int k1,k2;
    cout<<"Enter length of array 1:";
    cin>>k1;
    cout<<"Enter length of array 2:";
    cin>>k2;
    Array m1(k1),m2(k2),m3(k1+k2);
    m1.get();
    m2.get();
    m1.show();
    m2.show();
    m3.ascending(m1,m2);
    cout<<"After sorting\n";
    m3.show();

}


akarshsomani

Posts : 21
Join date : 2017-04-04

Back to top Go down

constructor and destructor assignment solutions Empty Re: constructor and destructor assignment solutions

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum