Sei sulla pagina 1di 4

CS125: Programming Fundamentals

Lab No. 10 Custom Vectors


Note: Be with me, we will work together and I will explain each function in detail.

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

Questions to be answered before starting lab work


If you made software for attendance and you made software for 20 students. What if the students increase to 30? What is difference between Vectors and arrays?

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; } };

//by default null pointer

Constructor Overloading
#include<iostream>

Lab No. 10: Custom Vectors

CS125: Programming Fundamentals


using namespace std; class my_vector { double *p; int size; public: my_vector() //by default null pointer { size=0; p=0; } my_vector(int s, double val = 0) { size=s; p=new double[size]; for(int i=0; i<size; i++) p[i] = val; }

Debug the above code and answer what is the purpose of constructor overloading.

Resizing Array Function


. . . void resize(int new_size) { if(new_size > size ) { double * pnew=new double[new_size]; for(int i=0; i<size; i++) //for data shifting { pnew[i]=p[i]; } size = new_size; delete [] p; p = pnew; } } . . .

User Defined push_back() Function


void push_back(double val){ resize(size+1); p[size-1] = val; }

Write a function which push the value on front of the array.

Lab No. 10: Custom Vectors

CS125: Programming Fundamentals

User Defined pop_front () Function


void pop_front() { size--; for(int i=0; i<size; i++) { p[i]=p[i+1]; } }

Write another function which pop the value at the end of the Array.

Destructor
~my_vector() { delete [] p; }

Is it necessary to use Destructor. If yes then Why?

Insert and Get value on Specific Index


void at(int i, double val) // insert value { if((i<0)|| (i>size)) { cout<<"Invalid Index Selection\nYour range is between 0 to "<<size<<" ."<<endl; } else { p[i]=val; } } double at(int i) //get value { return p[i]; }

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]; }

With Operator Overloading


double & operator [](int i) { return p[i]; }

Lab No. 10: Custom Vectors

CS125: Programming Fundamentals

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]; } }

//for data shifting

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:

Date: August 23 , 2013

Lab No. 10: Custom Vectors

Potrebbero piacerti anche