Viết chương trình C++ để hiển thị thông tin về sinh viên: id, điểm thi hai môn, điểm thể chất, tổng điểm, và điểm trung bình.
Dưới đây là chương trình C++ để giải bài tập trên. Mình sử dụng tính kế thừa trong C++, và thực hiện theo các bước:
#include<iostream>
#include<conio.h>
using namespace std;
class sinhvien
{
protected:
int mssv,diemkt1,diemkt2;
public:
void nhapdiemkiemtra()
{
cout<<"Nhap MSSV: ";
cin>>mssv;
cout<<"Nhap hai diem: ";
cin>>diemkt1>>diemkt2;
}
};
class gdthechat
{
protected:
int diemtc; // la diem GDTC
public:
void nhapdiemgdtc()
{
cout<<"\nNhap diem GDTC :";
cin>>diemtc;
}
};
class thongtinsv:public sinhvien,public gdthechat
{
int tongdiem,trungbinh;
public:
void display()
{
tongdiem=(diemkt1+diemkt2+diemtc);
trungbinh=tongdiem/3;
cout<<"\n\n\tMSSV : "<<mssv<<"\n\tTong diem : "<<tongdiem;
cout<<"\n\tDiem trung binh : "<<trungbinh;
}
};
int main()
{
thongtinsv obj;
obj.nhapdiemkiemtra();
obj.nhapdiemgdtc();
obj.display();
return 0;
}
```Chạy chương trình C++ trên sẽ cho kết quả như hình sau:
![Bài tập về Tính kế thừa trong C++](../bai_tap_cplusplus_co_giai/images/bai_tap_tinh_ke_thua_trong_cplusplus.PNG)" style="text-align: center"> <script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle" data-ad-client="ca-pub-2373417360267295" data-ad-slot="1837799362" style="display:inline-block;width:336px;height:280px"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
Unpublished comment
Viết câu trả lời