Using C++ to write .cpp and .h file. Main function and sample output are given. The task is to write a vector class for dynamic allocation.declare the the class named vector with the required attributes. The task are defined in the main function.
Implemented Code
#include <iostream>
using namespace std;
class vector
{
public:
vector() :vector(initialsize)
{
cout << "\nObject of class
vector was created using the vector() constructor";
}
vector(size_t size) :_size(size)
{
cout << "\nObject of class
vector was created using the vector(size_t) constructor";
data = new int[_size] {0};
}
vector(int* arr, size_t size)
{
data = new int[size];
for (size_t i = 0; i < size;
i++)
{
data[i] =
arr[i];
}
_size = size;
}
void assign(int* arr, size_t size)
{
if (data)
{
delete[]
data;
_size = 0;
}
data = new int[size];
for (size_t i = 0; i < size;
i++)
{
data[i] =
arr[i];
}
_size = size;
}
friend ostream& operator << (const
ostream& out, const vector& v)
{
cout << "\nVector :
size(3)";
cout << "\ndata[0] :"
<< data[0];
cout << "\ndata[1] :"
<< data[1];
cout << "\ndata[2] :"
<< data[2];
}
vector add(const vector& v)
{
int sizeofnewv = (this->_size
> v._size) ? v._size : this->_size;
vector res(sizeofnewv);
int k = 0;
for (size_t i = 0,j = 0; i <
this->_size,j < v._size; i++,j++)
{
res.data[k] =
this->data[i] + v.data[j];
k++;
}
return res;
}
vector operator + (const vector& v)
{
int sizeofnewv = (this->_size
> v._size) ? this->_size : v._size;
vector res(sizeofnewv);
int k = 0;
for (size_t i = 0, j = 0; i <
this->_size, j < v._size; i++, j++)
{
res.data[k] =
this->data[i] + v.data[j];
k++;
}
return res;
}
int& operator[](int index)
{
if(index < _size)
return
data[index];
return data[0];
}
void resize(size_t size)
{
data = (int*)realloc(data,
size);
}
~vector()
{
if(data)
delete[]
data;
_size = 0;
cout << "\nObject of class
vector is deleted";
}
private:
const int initialsize = 3;
int _size;
public:
int* data;
};
Using C++ to write .cpp and .h file. Main function and sample output are given. The...
Who could write the array.cpp file ? //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...
Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Template Classes – Chapter 12 Write an example of a template function that can swap 2 generic elements. Create a C++ class vector using the class diagram shown in Figure 12.2. in Liangs textbook. Do not use the C++ provided header file <vector>...
My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is not empty s1 (size 4): 4 6 2 17 s1 is not empty Testing copy constructor s1 (size 4): 4 6 2 17 s2 (size 4): 4 6 2 17 Testing clear() s1 (size 0): s2 (size 4): 0 1477251200 1477251168 1477251136 s3 (size 4): 28 75 41 36 Testing assignment operator s3 (size 4): 28 75 41 36 s4 (size 4): 28 75...
One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << ; cout << endl; return 0; }
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...
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...
Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for push_back() function as below: void push_back(string str) { // increase vector size by one // initialize the new element with str } In addition, the standard library vector doesn't provide push_front(). Implement push_front() for your vector. Test your code with the main function below. int main() { vector v1(3); cout<<"v1: "; v1.print(); // this should display -, -, - for...
When running the program at the destructor an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public: varArray(); // void constructor int arraySize() const { return size; } // returns the size of the array int check(double number); // returns index of element containg "number" or -1 if none void addNumber(double); // adds number to the array void removeNumber(double); // deletes the number from the array ...
1. Create an “include guard” (all lines of code required) for a file “plane.h” 2. When you look at a constructor, how can you determine if it is THE default constructor? 3. What can a “friend” function do that other non-member functions can not do? 4. class plane { int x;} ---------------- is x public or private? 5. What kind of a search first sorts the data into ascending or descending order, then splits the data in half, searches, splits,...
It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) { this->type = type; this->name = name; } //returns the type. string File::getType() { return type; } //returns the name of file. string File::getName() { return name; } File.h #ifndef __FILE_H__ #define...