k4info
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

Bài thực hành buổi 6 Môn: Lập Trình Hướng Đối Tượng GV: Lê Thị Thu Lan

3 posters

Go down

Cool Bài thực hành buổi 6 Môn: Lập Trình Hướng Đối Tượng GV: Lê Thị Thu Lan

Bài gửi by augustion_ptn Wed Oct 05, 2011 7:53 pm

1. Tái định nghĩa tác tử + hai phân số bằng hàm độc lập
Khai báo class 2 phân số, tái định nghĩa tác tử + hai phân số x, y để ra kết quả là 1 phân số a = x+y
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream>
using namespace std;
class phanso
{
      private:
              int tu, mau;
      public:
            phanso(int tu=0,int mau=1)
            {
                        this->tu=tu;
                        this->mau=mau;
            };
            void inphanso()
            {
                  cout<<tu<<"/"<<mau;
            };
//Khai bao ham dinh nghia toan tu + la friend de co the truy cap den cac thanh phan private cua phanso
friend phanso operator + (phanso,phanso);
};
phanso operator + (phanso x, phanso y)
{
      phanso a;
      a.tu=((y.tu*x.mau)+(x.tu*y.mau));
      a.mau=(y.mau*x.mau);
      return a;
};
int main()
{
    phanso a(1,2), b(3,4);
    phanso c=b+a; //Thuc hien cong 2 phan so voi phep cong binh thuong
    a.inphanso();
    cout<<"+";
    b.inphanso();
    cout<<"=";
    c.inphanso();
    getch();
}
Bài tự làm:
Tiếp tục phát triển đoạn code trên với toán tử trừ (-), nhân (*), chia (/) hai phân số
Biết rằng công thức:
- Trừ 2 phân số
- Nhân 2 phân số
- Chia 2 phân số
[You must be registered and logged in to see this link.]
Kết quả
[You must be registered and logged in to see this link.]
Ghi chú:
Friend phá vỡ tính bao gói của Class. Để hạn chế việc này có thể thực hiện tái định nghĩa tác tử bằng hàm thành viên.
2. Tái định nghĩa tác tử + hai phân số bằng hàm thành viên
Khai báo class 2 phân số, tái định nghĩa tác tử + hai phân số x, y để ra kết quả là 1 phân số a = x+y
Code:
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<string.h>
using namespace std;
class PhanSo
{
            private:
                    int tu, mau;
            public:
                    PhanSo(int tu=0, int mau=1)
                    {
                              this->tu = tu;
                              this->mau = mau;
                    };
void InPhanSo()
{
    cout<<tu<<"/"<<mau;
};
PhanSo
PhanSo operator + (PhanSo y)
{
      PhanSo a;
      a.tu = ((y.tu * this->mau) + (this->tu * y.mau));
      a.mau = (y.mau * this->mau);
      return a;
}
};
int main()
{
    PhanSo a(1,2), b(3,4);
    PhanSo c = b + a;
    a.InPhanSo(); cout<<"+"; b.InPhanSo(); cout<<"="; c.InPhanSo();
    getch();
}
[You must be registered and logged in to see this link.]
Bài tự làm:
Tiếp tục phát triển đoạn code trên với toán tử trừ (-), nhân (*), chia (/) hai phân số
3. Tái định nghĩa tác tử xuất nhập
Xây dựng Class Diem
Tái định nghĩa tác tử xuất (<<) nhập (>>) cho một loạt thông tin của một đối tượng
Tác tử này sẽ được thay thế cho hàm NhapTT(), InTT() của Class
Code:
#include <stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
class diem
{
      int x;
      int y;
      public:
            diem();
            /* -- theo cach truyen thong
            void NhapTT();
            void InTT();
            */
            // rut gon lenh khi su dung << thay cho InTT va >> cho NhapTT
            // khai bao ham thanh vien .. co the truy cap cac thanh phan private
            friend ostream& operator << (ostream& os, diem p);
            friend istream& operator >> (istream& is, diem& p);
};
diem::diem()
{
      x=y=0;
}
/*
void diem::NhapTT()
{
    cout<<"Nhap hoanh do:";
    cin>>x;
    cout<<"Nhap tung do:";
    cin>>y;
};
*/
// viet lai ham NhapTT bang tac tu >>
istream& operator >> (istream& is, diem& p)
{
// is thay the cho cin de luu cin mot loat thong tin
      cout << "Nhap hoanh do: "; is >> p.x;
      cout << "Nhap tung do: "; is >> p.y;
      return is;
}
/*
void diem::InTT() {
cout<<"("<<x<<","<<y<<")";
};
*/
// viet lai ham InTT bang tac tu <<
ostream& operator << (ostream& os, diem p)
{
// os thay the cho cout de cout mot loat thong tin
      os <<"("<<p.x<<","<<p.y<<")";
      return os;
}
int main()
{
    diem a;
    [You must be registered and logged in to see this link.]
    cin>>a;
    [You must be registered and logged in to see this link.]
    cout<<a;
    getch();
}
[You must be registered and logged in to see this link.]
4. Sử dụng tác tử xuất nhập một đối tượng cho xuất nhập từ file
Lấy class Diem làm ví dụ
Cần nhớ rõ trình tự xuất dữ liệu ra file để khi nhập dữ liệu từ file sẽ lấy được đúng nội dung.
Lưu ý:
o theo tác với file cần thư viện fstream.h
Ví dụ:
o Lưu vào file: được thực hiện theo trình tự sau: x, y
o Lấy dữ liệu từ file: phải theo trình tự dữ liệu đầu là x, dữ liệu thứ 2 là y
Code:
#include <stdio.h>
#include<conio.h>
#include<iostream>
#include <fstream.h>
using namespace std;
class Diem
{
      int x;
      int y;
      public:
      Diem();
      friend ostream& operator << (ostream& os, Diem p);
      friend istream& operator >> (istream& is, Diem& p);
};
Diem::Diem()
{
      x = y = 0;
}
istream& operator >> (istream& is, Diem& p)
{
//Dng cho nhap du lieu tu file
  is >> p.x;
  is >> p.y;
  return is;
}
ostream& operator << (ostream& os, Diem p)
{
//Dung cho xuat du lieu ra file
  os <<p.x<<" "<<p.y;
  return os;
}
int main()
{
    Diem a;
    cin>>a;
    //dua du lieu ra file, ofstream: output + save file
    ofstream f1("MyFile.txt");// MyFile.txt nam chung thu muc voi file nay
    f1<<a;
    f1.close();
    Diem b;
    //Du lieu tu file vào bien, ifstream: input data from file
    ifstream f2("MyFile.txt");
    f2>>b;
    cout<<"\nThong tin diem b:";
    cout<<b;
    getch();
}
[You must be registered and logged in to see this link.]
5. Bài tổng hợp
o Quản lý thông tin sinh viên, lưu dữ liệu ra file và đọc dữ liệu từ file
Code:
#include <stdio.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
#include <fstream.h>
class sinhvien
{
      private:
              char masv[10]; // ma so sinh vien
              char tensv[100]; // ten sinh vien
              int namsinh; // nam sinh
      public:
// Nhap xuat thong tin 1 sinh vien
            void NhapTT(char* = "");
            void InTT(char* = "");
// De giao tiep voi FILE
            friend ostream& operator << (ostream& os, sinhvien A);
            friend istream& operator >> (istream& is, sinhvien& A);
};
void sinhvien::NhapTT(char* str)
{
    cout<<str;
    cout<<"Ma sinh vien:";
    cin>>masv;
    cout<<"Ten sinh vien:";
    cin.ignore();
    cin.getline(tensv,49); // lay nguyen dong, cho phep nhap khoang trang
    cout<<"Nam sinh:";
    cin>>namsinh;
};
void sinhvien::InTT(char* str)
{
    cout<<str;
    cout<<"\n "<<masv<<" : "<<tensv<<" (NS:"<<namsinh<<")";
};
istream& operator >> (istream& is, sinhvien& A)
{
        is>>A.masv;
        is.ignore();
        is.getline(A.tensv,99);
        is>>A.namsinh;
        return is;
};
ostream& operator << (ostream& os, sinhvien A)
{
        os <<A.masv<<endl<<A.tensv<<endl<<A.namsinh;
        return os;
}
int main()
{
    sinhvien A;
    cout<<"\nNhap thong tin: ";
    A.NhapTT();
    cout<<"\nluu lai file";
    ofstream f1("MyFile.txt");
    f1<<A; // dua du lieu trong A vao file
    f1.close();
    cout<<"\n------------------\nLay tu file";
    ifstream f2("MyFile.txt");
    sinhvien B;
    f2>>B;
    cout<<"\nThong tin sinh vien luu trong file: ";
    B.InTT();;
    f2.close();
    getch();
}
[You must be registered and logged in to see this link.]
o Quản lý thông tin lớp học, lưu dữ liệu ra file và đọc dữ liệu từ file
Code:
#include <stdio.h>
#include<conio.h>
#include<iostream>
#include<string.h>
#include <fstream.h>
using namespace std;
class sinhvien
{
      private:
              char masv[10]; // ma so sinh vien
              char tensv[100]; // ten sinh vien
              int namsinh; // nam sinh
      public:
// Nhap xuat thong tin 1 sinh vien
            void NhapTT(char* = "");
            void InTT(char* = "");
// De giao tiep voi FILE
            friend ostream& operator << (ostream& os, sinhvien A);
            friend istream& operator >> (istream& is, sinhvien& A);
};
void sinhvien::NhapTT(char* str)
{
    cout<<str;
    cout<<"Ma sinh vien:";
    cin>>masv;
    cout<<"Ten sinh vien:";
    cin.ignore();
    cin.getline(tensv,49); // lay nguyen dong, cho phep nhap khoang trang
    cout<<"Nam sinh:";
    cin>>namsinh;
};
void sinhvien::InTT(char* str)
{
    cout<<str;
    cout<<"\n "<<masv<<" : "<<tensv<<" (NS:"<<namsinh<<")";
};
istream& operator >> (istream& is, sinhvien& A)
{
        is>>A.masv;
        is.ignore();
        is.getline(A.tensv,99);
        is>>A.namsinh;
        return is;
};
ostream& operator << (ostream& os, sinhvien A)
{
        os <<A.masv<<endl<<A.tensv<<endl<<A.namsinh;
        return os;
}
class LopHoc
{
      private:
              char malop[10];
              char* tenlop;
              char nienkhoa[20];
              int slsv;
              sinhvien* dssv[200]; //Toi da 200 sv cho 1 lop
      public:
            LopHoc();
            void NhapTT();
            void InTT();
            //De giao tiep voi FILE
            friend ostream& operator << (ostream& os, LopHoc A);
            friend istream& operator >> (istream& is, LopHoc& A);
};
LopHoc::LopHoc()
{
      strcpy(malop,"");
      tenlop = new char[50];
      strcpy(nienkhoa,"");
      slsv=0;
//dssv: mang con tro, contro nay se duoc cap phat vung nho khi co sinh vien cu the
}
void LopHoc::NhapTT()
{
    cout<<"\nMa lop hoc:";
    cin>>malop;
    cout<<"Ten lop hoc:";
    cin.ignore();
    cin.getline(tenlop,49);
    cout<<"Nien khoa:";
    cin.getline(nienkhoa,15);
    cout<<"So luong sinh vien:";
    cin>>slsv;
    for (int i=0; i < slsv; i++)
    {
        dssv[i] = new sinhvien();
        dssv[i]->NhapTT();
    }
};
void LopHoc::InTT()
{
    cout<<"\n Lop: "<<malop<<". Ten lop: "<<tenlop<<" ("<<nienkhoa<<"). So luong sv:"<<slsv<<"";
    cout<<"\n Danh sach sinh vien cua lop";
for (int i=0; i < slsv; i++)
{
    dssv[i]->InTT();
}
};
//Dua du lieu tu bien ra file
ostream& operator << (ostream& os, LopHoc A)
{
        os <<A.malop<<endl<<A.tenlop<<endl<<A.nienkhoa<<endl<<A.slsv;
        for (int i=0; i < A.slsv; i++)
        {
            os<<" ";
            os<<(*A.dssv[i]);
        }
        return os;
};
//Dua dl tu file vao bien, luu y tranh tu dua du lieu ra file truoc do de lay lai cho dung
istream& operator >> (istream& is, LopHoc& A)
{
        is>>A.malop;
        is.ignore();
        is.getline(A.tenlop,99);
        is.getline(A.nienkhoa,15);
        is>>A.slsv;
        for (int i=0; i < A.slsv; i++){
        A.dssv[i] = new sinhvien();
        is>>(*A.dssv[i]);// v. dssv là m.ng con tr.
}
        return is;
};
int main()
{
    LopHoc A;
    cout<<"\nNhap thong tin: ";
    A.NhapTT();
    cout<<"\nluu lai file";
    ofstream f1("MyFile.txt");
    f1<<A; // dua du lieu trong A vao file
    f1.close();
    cout<<"\n------------------\nLay tu file";
    ifstream f2("MyFile.txt");
    LopHoc B;
    f2>>B;
    cout<<"\nThong tin sinh vien luu trong file: ";
    B.InTT();;
    f2.close();
    getch();
}
[You must be registered and logged in to see this link.]
Bài tự làm (tổng hợp):
Xây dựng lại class Quản lý thông tin một phòng, lưu dữ liệu ra file và đọc dữ liệu từ file.
Nâng cấp xây dựng class quản lý trường nhiều phòng, lưu dữ liệu ra file và đọc, hiển thị dữ liệu từ file.
augustion_ptn
augustion_ptn

