如果我们使用任何原始数据类型但不适用于 ADT 它将起作用即使所有复制构造函数 ">>" "<<" "=" 运算符都被重载并且还编写了复制构造函数你可以看到下面的每个代码提前谢谢
void main(){
Array <Item> c;//It will work if we use Any Permitive Data Type but not Working for ADT Even though all copy constructor / >> << operators are overloaded
Item obj(334,"Mango Juice",90,"Drinks",10);
c.insertAtStart(obj);
c.insertAtStart(obj);/////The program Crashes Here!!
c.insertAtStart(obj);
c.PrintArray();
cout<<endl;`
////while Array.h is given below
`
#ifndef H_ARRAY
#define H_ARRAY
#include<iostream>
using namespace std;
template<class T>
class Array
{
private:
T *a;
int size; // total size
int length_used; // used size
public:
Array():a(NULL),size(0),length_used(0){}
void insertAtStart( T val){
if(isEmpty()){
a=new T();
a[0]=val;
length_used++;
size++;
cout<<"Pehli condition"<<endl;
}
else{
if(size>length_used){
shiftRight();
a[0]=val;
length_used++;
cout<<"jab size bara ho length_used"<<endl;
}
else if(size==length_used){
cout<<"jab size or length_used braber ho jao 3rd condiot"<<endl;
resizeByOne();
shiftRight();
a[0]=val;
length_used++;
}
}
}
void insertAtEnd( T val){
if(isEmpty()){
a=new T;
a[0]=val;
length_used++;
size++;
}
else{
if(size>length_used){
a[length_used+1]=val;
length_used++;
}
else if(size==length_used){
resizeByOne();
a[length_used]=val;
length_used++;
}
}
}
void deleteFromStart(){
if(isEmpty()){
cout<<"Container is Empty"<<endl;
}
else{
a[0]=='\0';
shiftLeft();
size--;
length_used--;
}
}
void deleteFromEnd(){
if(isEmpty()){
cout<<"Container is Empty"<<endl;
}
else{
a[length_used]='\0';
length_used--;
size--;
}
}
void PrintArray(){
for(int i=0;i<size;i++)
cout<<a[i]<<endl;
}
////////////////////Helper functions///////////////////////
bool isEmpty(){
if(a=='\0')
return 1;
return 0;
}
void shiftRight(){
int tempIterator=size;
for(int i=tempIterator-1;i>=0;i--)
a[i]=a[i-1];
}
void shiftLeft(){
int tempIterator=length_used;
for(int i=0;i<size;i++)
a[i]=a[i+1];
a[0]=NULL;
}
void resizeByOne(){
T *temp=new T[size+1];
for(int i=0;i<length_used;i++)
temp[i]=a[i];
a=NULL;
delete []a;
a=temp;
size++;
}
};
#endif`