Write a program that demonstrates default
argumented constructor function. The program uses the following properties –
class names- staff; private members: char *name, char gender, int age, long
salary ; public: staff( char
*n, char g, int a, long s), details(); Constructor function staff( char *n, char g, int a=40, long s=4500) sets
the values from object and with default values. details() function displays
staff details as name, gender, age salary. The program uses two parameterized
objects.First object displays staff details with all default values. Second
object displays staff details with all new values.
#include<iostream>
using
namespace std;
class staff
{
char *name;
char gender;
int age;
long salary;
public:
staff(char *n,char g,int a,long s);
void details();
};
staff::staff(char
*n,char g,int a=40,long s=4500)
{
name=n;
gender=g;
age=a;
salary=s;
}
void
staff::details()
{
cout<<"Name is :
"<<name<<endl;
cout<<"Gender is :
"<<gender<<endl;
cout<<"Age is :
"<<age<<endl;
cout<<"Salary is :
"<<salary<<endl;
}
int main()
{
staff s1("solaiman",'M');
s1.details();
staff s2("Ratan",'M',20,15000);
s2.details()
return 0;
}
No comments:
Post a Comment