write a program to displaying multiple constrictor
Answers
Answer:
IN JAVA -
class NumberValue {
private int num;
public NumberValue() {
num = 6;
}
public NumberValue(int n) {
num = n;
}
public void display() {
System.out.println("The number is: " + num);
}
}
public class Demo {
public static void main(String[] args) {
NumberValue obj1 = new NumberValue();
NumberValue obj2 = new NumberValue(15);
obj1.display();
obj2.display();
}
}
IN C++ -
#include <iostream>
using namespace std;
class construct
{
public:
float area;
// Constructor with no parameters
construct()
{
area = 0;
}
// Constructor with two parameters
construct(int a, int b)
{
area = a * b;
}
void disp()
{
cout<< area<< endl;
}
};
int main()
{
// Constructor Overloading
// with two different constructors
// of class name
construct o;
construct o2( 10, 20);
o.disp();
o2.disp();
return 1;
}
Explanation: