Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
PrintShapePointer
//g++ 7.4.0 //Print Shape Pointer: printing different shapes based on choice of user and with help of array pointer // code, except triangle, is created by Rezaul Hoque on April 20,2021 //Triangle code credit goes to Simple Snippet Tech //please contact at jewelmrh@yahoo.com #include <iostream> using namespace std; class Object { public: virtual void print() const {cout<<"No object is created.\n";} }; class Rectangle : public Object { public: void print() const { for(int i=0; i<5;i++) { for (int j=0; j<8;j++) cout <<"#"; cout <<"\n"; } } }; class Square : public Object { public: void print() const { for(int i=0; i<5;i++) { for (int j=0; j<5;j++) cout <<"#"; cout <<"\n"; } } }; class Triangle: public Object {// code credit Simple Snippet Tech public: void print () const { for (int i = 1; i <= 5; i++) { for (int p = 5- i; p> 0; p--) { cout << " "; } for (int j = 1; j <= i; j++) { cout << "# "; } cout << "\n"; } } }; int main (){ const int max =3; Object * a[max];//pointer array Object * point;//pointer to an instance of class Object type int choice; for (int i=0; i<3; i++) { cout <<" (1) Rectangle (2) Square (3)Triangle:\n"; cin>>choice; switch(choice)//based on choice of user the pointer indicates the correct derived class { case 1: point = new Rectangle; break; case 2: point = new Square; break; case 3: point = new Triangle; break; default: point = new Object ; break; } a[i]=point;//pointer array stores the pointers } for (int i=0;i<3;i++) a[i]->print(); return 0; }
run
|
edit
|
history
|
help
0
Identifying polimorphic types without using RTTI or type mappings
FindMissingLagrange
iviewb
Segment Tree
Prime calculator using bool
a ko z and z ko n string printing
InviteList
bc
26 და 28 მარტს დამუშავებული
Anagrams WIP