#include "stdafx.h"
#include <iostream>
#include <string>
usingnamespace std;
class employee
{
private:
string _name1;
int _age;
float _salary;
public:
employee(); //default constructor
employee(string name, int a, float sal)
{
_name1 = name;
_age = a;
_salary = sal;
}
// Copy constructor
employee(const employee &cSource)
{
_name1 = cSource._name1;
_age = cSource._age;
_salary = cSource._salary;
}
employee::~employee()
{
cout<<"\n Deleting the info.\n";
_name1.erase();
//_age.erase();
//_salary.erase();
//Destructor will not delete an array of pointer.
//Destructor will be used to delete the members only.
// and that too pointer to members.
//We do not need to delete the age and salary as these are not pointers.
}
void display();
//float increment_the_salary(float _salary, float PercentageIncrease); // why salary has to be declared float again
};
void employee::display()
{
cout<<"\n Name is "<<_name1;
cout<<"\n age is "<<_age;
cout<<"\n salaray is "<<_salary;
}
/*
float employee::increment_the_salary(float _salary, float PercentageIncrease)
{
float _salary = _salary*(1+PercentageIncrease/100);
return _salary;
}*/
int main()
{
string name1;
int age, option, n = 0;
float salary;
//Create array of point
employee *EmployeeList[10];
cout<<"You can enter upto 10 employees \n";
//option = 1;
//Enter employees information
do
{
cout<<"Enter the name\n";
cin>>name1;
cout<<"Enter the age \n";
cin>>age;
cout<<"Enter the salary \n";
cin>>salary;
EmployeeList[n] = new employee;
employee temp(name1, age, salary);
*EmployeeList[n]= temp;
//employee *temp= new employee(name1, age, salary);
//EmployeeList[n] = temp;
++n;
cout<<"Do you enter one more employee \n";
cout<<"1 for Yes; 0 for No";
cin>>option;
}while(option);
//Destructor is not created
//explicitly destructing
//Now printing all employee's information
for (int i=0; i<n; ++i)
{
EmployeeList[i]->display();
delete EmployeeList[i];
}
system("pause");
return 0;
}
Are you calling an undefined function anywhere? Is your function definition getting compiled/linked properly? Its hard to tell whats going on without the complete error message.
class employee
{
private:
string _name1;
int _age;
float _salary;
public:
employee(); //default constructor (never implemented)
employee(string name, int a, float sal)
{
_name1 = name;
_age = a;
_salary = sal;
}
employee(const employee &cSource) //does the default behaviour (no need)
{
_name1 = cSource._name1;
_age = cSource._age;
_salary = cSource._salary;
}
employee::~employee()
{
cout<<"\n Deleting the info.\n";
//_name1.erase(); //no need. When the class die, all its elements die
//You need to handle here the life time of raw pointers
}
void display();
//float increment_the_salary(float _salary, float PercentageIncrease); // why salary has to be declared float again
};
employee *EmployeeList[10]; I hope you've got a good reason (polymorphism) to use pointers. However, ¿why an array instead of a vector? (especially since you are keeping track of the size).
1 2 3 4
//EmployeeList[n] = new employee;
//employee temp(name1, age, salary);
//*EmployeeList[n]= temp;
EmployeeList[n] = new employee(name1, age, salary);
EmployeeList[n] = new employee; //the employee must have a default constructor.
employee temp(name1, age, salary);
*EmployeeList[n]= temp; //copying the object (discarding what the default constructor did)
The copy could be inefficient, and a class could not have a default constructor.
Besides EmployeeList[n] = new employee(name1, age, salary); is more compact and is clear what is the result (don't have to look up to what is temp)
The question remains, ¿why do you use pointers?
The idea is that a vector can growth dynamically, but you don't need to worry about memory management. http://www.cplusplus.com/reference/stl/vector/ std::vector<employee> EmployeeList; creating a vector EmployeeList[n]; accessing an element (the index must be valid, between 0 and size-1) EmployeeList.push_back( employee(name1, age, salary) ); adding an employee (increasing the size) Employee.size(); checking the size