Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task 1: Recall the class we used in the previous lab to allocate memory dynamically. Modify the header file and the source file given below

Task 1: Recall the class we used in the previous lab to allocate memory dynamically. Modify the header file and the source file given below so that they now work as template class (the array elements in the dynamically allocated memory can be any type as the user defines). dynarr.h #ifndef DYNARR_H_INCLUDED #define DYNARR_H_INCLUDED class dynArr { private: int *data; int size; public: dynArr(int); ~dynArr(); void setValue(int, int); int getValue(int); }; #endif // DYNARR_H_INCLUDED

dynarr.cpp #include "dynarr.h" #include using namespace std; dynArr::dynArr(int s) { data = new int[s]; size = s; } dynArr::~dynArr() { delete [] data; } int dynArr::getValue(int index) { return data[index]; } void dynArr::setValue(int index, int value) { data[index] = value; }

Task 2: Recall the complex number class we discussed in our lectures. Modify the class and overload the * (multiplication) and the != (not equal) operators for the class given below. complex.h #ifndef COMPLEX_H_INCLUDED #define COMPLEX_H_INCLUDED class Complex { public: Complex(); Complex(double, double); Complex operator+(Complex); void Print(); private: double Real, Imaginary; }; #endif // COMPLEX_H_INCLUDED

complex.cpp #include "complex.h" #include using namespace std; Complex::Complex() { Real = 0; Imaginary = 0; } Complex::Complex(double r, double i) { Real = r; Imaginary = i; } Complex Complex::operator+(Complex a) { Complex t; t.Real = Real + a.Real; t.Imaginary = Imaginary + a.Imaginary; return t; } void Complex::Print() { cout << Real << endl; cout << Imaginary << endl; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

ISBN: 1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago