Create a class MyVectorclass which contains a dynamic array of sizes given in the constructor. The class must contain following methods

Create a class MyVectorclass which contains a dynamic array of sizes given in the constructor. The class must contain the following methods;

1. Constructor: Allocate dynamic memory for an array of the size taken as an argument and store zero in all elements.

2. Destructor: Deallocate the memory of the dynamic array.

3. Save_at(int index, int element): Insert an element at the given location in the array.

4. Get_at(int index): Return an element of the given location in the array.

5. Print: Display all elements present in the array. 




Solution :

#include<iostream>

using namespace std;

class My_Vector                                        //Create Classs

{

public:

int* Array;

int size_of_Array;

My_Vector(int size)                               //Create Constructor

{

size_of_Array = size;

Array = new int[size_of_Array];               //Dynamic Array

for (int i = 0; i<size_of_Array; i++)

{

Array[i] = 0;

}

}

~My_Vector()                                      //Create Desstructor

{

delete[] Array;

}

void Save_at(int index, int element)

{

Array[index] = element;

}

int Get_at(int index)

{

return Array[index];

}

void Print()                                      //Create Print

{

cout<<"....................................................."<<endl;

cout << "\tShow All Elements Store In Array" << endl;

cout<<"....................................................."<<endl;

cout<<endl;

for (int i = 0; i<size_of_Array; i++)

{

cout << "\tIndex Address "<<i+0<<" At Store Value"<<" =\t " << Array[i] << endl;

}

}

};

 

int main()

{

int Index_Address, Show_Number_Given_Index;

My_Vector v(10);                                        //Array Size

Index_Address = 7;                                     //Index Address

Show_Number_Given_Index = 396;                          //element value

v.Save_at(Index_Address, Show_Number_Given_Index);

cout<<"....................................................."<<endl;

cout<<"\t\tProgram Information "<<endl;

cout<<"....................................................."<<endl;

cout<<endl;

cout<<"\tTotal Array Size =\t"<<v.size_of_Array<<endl;

cout <<"\tUser Store Value =\t"<<Show_Number_Given_Index<<endl;

cout<<"\tAt Index Address =\t"<<Index_Address<<endl;

cout<<endl;

v.Print();

return 0;

}


OUTPUT


Create a class MyVectorclass which contains a dynamic array of sizes given in the constructor. The class must contain following methods
Create a class MyVectorclass which contains a dynamic array of sizes given in the constructor. The class must contain the following methods




 random/hot-posts 

PROGRAMMING QUESTIONS/feat-big


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.