Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
PointPattern
//g++ 7.4.0 //PointPattern: printing different patterns using pointer array //credits for triangle, half triangle, inverted triangle and inverted half triangle codes go to pencilprogrammer.com //codes for pointer arithmetics and test driver are created by Rezaul Hoque on September 30,2021;contact: jewelmrh@yahoo.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; #include <iostream> using namespace std; class Pattern { public: virtual void print() const {cout<<"No pattern is created.\n";} }; class Triangle : public Pattern{ public: void print () const { for (int i = 1; i <= 5; i++) { for (int p = 1; p<=5-i; p++) { cout << " "; } for (int j = 1; j <2*i; j++) { cout << " #"; } cout << "\n"; } } }; class DownTri : public Pattern{ public: void print () const { for (int i = 1; i <= 5; i++) { for (int p = 1; p<=i; p++) { cout <<" "; } for (int j = 1; j <= 2*(5-i)+1; j++) { cout <<" #"; } cout <<" \n"; } } }; class HalfTri: public Pattern { public: void print() const { for(int l=1; l<=5;l++){ for(int m=1;m<=5;m++){ if(l>=m) std::cout<<" #";} std::cout<<"\n";} } }; class DownHalfTri: public Pattern{ public: void print() const { for(int l=1; l<=5;l++){ for(int m=1;m<=5;m++){ if(l<=m) std::cout<<" #"; } std::cout<<" \n";} } }; int main (){ const int max =4; Pattern * a[max];//pointer array Pattern * point;//pointer to an instance of class Pattern type int choice; for (int i=0; i<4; i++) { cout <<" (1) Triangle (2) Inverted Triangle (3) Half Triangle (4)Inverted Half Triangle\n"; cin>>choice; switch(choice)//based on choice of user the pointer indicates the correct derived class { case 1: point = new Triangle; break; case 2: point = new DownTri; break; case 3: point = new HalfTri; break; case 4: point = new DownHalfTri; break; default: point = new Pattern; break; } a[i]=point;//pointer array stores the pointers } for (int i=0;i<4;i++) a[i]->print(); return 0; }
run
|
edit
|
history
|
help
0
带头结点的单链表
pow implementation
base and derr con
Romberg Integration
CS 201
AgeGroupHisto
3
string_match
Size of data type test
Stream2