Bài 1: Sinh viên. Mã: #include<iostream> using namespace std; class sinhvien { char hoten[20]; char masv[20]; float d1,d2,d3; public: void nhap(); void xuat(); float tongdiem(); friend void sapxep(sinhvien a[], int n); }; void sinhvien::nhap() { cin.ignore(); cout<<" Nhap Hoten: "; gets(hoten); cout<<" Nhap MaSV: "; gets(masv); cout<<" Nhap D1: ";cin>>d1; cout<<" Nhap D2: ";cin>>d2; cout<<" Nhap D3: ";cin>>d3; } void sinhvien::xuat() { cout<<"Sinh vien: "<<hoten<<endl; cout<<"Co ma la: "<<masv<<endl; cout<<"Diem 1 la: "<<d1<<endl; cout<<"Diem 2 la: "<<d2<<endl; cout<<"Diem 3 la: "<<d3<<endl; } float sinhvien::tongdiem() { return (d1+d2+d3)/3; } void sapxep(sinhvien a[],int n) { sinhvien x; for(int i=0;i<n;i++) { int m=i; for(int j=i+1;j<=n;j++) if(strcmp(a[j].hoten,a[m].hoten)<0) m=j; if(m!=i) { x=a[m]; a[m]=a[i]; a[i]=x; } } } void main () { sinhvien a[50]; int n; cout<<" Nhap so luong sinh vien: ";cin>>n; for(int i=0;i<n; i++ ) { cout<<"Nhap sinh vien thu: "<<i<<"\t"<<endl; a[i].nhap(); } sapxep(a,n); for(int j=0;j<n;j++) { a[j].xuat(); cout<<" Tong diem la: "<<a[j].tongdiem()<<endl; } } Bài 2: Hình chữ nhật, Mã: #include <iostream> using namespace std; class hcn { double d, r; public: void Nhap(double, double); void Chuvi(); void Dientich(); }; void hcn::Nhap(double x, double y) { d=x; r=y; } void hcn::Chuvi() { cout<<"Chu vi = "<<(d+r)*2<<endl; } void hcn::Dientich() { cout<<"Dien tich = "<<d*r<<endl; } void main() { double a,b; hcn h; cout<< " Nhap chieu dai : "; cin>>a; cout<< " Nhap chieu rong : "; cin>>b; h.Nhap(a,b); h.Chuvi(); h.Dientich(); } Bài 3: Hình tròn. Mã: #include<iostream> using namespace std; class hinhtron { private: int r; public: void nhap(); void xuat(); float chuvi(); float dientich(); }; void hinhtron::nhap() { cout<<"Nhap ban kinh: ";cin>>r; } void hinhtron::xuat() { cout<<" Chu Vi cua hinh tron la: "<<chuvi()<<endl; cout<<" Dien Tich cua hinh tron la: "<<dientich()<<endl; } float hinhtron::chuvi() { return 2*3.14*r; } float hinhtron::dientich() { return 3.14*r*r; } void main() { hinhtron i; i.nhap(); i.xuat(); i.chuvi(); i.dientich(); } Bài 4: Nhân viên Mã: #include<iostream> using namespace std; class nhanvien { private: char hoten[50]; float phucap; float hesoluong; public: void nhap(); float luong(); void in(); }; void nhanvien::nhap() { cout<<"Nhap ho ten: "; gets(hoten); cout<<"Nhap he so luong: "; cin>>hesoluong; cout<<"Nhap phu cap: "; cin>>phucap; } float nhanvien::luong() { return 730*(hesoluong+phucap); } void nhanvien::in() { cout<<hoten<<" co muc luong duoc nhan la: "<<luong(); } void main() { nhanvien t; t.nhap(); t.in(); }
Hình lập phuơng Mã: #include<iostream> using namespace std; class lp { int d,r,x; public: void nhap(int,int,int); void s(); void v(); }; void lp::nhap(int a,int b,int c) { d=a; r=b; x=c; } void lp::s() { cout<<endl<<"dien tich la"<<"\t"<<d*r*6; } void lp::v() { cout<<endl<<"the tich la"<<"\t"<<d*r*x; } void main() { lp h1; int dai,rong,cao; cout<<"nhap dai"<<endl; cin>>dai; cout<<"nhap rong"<<endl; cin>>rong; cout<<"nhap cao "<<endl; cin>>cao; h1.nhap(dai,rong,cao); h1.s(); h1.v(); cout<<endl; } Hình Cầu Mã: #include <iostream> #include <cmath> using namespace std; double pi=3.1415; class hinhcau { double r; public: void nhapbk(double bk) { r=bk; } void thetich() { cout<<" the tich cua hinh cau : "<<(4/3)*pi*r*r*r<<endl; } void dientich() { cout<<" dien tich cua hinh cau: "<<4*pi*r*r<<endl; } }; void main() { double R; hinhcau hc; cout<<"nhap ban kinh cua hinh cau: "; cin>>R; hc.nhapbk(R); hc.thetich(); hc.dientich(); }