Assignment
24
Write a C++ program using class which contains
two data members of type integer. Create and initialize the object using
default constructor, parameterized constructor and parameterized constructor
with default value. Write a member function to display maximum from given two
numbers for all objects.
Program
Code for run:
#include
<iostream>
using
namespace std;
class
ConstDemo
{
int n1, n2;
public:
ConstDemo()
{
n1 = n2 = 0;
}
ConstDemo(int a1 , int a2 = 0 )
{
if(a2 ==0)
a2 = a1;
n1 = a1;
n2 = a2;
}
void maxNumber()
{
if ( n1 > n2)
{
cout << ” n1 ( value
= ” << n1 << ” ) is greater “;
cout << “than n2 (
value = ” << n2 << ” )”;
}
else
if ( n2 > n1 )
{
cout << ” n2 (
value = ” << n2 << ” ) is greater “;
cout << “than n1
( value = ” << n1 << ” )”;
}
else
{
cout << ” Both
values n1 ( value = ” << n1 << ” ) “;
cout << “and n2
( value = ” << n2 << ” ) are same. “;
}
}
};
int
main()
{
while(true)
{
int n, n1, n2;
cout << “* * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<<endl;
cout << ” \t Accept two integer
data members ” << endl;
cout << “ Display maximum from two integer data
numbers ” << endl;
cout << “* * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<<endl;
cout << ” \t Initialize the
object using ” << endl;
cout << ” 1 : Default
Constructor \t 2 : Parameterized Constructor” << endl;
cout << “ 3 : Parameterized Constructor with default
value” << endl;
cout << “\t \t Exit :
Other” << endl;
cout << ” \t Choose Appropriate
option ” << endl;
cout << “* * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<<endl;
cout << ” \t “;
cin >> n;
switch(n)
{
case 1:
{
ConstDemo c1;
cout << “* * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<<endl;
cout << ” \t Display maximum
from two integer data numbers ” << endl;
cout << “* * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<<endl;
c1.maxNumber();
break;
}
case 2:
{
cout << ” \t Accept first
Parameter ” << endl << ” \t “;
cin >> n1;
cout << ” \t Accept Second
Parameter ” << endl << ” \t “;
cin >> n2;
ConstDemo c2( n1, n2);
cout << “* * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<<endl;
cout << “ Display maximum from two integer data
numbers ” << endl;
cout << “* * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<<endl;
c2.maxNumber();
break;
}
case 3:
{
cout << ” \t Accept One
Parameter ” << endl ;
cout << ” \t ( This
Parameter use as default Parameter )” << endl;
cout << ” \t “;
cin >> n1;
ConstDemo c3( n1);
cout << “* * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<<endl;
cout << ” \t Display maximum
from two integer data numbers ” << endl;
cout << “* * * * * * * * * *
* * * * * * * * * * * * * * * * * *”<<endl;
c3.maxNumber();
break;
}
default:
cout << ” \t Thank You to
Use this Program !”;
exit(0);
}
cout << endl;
cout << “* * * * * * * * * * * *
* * * * * * * * * * * * * * * *”<<endl;
cout << ” \t Are you want to
continue … ” << endl;
cout << “* * * * * * * * * * * *
* * * * * * * * * * * * * * * *”<<endl;
cout << ” \t IF Yes : 1 \t No :
Any other value ” << endl << ” \t “;
cin >> n;
if(n == 1)
{
continue;
}
else
{
cout << ” \t Thank You to
Use this Program !”;
exit(0);
}
}
}
Output
of Program:
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Accept two integer data members
Display maximum from two integer data
numbers
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Initialize the object using
1 : Default Constructor 2 :
Parameterized Constructor
3 : Parameterized Constructor with default
value
Exit : Other
Choose Appropriate option
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Display maximum from two integer data numbers
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Both values n1 ( value = 0 ) and n2 ( value =
0 ) are same.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Are you want to continue …
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
IF Yes : 1 No : Any other value
1
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Accept two integer data members
Display maximum from two integer data
numbers
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Initialize the object using
1 : Default Constructor 2 :
Parameterized Constructor
3 : Parameterized Constructor with default
value
Exit : Other
Choose Appropriate option
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
Accept first Parameter
24
Accept Second Parameter
48
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Display maximum from two integer data
numbers
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
n2 ( value = 48 ) is greater than n1 ( value =
24 )
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Are you want to continue …
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
IF Yes : 1 No : Any other value
1
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Accept two integer data members
Display maximum from two integer data
numbers
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Initialize the object using
1 : Default Constructor 2 :
Parameterized Constructor
3 : Parameterized Constructor with default
value
Exit : Other
Choose Appropriate option
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3
Accept One Parameter
( This Parameter use as default Parameter )
68
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Display maximum from two integer data numbers
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Both values n1 ( value = 68 ) and n2 ( value =
68 ) are same.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Are you want to continue …
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
IF Yes : 1 No : Any other value
1
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Accept two integer data members
Display maximum from two integer data numbers
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Initialize the object using
1 : Default Constructor 2 :
Parameterized Constructor
3 : Parameterized Constructor with default
value
Exit : Other
Choose Appropriate option
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4
Thank You to Use this Program !