e có 1 class thisinh, trong đó có hàm khởi tạo , giải sử thisinh(x,y,z)
trong hàm main e khai báo
thisinh *ts
ts= new thisinh[n]
với n được nhập từ bàn phím, giả sử n đươc nhập là 5.
bây giờ e muốn khởi tạo cho i=3 thì làm sao ạ, e làm như thế này theo cú pháp thì báo lỗi tại i,
thisinh a[i](x,y,z)
các ac có thể giải thích cho e được k ạ, e mới học class, e cảm ơn nhiều.!!!
e thêm code của e
#include<iostream>
using namespace std;
class thisinh {
private:
char *ten;
float toan;
float ly;
float hoa;
public:
thisinh(); // khởi tạo không tham số
thisinh(char *ht,float t,float l,float h); // hàm khởi tạo có tham số
float xuattoan();
float xuatly();
float xuathoa();
float tong();
~thisinh();
};
thisinh::thisinh() {
ten = new char[50];
ten = "";
toan = 0;
ly = 0;
hoa = 0;
}
thisinh::thisinh(char *ht, float t, float l, float h) {
ten = new char[50];
ten = ht;
toan = t;
ly = l;
hoa = h;
}
float thisinh::xuattoan() {
return toan;
}
float thisinh::xuatly() {
return ly;
}
float thisinh::xuathoa() {
return hoa;
}
float thisinh::tong() {
return toan + ly + hoa;
}
thisinh::~thisinh() {
delete [] ten;
}
int main() {
thisinh *a;
int n;
float toan, ly, hoa;
char *hoten;
hoten = new char[50];
cout << "Nhap so luong thi sinh: "; cin >> n;
cin.ignore();
a = new thisinh[n];
for (int i = 0; i < n; i++) {
cout << "Nhap thong tin sinh vien thu " << i + 1 << " : " << endl;
cout << "Nhap ten sinh vien: "; cin.getline(hoten, 50);
cout << "Nhap lan luot diem toan, ly, hoa: "; cin >> toan >> ly >> hoa;
thisinh a[i](hoten, toan, ly, hoa);
}
delete [] hoten;
delete [] a;
system("pause");
return 0;
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?