constructor and destructor assignment solutions
Page 1 of 1
constructor and destructor assignment solutions
please share constructor and destructor assignment solutions along with the question number in the comment section.
akarshsomani- Posts : 21
Join date : 2017-04-04
Re: constructor and destructor assignment solutions
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
Re: constructor and destructor assignment solutions
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.
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();
}
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
Re: constructor and destructor assignment solutions
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
Similar topics
» class and objects assignment solutions.
» Constructos and Destructors assignment question:CD-3
» sholay part 2.please share better solutions.my solution is giving time out
» Constructos and Destructors assignment question:CD-3
» sholay part 2.please share better solutions.my solution is giving time out
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum