You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<iostream>usingnamespacestd;classNum{
public:int value;
Num(int a) {
value = a;
}
};
intmain() {
Num a1(45);
cout << "The value is = " << a1.value << endl;
return0;
}
Example Two
// Multiple Object with Parameters
#include<iostream>usingnamespacestd;classNum{
public:int value;
Num(int a) {
value = a;
}
};
intmain() {
Num a1(45);
Num a2(4);
Num a3(5);
Num a4(245);
cout << "The value is = " << a1.value << endl;
cout << "The value is = " << a2.value << endl;
cout << "The value is = " << a3.value << endl;
cout << "The value is = " << a4.value << endl;
return0;
}
Example Three
//Use of multiple parameter s
#include<iostream>usingnamespacestd;classStudent{
string name;
int roll;
public:Student(string stu_name) {
name = stu_name;
roll = 12;
}
Student(int stu_roll) {
name = "Sita";
roll = stu_roll;
}
Student(string stu_name, int stu_roll) {
name = stu_name;
roll = stu_roll;
}
voiddisplay() {
cout << "Name = " << name << endl;
cout << "Roll no. = " << roll << endl;
}
};
intmain() {
Student s1("Mercury");
Student s2(17);
Student s3("Nikhil",18);
s1.display();
s2.display();
s3.display();
return0;
}
Important Question
1. What is Constructor? Differenciate default constructor and parameterized constructor.