Class with Multiple Constructors
C++
Easy
4 views
Problem Description
Implement constructor overloading with default and parameterized
constructors. This teaches polymorphism basics.
Logic: Same function name but different parameters
Official Solution
class Box {
public:
int side;
Box() {
side = 1;
}
Box(int s) {
side = s;
}
int getVolume() {
return side * side * side;
}
};
void question3_constructor_overload() {
Box b1;
Box b2(5);
cout << "Box1 volume: " << b1.getVolume() << endl;
cout << "Box2 volume: " << b2.getVolume() << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!