Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 645179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:28:25+00:00 2026-05-13T21:28:25+00:00

Have a look at the following code you are not required to read the

  • 0

Have a look at the following code you are not required to read the whole code just read the code of copy constructor and the main program.

the following statement in the copy constructor

//the statement below should do the shallow copy 
    ptr[currentsize++]=object.ptr[i];

this statement should do a shallow copy in case of array of pointers so please see the code of copy constructor and the main function i have mentioned the problemes in the code.

#include<iostream.h>
template<typename t>
class vector
{
public:
    vector(int size);
    vector(const vector<t>&);
    void insert(t);
    void display()const;
    void makeEmpty();//sets all pointers to NULL
private:
    t* ptr;
    int size;
    int currentsize;
};
template<typename t>
vector<t>::vector<t>(int s)
{
    currentsize=0;
    size=s;
    ptr=new t[size];
}
//copy constructor
template<typename t>
vector<t>::vector<t>(const vector<t>& object)
{
    size=object.size;
    ptr=new t[size];
    currentsize=0;
    for(int i=0;i<object.currentsize;i++)
    {
        //the statement below should do the shallow copy 
        ptr[currentsize++]=object.ptr[i];
    }
}
template<class t>
void vector<t>::insert(t element)
{
    if(currentsize < size)
        ptr[currentsize++]=element;
}
template<class t>
void vector<t>::display()const
{
    for(int i=0;i<currentsize; i++)
    {
        cout<<ptr[i]<<endl;
    }
}
template<class t>
void vector<t>::makeEmpty()
{
    for(int i=0;i<currentsize;i++)
        ptr[i]=NULL;
}
main()
{
    vector<char*>object(10);
    char *array[]={"zia","arshad","muneeb"};
    object.insert(array[0]);
    object.insert(array[1]);
    object.insert(array[2]);
    vector<char*> object1=object;
    cout<<"displaying the new object"<<endl;
    object1.display();
    object.makeEmpty();//sets all pointers to NULL
    //now here the object 1 should be changed but its not 
    cout<<"again displaying object1"<<endl;
    object1.display();//still displaying the three names 

    cout<<endl;
    system("pause");

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:28:25+00:00Added an answer on May 13, 2026 at 9:28 pm

    You get exactly what you wrote. Your copying makes a copy of the array. You shouldn’t expect changes to the first vector touch the second one.

    Note that you’ve removed pointers to the strings in the first vector. In the second they stay intact.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.