Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ class that is derived from the Progression class to produce a progre

ID: 3883890 • Letter: W

Question

Write a C++ class that is derived from the Progression class to produce a progression where each value is the absolute value of the difference between the previous two values. You should include a default constructor that starts with 2 and 200 as the first two values and a parametric constructor that starts with a specified pair of numbers as the first two values.

Here is the Progression class to derive from

class Progression {

public:

Progression(long f = 0) // constructor

: first(f), cur(f) { }

virtual ˜Progression() { }; // destructor

void printProgression(int n); // print the first n values

protected:

virtual long firstValue(); // reset

virtual long nextValue(); // advance

protected: long first; // first

value long cur; // current value

};

Explanation / Answer

//main.cpp

#include <iostream>

#include "progression.hpp"

using namespace std;

/** Test program for the progression classes */

int main() {

Progression* prog;

// test ArithProgression

cout << "Arithmetic progression with default increment: ";

prog = new ArithProgression();

prog->printProgression(10);

cout << "Arithmetic progression with increment 5: ";

prog = new ArithProgression(5);

prog->printProgression(10);

// test GeomProgression

cout << "Geometric progression with default base: ";

prog = new GeomProgression();

prog->printProgression(10);

cout << "Geometric progression with base 3: ";

prog = new GeomProgression(3);

prog->printProgression(10);

// test FibonacciProgression

cout << "Fibonacci progression with default start values: ";

prog = new FibonacciProgression();

prog->printProgression(10);

cout << "Fibonacci progression with start values 4 and 6: ";

prog = new FibonacciProgression(4, 6);

prog->printProgression(10);

return EXIT_SUCCESS; // successful execution

}

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

//progression.hpp

#ifndef PROGRESSION_H

#define PROGRESSION_H

using namespace std;

// Base class:

class Progression {

public:

Progression (long f = 0) : first(f), cur(f) {}

virtual ~Progression() {};

void printProgression(int n);

protected:

virtual long firstValue(); // reset

virtual long nextValue() = 0; // advance

protected:

long first;

long cur;

};

void Progression::printProgression(int n) {

cout << firstValue();

for (int i = 2; i <= n; i++) {

cout << ' ' << nextValue();

}

cout << endl;

}

long Progression::firstValue() {

cur = first;

return cur;

}

long Progression::nextValue() {

return ++cur;

}

// Arithmetic subclass:

class ArithProgression : public Progression {

public:

ArithProgression(long i = 1);

protected:

virtual long nextValue();

protected:

long inc; // increment

};

// Constructor:

ArithProgression::ArithProgression(long i) : Progression(), inc(i) {}

// advance by adding:

long ArithProgression::nextValue() {

cur += inc;

return cur;

}

// Geometric subclass

class GeomProgression : public Progression{

public:

GeomProgression(long b = 2);

protected:

virtual long nextValue();

protected:

long base;

};

// constructor

GeomProgression::GeomProgression(long b) : Progression(1), base(b) {}

long GeomProgression::nextValue() {

cur *= base;

return cur;

}

// Fibonacci subclass:

class FibonacciProgression : public Progression {

public:

FibonacciProgression(long f = 0, long s = 1);

protected:

virtual long firstValue();

virtual long nextValue();

protected:

long second;

long prev;

};

FibonacciProgression::FibonacciProgression(long f, long s)

: Progression(f), second(s), prev(second-first) {}

long FibonacciProgression::firstValue() {

cur = first;

prev = second - first;

return cur;

}

long FibonacciProgression::nextValue() {

long temp = prev;

prev = cur;

cur += temp;

return cur;

}

#endif

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote