Thứ Năm, 8 tháng 3, 2012

Xây dựng lớp Nhanvien trong C+

Xây dựng lớp nhanvien có các thành phần sau:
- Các thuộc tính: tendem, ho, ngaysinh, ngayvaolam. Chú ý : sử dụng con trỏ char cho tendem, ho và ngaysinh, ngayvaolam có kiểu lớp date1 vừa xây dựng ở bài tập trước.
- Hàm tạo
- Hàm trả về tendem
- Hàm trả về ho
- Hàm huỷ
- Hàm hiển thị

File employee.h

#ifndef employee1_h
#define employee1_h
#include "date1.h"
class employee1
{
  private :
    char * firstname;
    char * lastname;
    date1 * birthdate;
    date1 * hiredate;
  public:
    employee1(char *,char *,int,int,int,int,int,int);
    void print();
    char *get_firstname();
    char *get_lastname();
    ~employee1();
};
#endif

File employee.cpp

#include <iostream.h>
#include <conio.h>
#include "employee1.h"
#include <string.h>
#include "date1.h"
 
employee1::employee1(char * fn,char *ln,int bd,int bm,int by,int hd,int hm,int hy)
{
  firstname=new char[strlen(fn)+1];
  strcpy(firstname,fn);
 
  lastname=new char[strlen(fn)+1];
  strcpy(lastname,ln);
  birthdate=new date1(bd,bm,by);
  hiredate=new date1(hd,hm,hy);
 
}
char *employee1::get_firstname()
{
    return firstname;
}
 
char *employee1::get_lastname()
{
    return lastname;
}
 
employee1::~employee1()
{
  cout<<"ham huy duoc goi!"<<endl;
  delete firstname;
  delete lastname;
  delete birthdate;
  delete hiredate;
  getch();
}
 
void employee1::print()
{
  cout << " Ho va ten la:"<<lastname<<"  " << firstname <<endl;
  cout<<" Ngay sinh la:";
   birthdate->print();
  cout<<" Ngay dau di lam la:";
  hiredate->print();
}
 
void main()
{
  employee1 e(" Van Dong "," Pham ",3,4,1983,4,3,2004);
  e.print();
  employee1 * e1=new employee1(" Van A ", " Nguyen ",20,4,1988,7,9,2006);
  cout<<"Ten la:"<<e1->get_firstname()<<endl;
  delete e1;
  getch();
}
Share:

0 nhận xét:

Đăng nhận xét