Dùng tongD ở trên cho hàm xuat()

Làm thế nào để có thể dùng tongD ở trên cho hàm xuat() ạ

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <string.h>

using namespace std;

class SINHVIEN
{
private:
    int maSV;
    char hoTen[20];
    int diemToan;
    int diemLy;
    int diemHoa;
public:
    void nhap();
    int tongDiem();
    void sapxep();
    void xuat();
};

void SINHVIEN::nhap()
{
    cout<<"Nhap ma sinh vien:"; cin>>maSV;
    cout<<"Nhap ho va ten:";    fflush(stdin);  gets(hoTen);
    cout<<"Nhap diem Toan:"; cin>>diemToan;
    cout<<"Nhap diem Ly:"; cin>>diemLy;
    cout<<"Nhap diem Hoa:"; cin>>diemHoa;
}
int SINHVIEN::tongDiem()
{
    int tongD=diemHoa+diemLy+diemToan;
}
void SINHVIEN::sapxep()
{

}
void SINHVIEN::xuat()
{
    cout<<maSV<<setw(20)<<hoTen<<setw(20)<<tongD<<endl;
}
int main()
{
    int n;
    cout<<"Nhap so SV:"; cin>>n;
    SINHVIEN a[50];
    for(int i=0;i<n;i++)
    {
        cout<<"Nhap thong tin sinh vien thu "<<i+1<<":"<<endl;
        a[i].nhap();
    }
    cout<<"========Thong tin sinh vien ======="<<endl;
    cout<<"Ma SV"<<setw(20)<<"Ho Ten"<<setw(20)<<"Tong Diem"<<endl;
    for(int i=0;i<n;i++)
    {
        a[i].xuat();
    }

}

Khai báo nó là biến của lớp, không phải biến của hàm. Có thể xem là toàn cục (lớp).

2 Likes

làm thế nào để truy xuất được ạ?

Bạn hãy nhìn kỹ lại cách khai báo và implement hàm tongDiem của bạn xem

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