Posts : 634
Thanked : 100
Gia Nhập 27/08/2010

Tài Sản
Thú nuôi:

http://thanhnhancomputer.freevnn.com

Về Đầu Trang Go down

Cool Re: Bài thực hành buổi 6 Môn: Lập Trình Hướng Đối Tượng GV: Lê Thị Thu Lan

Bài gửi by 0951010003 Thu Oct 06, 2011 9:18 am

Hay quá, mình có một ý là rút gọn phân số mình tìm ucln của tử và mẫu, rồi lần lượt lấy tử và mẫu chia cho ucln. nhưng mà mình làm vẫn chưa chạy, có bạn nào biết chỉ mình với
0951010003
0951010003

Posts : 90
Thanked : 13
Gia Nhập 09/09/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: Bài thực hành buổi 6 Môn: Lập Trình Hướng Đối Tượng GV: Lê Thị Thu Lan

Bài gửi by Admin Fri Oct 07, 2011 6:59 pm

0951010003 đã viết:Hay quá, mình có một ý là rút gọn phân số mình tìm ucln của tử và mẫu, rồi lần lượt lấy tử và mẫu chia cho ucln. nhưng mà mình làm vẫn chưa chạy, có bạn nào biết chỉ mình với

Của bác đây! Chúc thành công nhé Bài thực hành buổi 6 Môn: Lập Trình Hướng Đối Tượng GV: Lê Thị Thu Lan 3996168047

Code:


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

class phanso {
      int tu, mau;
  public:
      phanso(int tu=0, int mau=1)
          {this->tu = tu; this->mau = mau;}
      void In(char* str="")
          {int i=1, a = tu, b = mau;
          cout<<str;
          while(a!=b){if(a>b) a = a-b; else b = b-a;};
          if((mau==1) && (a==1)) cout<<tu;
              else if(a==mau) cout<<1;
                else cout<<(tu/a)<<"/"<<(mau/a);
          }
         
      friend phanso operator + (phanso,phanso);
};
      phanso operator + (phanso x, phanso y)
            {
            phanso a;
            a.tu = ((x.tu*y.mau) + (y.tu*x.mau));
            a.mau = (x.mau*y.mau);
            return a;
            }
int main()
{
    phanso a(1,2), b(3,4), e(5,5);
    phanso c = a + b;
    a.In(); cout<<" + "; b.In(); cout<<":= "; c.In();
    cout<<"\n"; e.In("Phan so E:= 5/5 don gian thanh: ");
    getch();
}


Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Cool Re: Bài thực hành buổi 6 Môn: Lập Trình Hướng Đối Tượng GV: Lê Thị Thu Lan

Bài gửi by Sponsored content


Sponsored content


Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết