Not getting proper output after overloading <<


for the first two values it prints the addresses or some random value

<< operator overloading is working fine

you don’t have copy constructor
hence when you are trying to print fun then only it shows error

sir my doubt is when right fun=v for the first two values it shows some random value how it is

#include
using namespace std;
// define a user-defined vector class
class vector{
int arr;
int cs;
int max_size;
public:
vector(int default_size=4){
max_size=default_size;
cs=0;
arr=new int[max_size];
}
void push_back(int data){
if(cs==max_size){
// Doubling Operations
int oldArr=arr;
arr=new int[2
max_size];
max_size
=2;
for(int i=0;i<cs;i++){
arr[i]=oldArr[i];
}
// Delete the oldArr
delete [] oldArr;
}
arr[cs]=data;
cs++;
}
bool empty(){
return cs==0;
}
int getsize()
{
return cs;
}
int getmax_size()
{
return max_size;
}
void pop_back(){
if(!empty()){
cs–;
}
}
void print(){
for(int i=0;i<cs;i++){
cout<<arr[i]<<",";
}
}
int at(int i){
return arr[i];
}
};
ostream &operator <<(ostream &os,vector v)
{
cout<<“in this operator”<<endl;
v.print();
return os;
}
int main()
{
vector v(1000);---------------------->after declare size i get the correct output i.e contents get copied correctly from vector v to fun
for(int i=0;i<=3;i++)
{
v.push_back(i*i);
}
vector fun;
fun=v;
v.push_back(7);
cout<<v<<fun;
return 0;
}
please justify it means aaesa kyu hooraha hein

means sir doubt ye hein ki when we make vector v only without calling constructor it does not copy the content of vector v to fun but jabb hum constructor ko call karrahe with declaring size also while making vector v content is copy correctly and in the lecture we use copy assignment operator for copy

hi @dipeshgupta197
I just going through you code and realizes you mistake

actually what happen in this code is that as your class doesn’t have copy constructor so when you write
fun=v;
it copies the value of all data member of v to data members of fun
hence the pointer (arr) which hold the address of array also got copied to fun

and we we delete the arr pointer here

if(cs==max_size){

            // Doubling Operations

            int *oldArr=arr;

            arr=new int[2*max_size];

            max_size*=2;

            for(int i=0;i<cs;i++){

                arr[i]=oldArr[i];

            }

            // Delete the oldArr

            delete [] oldArr; // HERE <========

        }

then arr pointer of v got update
but arr pointer of fun will still points to that deleted memory address
hence it prints garbage value

i hope now its clear
to avoid such thing make a copy constructor

now plz give feedback from below link

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

  1. HOW TO CORRECT THIS I UNDERSTAND THE EXPLAINATION THAT IS WE HAVE TO MAKE COPY CONSTRUCTOR
  2. WHAT ABOUT THE MENTIONING OF SIZE V(6) BECAUSE OF WHICH GARBAGE VALUE NOT COME
  3. HOW TO MAKE A COPY CONSTRUCTOR FOR THIS

because in that case deletion will not happen
6>4

i will share the code soon

meanwhile you can give your feedback

WHY DELETION IS NOT HAPPEN

YUP NOW ITS CLEAR
SOLUTION IS TO MAKE COPY CONSTRUCTOR

JUST PROVIDE THE CODE FOR COPY CONSTUCTOR I ALSO TRIED FROM MY SIDE JUST PLS DM ME

thanks bro it runs fine you checked it

thanks it runs fine keep your constant support with me so that mein bhi placed hoojau in some good company just dm me once if you dont have any problem