Please code the Copy Constructor (the reverse)

Question

Please code the Copy Constructor (the reverse) and the removeLastThree function described in B2 without using the existing functions.
B2

#include <iostream>
#include<vector>
using namespace std;
int i=0;
class node
{
public :
char bag[100];
int length=0;
node()
{
for(i=0;i<100;i++)
{
bag[i]='\0';
}
}
node(node &old_bag)
{
int j=0;
for(i=(old_bag.length-1);i>=0;i--)
{
this->bag[j++]=old_bag.bag[i];  
}
this->length=old_bag.length;
}
void addVector(std::vector<std::string> vtr)
{
int j=this->length;
for(i=0;vtr.at(0)[i]!='\0';i++)
{
this->bag[j++]=vtr.at(0)[i];
}
this->length=j;
}
void remove(int i)
{
int j=0;
for(j=i;j<length-1;j++)
{
this->bag[j]=this->bag[j+1];
}
this->bag[j]='\0';
this->length-=1;
}
void removeLast3(char c)
{
int count=3;
for(i=length-1;i>0;i--)
{
if((count>0)&&(this->bag[i]==c))
{
this->remove(i);
count=count-1;
i=i+1;
}
}
cout<<endl;
if(count>0)cout<<"There are no three occurences of given character";
}
};
int main() {
char temp[]={'e','l','e','c','t','r','i','c','a','l'};
node old_bag;
for(i=0;i<sizeof(temp);i++)
{
old_bag.bag[i]=temp[i];
}
old_bag.length=sizeof(temp);
for(i=0;i<old_bag.length;i++)
{
cout<<old_bag.bag[i];
}
cout<<endl;
node new_bag(old_bag);
for(i=0;i<new_bag.length;i++)
{
cout<<new_bag.bag[i];
}
cout<<endl;
string k ="engineer";
vector<string> v;
v.push_back(k);
new_bag.addVector(v);
for(i=0;i<new_bag.length;i++)
{
cout<<new_bag.bag[i];
}
cout<<endl;
new_bag.removeLast3('e');
for(i=0;i<new_bag.length;i++)
{
cout<<new_bag.bag[i];
}
cout<<endl;
}

OUTPUT:

electrical
lacirtcele
lacirtceleengineer
lacirtcelenginr

Summary

Initially in the given question we there is a given code for which the two required code for the two functionalities has been getting changed. Here we have changed the copy constructor and the method named ‘removeLast3’. where there is no existing methods were used in that methods.

Explanation

Basically, in the given program node old_bag named object is passed to the copy constructor. And it will get traversed from the front. Where characters have been copied to the current object from the last where the length of the old_bag node is from the index equals.
In the method removeLast3, we have set the variable count to 0. After that from the end the bag is traversed and each character will get checked. If the character that is passed to the function is equal or not. If the program found that the character is equal then the count will get incremented by 1, and all the elements in the list after the character will get shifted to the left side by the one index. Once the loop reaches the number 3 it will stop executing.

Code

#include <iostream>
#include<vector>
using namespace std;
int i=0;
class node
{
public :
char bag[100];
int length=0;

node()
{
for(i=0;i<100;i++)
{
bag[i]='\0';
}
}

/* copy constructor code has been changed */
node(node &old_bag)
{
    /* to update the length of the new bag */
    this->length=old_bag.length;
    /* the old_bag is traversed from front, but 
    the characters are stored from the end in the new_bag */
    for(int i=0;i<sizeof(old_bag.bag);i++)
    {
        bag[old_bag.length-i-1]=old_bag.bag[i];
    }
}

void addVector(std::vector<std::string> vtr)
{
int j=this->length;
for(i=0;vtr.at(0)[i]!='\0';i++)
{
this->bag[j++]=vtr.at(0)[i];
}
this->length=j;
}

void remove(int i)
{
int j=0;
for(j=i;j<length-1;j++)
{
this->bag[j]=this->bag[j+1];
}
this->bag[j]='\0';
this->length-=1;
}

/* the function removeLast3 has been modified */
/* no existing functions are used */
void removeLast3(char c)
{
    /* to count the number of occurrrences of the given character */
    int count=0;
    /* to check if the three occrrence are erased */
    bool bb=false;
    /* traversing the bag from end */
    for(int i=length-1;i>=0;i--)
    {
        /* if the character matches with c */
        if(bag[i]==c)
        {
            /* shifting the characters by 1 */
            for(int j=i;j<length;j++){
                bag[j]=bag[j+1];
            }
            /* decrementing the length by 1, after erasing */
            this->length-=1;
            i++;
            count++;
            if(count==3)
            {
                bb=true;
                break;
            }
        }
    }
    if(bb==false)
    cout<<"There arn't enough occurrence of character-"<<c<<" in the string"<<endl;
}

};

int main() {
char temp[]={'e','l','e','c','t','r','i','c','a','l'};
node old_bag;
for(i=0;i<sizeof(temp);i++)
{
old_bag.bag[i]=temp[i];
}
old_bag.length=sizeof(temp);
for(i=0;i<old_bag.length;i++)
{
cout<<old_bag.bag[i];
}
cout<<endl;
node new_bag(old_bag);
for(i=0;i<new_bag.length;i++)
{
cout<<new_bag.bag[i];
}
cout<<endl;
string k ="engineer";
vector<string> v;
v.push_back(k);
new_bag.addVector(v);
for(i=0;i<new_bag.length;i++)
{
cout<<new_bag.bag[i];
}
cout<<endl;
new_bag.removeLast3('e');
for(i=0;i<new_bag.length;i++)
{
cout<<new_bag.bag[i];
}
cout<<endl;
}

Output

Copy Constructor (the reverse)

 

Also read, Can you fix any mistake on this C program

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *