In: Computer Science
I have taken example of employee.
emp.h
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class employee
{
private: // private data members
int id;
char name[20];
int age;
char gen[5];
int sal;
public:
employee(); //default constructor
employee(int,const char*,int,const char*,int);
//parameterized constructor
employee(int,int,const char*,const char*,int);
//overloaded parameterized constructor
employee(int,int,int,const char*,const char*);
//overloaded parameterized constructor
void display(); //display function to display all attributes
};
emp.cpp
#include"emp.h"
employee:: employee()
{
this->id=0;
strcpy(this-> name,"\0");
this->age=0;
strcpy(this->gen,"\0");
this->sal=0;
}
employee::employee(int id,const char* name,int age,const char* gen,int sal)
{
this->id=id;
strcpy(this-> name,name);
this->age=age;
strcpy(this->gen,gen);
this->sal=sal;
}
employee::employee(int id,int age,const char* name,const char*
gen,int sal)
{
this->id=id;
this->age=age;
strcpy(this-> name,name);
strcpy(this->gen,gen);
this->sal=sal;
}
employee::employee(int id,int age,int sal,const char* name,const
char* gen)
{
this->id=id;
this->age=age;
this->sal=sal;
strcpy(this-> name,name);
strcpy(this->gen,gen);
}
void employee::display()
{
cout<<"id is: "<<id<<endl;
cout<<"name is:"<<name<<endl;
cout<<"age is: "<<age<<endl;
cout<<"gender is:
"<<gen<<endl;
cout<<"salary is: "<<sal<<endl;
}
main.cpp
#include"emp.h"
int main()
{
employee empl1(123,"vishal",24,"M",45000);
//parameterized constructor
cout<<"Costructor 1: "<<endl;
empl1.display();
cout<<endl;
employee empl2(124,24,"mohit","M",55000); //overloaded
constructor
cout<<"Overloaded Costructor called :
"<<endl;
empl2.display();
cout<<endl;
employee empl3(125,24,45000,"sanjana","F");
//overloaded constructor
cout<<"Overloaded Costructor called :
"<<endl;
empl3.display();
_getch();
return 0;
}
======================OUTPUT==================
//Please upvote!!!