Em có đoạn code sau:
#include <iostream>
#include <cstring>
#include "Date.cpp"
using namespace std;
class ConNguoi{
private:
char * HoTen;
char * GioiTinh;
Date NamSinh;
char * NoiSinh;
char * DiaChi;
public:
ConNguoi();
ConNguoi(const ConNguoi& tmp);
ConNguoi& operator = (const ConNguoi& tmp){
delete [] HoTen;
delete [] GioiTinh;
delete [] NoiSinh;
delete [] DiaChi;
HoTen = new char[50];
strcpy(HoTen, tmp.HoTen);
GioiTinh = new char[5];
strcpy(GioiTinh, tmp.GioiTinh);
NamSinh = tmp.NamSinh;
NoiSinh = new char[100];
strcpy(NoiSinh, tmp.NoiSinh);
DiaChi = new char[100];
strcpy(DiaChi, tmp.DiaChi);
return *this;
};
~ConNguoi();
friend ostream& operator << (ostream& os, ConNguoi tmp);
friend istream& operator >> (istream& is, ConNguoi& tmp);
};
ConNguoi::ConNguoi():NamSinh(1,1,2000){
HoTen = new char[50];
strcpy(HoTen, "");
GioiTinh = new char[5];
strcpy(GioiTinh, "");
NoiSinh = new char[100];
strcpy(NoiSinh, "");
DiaChi = new char[100];
strcpy(DiaChi, "");
}
ConNguoi::ConNguoi(const ConNguoi& tmp):NamSinh(tmp.NamSinh){
HoTen = new char[50];
strcpy(HoTen, tmp.HoTen);
GioiTinh = new char[5];
strcpy(GioiTinh, tmp.GioiTinh);
NoiSinh = new char[100];
strcpy(NoiSinh, tmp.NoiSinh);
DiaChi = new char[100];
strcpy(DiaChi, tmp.DiaChi);
}
ConNguoi::~ConNguoi(){
delete [] HoTen;
delete [] GioiTinh;
delete [] NoiSinh;
delete [] NoiSinh;
}
ostream& operator << (ostream& os, ConNguoi tmp){
os << "Ho ten " << tmp.HoTen;
os << "Gioi tinh " << tmp.GioiTinh;
os << "Nam sinh "; tmp.NamSinh.Xuat();
os << "Noi sinh " << tmp.NoiSinh;
os << "Dia chi " << tmp.DiaChi;
return os;
}
istream& operator >> (istream& is, ConNguoi& tmp){
cout << "Nhap ho ten " << endl;
is.getline(tmp.HoTen, 50);
cout << "Gioi tinh " << endl;
is.getline(tmp.GioiTinh, 5);
cout << "Nam sinh " << endl;
is >> tmp.NamSinh;
cout << "Noi sinh " << endl;
is.getline(tmp.NoiSinh, 100);
cout << "Dia chi " << endl;
is.getline(tmp.DiaChi, 100);
return is;
}
class GiaoVien: public ConNguoi{
private:
Date NgayVaoTruong;
char * ChuyenMon;
public:
GiaoVien():ConNguoi(){
ChuyenMon = new char[30];
strcpy(ChuyenMon, "");
}
GiaoVien(const GiaoVien& tmp):ConNguoi(tmp){
NgayVaoTruong = tmp.NgayVaoTruong;
ChuyenMon = new char[30];
strcpy(ChuyenMon, tmp.ChuyenMon);
}
GiaoVien& operator = (const GiaoVien& tmp){
NgayVaoTruong = tmp.NgayVaoTruong;
delete [] ChuyenMon;
ChuyenMon = new char[30];
strcpy(ChuyenMon, tmp.ChuyenMon);
return *this;
};
~GiaoVien();
friend ostream& operator << (ostream& os, GiaoVien tmp);
friend istream& operator >> (istream& is, GiaoVien& tmp);
};
GiaoVien::~GiaoVien(){
delete [] ChuyenMon;
}
ostream& operator << (ostream& os, GiaoVien tmp){
cout << tmp;
os << "Ngay vao truong " << tmp.NgayVaoTruong;
os << "Chuyen mon " << tmp.ChuyenMon;
return os;
}
istream& operator >> (istream& is, GiaoVien& tmp){
cin >> tmp;
cout << "Ngay vao truong " << endl;
is >> tmp.NgayVaoTruong;
cout << "Chuyen mon " << endl;
is.getline(tmp.ChuyenMon, 30);
return is;
}
class SinhVien: public ConNguoi{
private:
int DiemVan;
int DiemToan;
int DiemNgoaiNgu;
public:
};
int main(){
GiaoVien a;
cin >> a;
cout << a;
return 0;
}
Đừng quan tâm tới lớp sinh viên.
Vấn đề là em khởi tạo được thuộc tính NamSinh trong lớp ConNguoi mà em không khởi tạo được thuộc tính NgayVaoTruong trong lớp GiaoVien em cũng khởi tạo như lớp ConNguoi mà không được. Có thánh nào pro vào hướng dẫn em với.



83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?