How to Print Diamond Shape with OOP Classes and objects
How to Print Diamond Shape with OOP Classes and objects
In this tutorial, we will code the program of printing the Diamond Shape with OOP Classes and objects.
Program to Print Diamond Shape with Classes and objects
Here, we have a code of Program to Print Diamond Shape with Classes and objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include<iostream> using namespace std; class DiamodShape { public : int dimond() { int n, c, k, space=1; cout<<"Enter number of rows (for diamond dimension) : "; cin>>n; space=n-1; for (k=1; k<=n; k++) { for(c=1; c<=space; c++) { cout<<" "; } space--; for(c=1; c<=(2*k-1); c++) { cout<<"*"; } cout<<"\n"; } space=1; for(k=1; k<=(n-1); k++) { for(c=1; c<=space; c++) { cout<<" "; } space++; for(c=1 ; c<=(2*(n-k)-1); c++) { cout<<"*"; } cout<<"\n"; } } }; int main() { DiamodShape dimondDiamodShape; dimondDiamodShape.dimond(); } |
Output
Program to Print Diamond Shape with Constructor
Here, we have a code of Program to Print Diamond Shape with the constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include<iostream> using namespace std; class shape { public : shape() { int n, c, k, space=1; cout<<"Enter number of rows (for diamond dimension) : "; cin>>n; space=n-1; for (k=1; k<=n; k++) { for(c=1; c<=space; c++) { cout<<" "; } space--; for(c=1; c<=(2*k-1); c++) { cout<<"*"; } cout<<"\n"; } space=1; for(k=1; k<=(n-1); k++) { for(c=1; c<=space; c++) { cout<<" "; } space++; for(c=1 ; c<=(2*(n-k)-1); c++) { cout<<"*"; } cout<<"\n"; } } }; int main() { shape dimondshape; } |
Output
Program to Print Diamond Shape with Destructor
Here, we have a code of Program to Print the Diamond Shape with the destructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#include<iostream> using namespace std; class shape { public : shape() { int n, c, k, space=1; cout<<"Enter number of rows (for diamond dimension) : "; cin>>n; space=n-1; for (k=1; k<=n; k++) { for(c=1; c<=space; c++) { cout<<" "; } space--; for(c=1; c<=(2*k-1); c++) { cout
<<"*"; } cout<<"\n"; } space=1; for(k=1; k<=(n-1); k++) { for(c=1; c<=space; c++) { cout<<" "; } space++; for(c=1 ; c<=(2*(n-k)-1); c++) { cout<<"*"; } cout<<"\n"; } } ~shape() { int n, c, k, space=1; cout<<"Enter number of rows (for diamond dimension) : "; cin>>n; space=n-1; for (k=1; k<=n; k++) { for(c=1; c<=space; c++) { cout<<" "; } space--; for(c=1; c<=(2*k-1); c++) { cout<<"*"; } cout<<"\n"; } space=1; for(k=1; k<=(n-1); k++) { for(c=1; c<=space; c++) { cout<<" "; } space++; for(c=1 ; c<=(2*(n-k)-1); c++) { cout<<"*"; } cout<<"\n"; } } }; int main() { shape dimondshape; } |
Output
Program to Print Diamond Shape with inheritance (Parent and Child Class)
Here, we have a code of Program to Print Diamond Shape with inheritance (Parent and Child Class).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include<iostream> using namespace std; class shape { protected: int n, c, k, space=1; }; class dimond : public shape { public : int dimonds() { int n, c, k, space=1; cout<<"Enter number of rows (for diamond dimension) : "; cin>>n; space=n-1; for (k=1; k<=n; k++) { for(c=1; c<=space; c++) { cout<<" "; } space--; for(c=1; c<=(2*k-1); c++) { cout<<"*"; } cout<<"\n"; } space=1; for(k=1; k<=(n-1); k++) { for(c=1; c<=space; c++) { cout<<" "; } space++; for(c=1 ; c<=(2*(n-k)-1); c++) { cout<<"*"; } cout<<"\n"; } } }; int main() { dimond dimondshape; dimondshape.dimonds(); } |
Output