How to use constructor overloading in C++

1 Answer

0 votes
#include <iostream>

class Rectangle {
public:
    int x, y; 
    
    Rectangle();
    Rectangle(int, int);
    
    void Show();
};
    
Rectangle::Rectangle() { x = 30; y = 14; }
Rectangle::Rectangle(int a, int b) { x = a; y = b; }

void Rectangle::Show() {
    std::cout << x << " " << y;
}

int main() {
    Rectangle r1;
    r1.Show();
    
    std::cout << "\n";
    
    Rectangle r2(5, 9);
    r2.Show();
}




/*
run:

30 14
5 9

*/

 



answered Dec 1, 2022 by avibootz

Related questions

2 answers 334 views
1 answer 151 views
151 views asked Dec 2, 2022 by avibootz
1 answer 170 views
170 views asked Dec 10, 2020 by avibootz
1 answer 232 views
1 answer 253 views
1 answer 220 views
...