Question

28. Implement an overloaded member function of the IntVector class named assign. This version of assign passes in an array of

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//IntVector.h

#pragma once
#include<iostream>

using namespace std;

class IntVector
{
   int unsigned sz;
   int unsigned cap;
   int *data;
   void expand();
   void expand(unsigned int amount);
public:
   IntVector();
   IntVector(unsigned size);
   IntVector(unsigned size, int value);
   void assign(unsigned n, int value);
   void assign(int a[], unsigned begin, unsigned end);
   void print();
};

=============================

//IntVector.cpp

#include"IntVector.h"

IntVector::IntVector()
{
   sz=0;
   //default capacity
   cap=2;
   data=new int[cap];
}
IntVector::IntVector(unsigned size)
{
   sz = 0;
   cap = size;
   data = new int[cap];
}
IntVector::IntVector(unsigned size, int value)
{
   sz = 0;
   cap = size;
   for (int i = 0; i < size; i++)
       data[i] = value;
}
void IntVector::assign(unsigned n, int value)
{
   expand(n);
   for (int i = 0; i < n; i++)
       data[sz++] = value;
}
void IntVector::assign(int a[], unsigned begin, unsigned end)
{
   expand(end);

   for (int i = begin; i <= end; i++)
   {
       data[sz++] = a[i];
   }
}
void IntVector::expand()
{
   cap = cap*2;
   int *tmp = new int[cap];
   for (int i = 0; i < sz; i++)
   {
       tmp[i] = data[i];
   }
   data = tmp;
}
void IntVector::expand(unsigned int amount)
{
   cap = amount;
   int *tmp = new int[cap];
   for (int i = 0; i < sz; i++)
   {
       tmp[i] = data[i];
   }
   data = tmp;
}
void IntVector::print()
{
   for (int i = 0; i < sz; i++)
   {
       cout << data[i] << " ";
   }
   cout << endl;
}

================

//Main.cpp for testing

#include"IntVector.h"

int main()
{
   int iarr[] = { 1,2,3,4,5,6,7,8,9,10 };
   IntVector v;
   v.assign(iarr, 1, 5);
   //print vector
   cout << "vector is : ";
   v.print();
}

/*Output
vector is : 2 3 4 5 6
*/

Add a comment
Know the answer?
Add Answer to:
28. Implement an overloaded member function of the IntVector class named assign. This version of assign passes in an ar...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class declaration named Circle with a private member variable named radius.

     in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159*radius*radius. Add a default constructor to the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign its value to the radius...

  • Write a class declaration named Circle with a private member variable named radius.

    CODE IN C++Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius. Add a default constructor the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign radius...

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

  • You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function...

    You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function into a file named Q1.cpp. Q1.cpp should only include your function implementation, the necessary #include directives if needed, and should not contain anything else such as the main function or global variable declarations. Test your code using a separate main.cpp file where you implement a sufficient number of test cases. You should use Q1.h as a header file and Q1main.cpp as a main function...

  • Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower

    Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower named Rose. The Rose class has one member variable name.  Add a constructor which expects color and  name. Pass color to the base constructor and set name to it's member variable.Write a program that has an array of ...

  • Assuming you have the following ordered-array class definition: class ordered_array { public: ordered_array(int c) { data...

    Assuming you have the following ordered-array class definition: class ordered_array { public: ordered_array(int c) { data = new int[c]; cp = c; sz = 0; } ... private: int sz, cp; // Current size, total capacity int* data = nullptr; }; Suppose that ordered_arrays can contain duplicates. Implement a method remove_duplicates(e)which takes an element e and removes it and any duplicates of it. (Note that e is the actual value to be removed, not its index in the array.) If...

  • Write a full class definition for a class named Player , and containing the following members: A data member name of t...

    Write a full class definition for a class named Player , and containing the following members: A data member name of type string . A data member score of type int . A member function called setName that accepts a parameter and assigns it to name . The function returns no value. A member function called setScore that accepts a parameter and assigns it to score . The function returns no value. A member function called getName that accepts no...

  • C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement...

    C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement the Inner and Outer classes (Circular Buffer of circular buffers using Queues). No need of any classes from the Standard Template Library (STL), not even vector. Add the member functions of those classes by following the codes of InnerCB.h and CBofCB.h below: // file: InnerCB.h // Header file for Inner Circular Buffer. // See project description for details. // #ifndef _INNERCB_H_ #define _INNERCB_H_ class...

  • C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed. Chapter 8 discussed vectors, which are like arrays that...

    C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed. Chapter 8 discussed vectors, which are like arrays that can grow in size. Suppose that vectors were not defined in C++. Define a class called VectorDoublethat is like a class for a vector with base type double. Your class VectorDoublewill have a private member variable for a dynamic array of doubles. It will also have two member variables of type...

  • Your will write a class named Gasket that can be used to build Gasket objects (that...

    Your will write a class named Gasket that can be used to build Gasket objects (that represent Sierpinski gaskets) that you can display graphically.  Your Gasket class is defined in the provided file Gasket.h.   DO NOT CHANGE the file Gasket.h.  The attributes and methods defined for class Gasket are described below.     ·sideLength            an int that holds the length of each side of the Gasket.  The length of the side is measured as a number of pixels ·xLocation              an int that holds the x coordinate (in pixels)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT