Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Objective
1. 2. 3. 4. 5. 6. 7. 8. Vector Class Constructor Overloading Dynamic Memory Allocation User Defined push_back() function. Resize Array. Destructor. Operator overloading Copy Constructor
Vector Class
class my_vector { double *p; int size; };
Default constructor
class my_vector { double *p; int size; public: my_vector() { size=0; p=0; } };
Constructor Overloading
#include<iostream>
Debug the above code and answer what is the purpose of constructor overloading.
Write another function which pop the value at the end of the Array.
Destructor
~my_vector() { delete [] p; }
What if we make a single function which we can insert value and can get value as well. But how?
double & at(int i) { return p[i]; }
Copy Constructor
What is the Purpose of copy Constructor? Why we used copy constructor? What are the pros and cons of copy constructor?
my_vector(my_vector & v2) { size=v2.size; p=new double[size]; for(int i=0; i<size; i++) { p[i]=v2.p[i]; } }
Main Function
int main() { my_vector v(5); v.push_back(3.5); v.push_back(4.0); v.pop_front(); v.resize(10); v.at(8, 1000); v.at(9) = 900; cout << "\n"<<v[4]; v[2] = 222; { my_vector v2 = v; } v[2] = 222; system("pause"); return 0; }
Checked By: