//There have 3 constructors with same name called overloading #include <bits/stdc++.h> using namespace std; class rectangle //class { private: int length; int breadth; public: rectangle() //non-parameterised constructor { length=5; breadth=4; } rectagle(int l=5,int b) //parameterised constructor { setLength(l); setBreadth(b); } rectangle(rectangle &r) //copy constructor { length=r.length; breadth=r.breadth; } void setLength(int l) { if(l>0) length=l; else length=1; } void setBreadth(int b) { if(b>0) breadth=b; else breadth=1; } int getLength() { return length; } int getBreadth(); //Define outside class with scope resolution int area() { return length*breadth; } int perimeter(); //Define outside class with scope resolution }; int rectangle::perimeter() //Define outside class with scope { // resolution return 2*(length+breadth); } int rectangle::getBreadth() //Define outside class with scope { //resolution return breadth; } int main() { rectangle r1; //object r1 r1.setLength(15); r1.setBreadth(2); rectangle r2(r1); //object r2 cout<<r1.area()<<"\n"; cout<<r1.perimeter()<<"\n"; cout<<r2.perimeter()<<"\n"; return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter