Answer to Question #206247 in C++ for Ahmad

Question #206247

Code the following diagram Progression Class should be taken as abstract class,

think carefully before implementation. Correct Approach will give you maximum

marks.

Class progession

Fields:long first

Long cur

Methods:Progession()

Long firstvalue()

Long nextvalue()

Void print progession()


Implement through composition


Class ArithProgression

Fields:long inc

Methods:ArithProgression()

ArithProgression(long)

Long nextvalue()


2) class GeomProgression

Fields:long base Methods:GeomProgression()

GeomProgression(long)

Long nextvalue()


3)class FibonacciProgression

Fields:long prev

Methods:FibonacciProgression()

FibonaciiProgression(long, long)

Long nextvalue()


To complete our program, we define the main function, which p erforms a simpletest of each of the 3 classes.


1
Expert's answer
2021-06-13T00:03:19-0400

Answer:-


#include <iostream>
using namespace std;
class Progression{
    protected:
        long first, cur;
    public:
        Progression(){}
        long firstvalue(){
            return first;
        }
        virtual long nextvalue(){
            return cur;
        }
        virtual void print_progression(){}
};
class ArithProgression: public Progression{
    long inc;
    public:
        ArithProgression(): Progression(){}
        ArithProgression(long f): Progression(){
            cout<<"Input first value of arithmetic progression: ";
            cin>>first;
            cur = first;
            inc = f;
        }
        long nextvalue(){
            cur += inc;
            return cur;
        }
};
class GeomProgression: public Progression{
    long base;
    public:
        GeomProgression(): Progression() {}
        GeomProgression(long b): Progression(), base(b){
            cout<<"Input first value of geometric progression: ";
            cin>>first;
            cur = first;
        };
        long nextvalue(){
            cur *= base;
            return cur;
        }
};
class FibonacciProgression: public Progression{
    long prev;
    public:
        FibonacciProgression(): Progression(){}
        FibonacciProgression(long f, long s): Progression() {
            first = f;
            prev = f;
            cur = s;
        }
        long nextvalue(){
            long temp = cur;
            cur += prev;
            prev = temp;
            return cur;
        }
};
int main(){
    ArithProgression arith(1);
    cout<<"Arithmetic progression first 10 values\n";
    cout<<arith.firstvalue()<<" ";
    for(int i = 0; i < 9; i++) cout<<arith.nextvalue()<<" ";
    cout<<endl;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog