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.
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;
Comments
Leave a comment