C++ 24:Constructor Overloading
1. Defining a multiple constructors in a same class is called as constructors overloading but these constructors differ in terms of number pf arguments and type of arguments.
2. An appropriate constructor will be automatically invoke as per the passed arguments.
Example 1:
#include <iostream>
using namespace std;class Circle{private: double R;
public: Circle(double r) { R = r; //Parameterised constructor } //Constructor overloading Circle() { R = 1; //default constructor }
double area() { return 3.14 * R * R; }
double circumference() { return 2 * 3.14 * R; }};int main(){ double a, c; double ab, cb; Circle c1(2); a = c1.area(); c = c1.circumference();
cout << "Area is " << a << endl; cout << "Circumference is " << c << endl;
Circle c2(); ab = c2.area(); cb = c2.circumference();
cout << "Area is " << ab << endl; cout << "Circumference is " << cb << endl;
return 0;}Output:
Enter your age: 56
You can voteDesign a class box having contractor overloading with two constructor one having 1 argument and another having 2 argument.
#include <iostream>
using namespace std;class Box{private: int L, B, H;
public: Box(int l, int b, int h) { L = l; B = b; H = h; } Box(int p) { L = B = H = p; } void volume() { cout << "The Volume of Box: " << L * B * H << " cubic units" << endl; } void surfaceArea() { cout << "Surface Area of Box: " << 2 * (L * B + B * H + L * H) << " square units" << endl; }};int main(){ Box b1(20); b1.volume(); b1.surfaceArea();
Box b2(10, 5, 20); b2.volume(); b2.surfaceArea();
return 0;}Output:
Enter your age: 56
You can voteConstructor with default Arguments:
Example:
Design a Class Distance with data member function Feet and Inches.
Define Member Function show data().The objects of class must be created by passing
two or three arguments.
#include <iostream>
using namespace std;class Distance{private: int I, F;
public: Distance(int f = 0, int i = 0) { F = f + (i / 12); i = i % 12; I = i; }
void showdata() { cout << "Distance is Feet: " << F << " And Inches: " << I << endl; }};
int main(){ Distance a(2, 16); a.showdata();
//with one arguments Distance b(3); b.showdata();
//with zero arguments Distance c; c.showdata();}Output:
Distance is Feet: 3 And Inches: 4
Distance is Feet: 3 And Inches: 0
Distance is Feet: 0 And Inches: 02.
#include <iostream>
using namespace std;class Circle{private: int R;
public: Circle(int r = 1) { R = r; }
void area() { float a = 3.14 * R * R; cout << "Area of Circle is " << a << endl; } void circumference() { float c = 3.14 * 2 * R; cout << "Circumference of Circle is " << c << endl; }};
int main(){
Circle a(5); a.area(); a.circumference();
Circle b; b.area(); b.circumference();}Output:
Area of Circle is 78.5
Circumference of Circle is 31.4
Area of Circle is 3.14
Circumference of Circle is 6.283. Define a class Box with member L,B,H and member Functions volume which
will return the volume .the constructor must be passed with 1 or 3
arguments
#include <iostream>
using namespace std;class Box{private: int L, B, H;
public: Box(int l, int b = 1, int h = 1) { L = l; B = b; H = h; } void volume() { cout << "Volume of Box is: " << L * B * H << endl; }};
int main(){
Box b1(2, 4, 6); b1.volume();
Box b2(2, 4); b2.volume();
Box b3(2); b3.volume();}Output:
Volume of Box is: 48
Volume of Box is: 8
Volume of Box is: 24. Design a class Currency with data members Rupees and paise. Define member
Function showdata().The object can be created by passing 1,2 or 0 arguments.
#include <iostream>
using namespace std;class Currency{
private: int R, P;
public: Currency(int r = 0, int p = 0) { R = r; P = p; } void showdata() { R += P / 100; P = P % 100;
cout << "Rupees: " << R << " Paise: " << P << endl; }};
int main(){ Currency a(20, 120); a.showdata();
Currency b(200); b.showdata();
Currency c; c.showdata();}Output:
Rupees: 21 Paise: 20
Rupees: 200 Paise: 0
Rupees: 0 Paise: 0

Comments
Post a Comment