Kênh Tên Miền chuyên cung cấp tên miền đẹp, giá rẻ! Hãy liên hệ kỹ thuật: 0914205579 - Kinh doanh: 0912191357 để được tư vấn, hướng dẫn miễn phí, Cảm ơn quý khách đã ủng hộ trong thời gian qua!
Monday, March 9, 2015

Hướng dẫn thực hành Xây dựng hàm Class đơn giản:


Code hướng dẫn:













/*

Name: Quan ly thong tin toa do diem

Copyright: None

Author: Tran Anh

Date: 20/01/15 01:53

Description: http:/code.tavn.net

*/

#include<stdio.h>

#include<conio.h>

#include<iostream>

using namespace std;


class Diem{
private:
int x;
int y;
public:
void KhoiTao(int, int);
void NhapDiem();
void InDiem();
void DoiDiem(int, int);
int GiaTriX();
int GiaTriY();
};

void Diem::KhoiTao(int u, int v)
{
x=u;
y=v;
}
void Diem::NhapDiem()
{
cout<<"Nhap X: ";
cin>>x;
cout<<"Nhap Y: ";
cin>>y;
}
void Diem::InDiem()
{
cout<<"(X= "<<x<<", Y= "<<y<<")"<<endl;  //"endl;" la in xong xuong dong
}
void Diem::DoiDiem(int dx, int dy)
{
x+=dx;
y+=dy;
}
int Diem::GiaTriX()
{
return x;
}
int Diem::GiaTriY()
{
return y;
}

main()
{
cout<<"***DUNG BIEN THONG THUONG:"<<endl;
Diem a;
a.KhoiTao(5,3);
a.InDiem();
//Doi diem a dich chuyen x tang 1, y tang 2
a.DoiDiem(1,2);
a.InDiem();
//In gia tri X cua diem a
cout<<"X cua a= "<<a.GiaTriX()<<endl;
//In gia tr Y cua diem a
cout<<"Y cua a= "<<a.GiaTriY()<<endl;
//Thu nhap diem cho a
a.NhapDiem();
a.InDiem();

cout<<endl<<"***DUNG BIEN CON TRO:"<<endl;
Diem *p;
p =  new Diem;
//Khoi tao diem p con tro
p->KhoiTao(7,4);
p->InDiem();
//Nhap diem p con tro
p->NhapDiem();
p->InDiem();
//Doi diem X tang them 6, Y tang them 7
p->DoiDiem(6,7);
p->InDiem();
//Gia tri X cua P con tro
p->GiaTriX();
//Gia tri Y cua P con tro
p->GiaTriY();


cout<<endl<<"***DUNG MANG DAY SO:"<<endl;
Diem ds[7];

for(int i=0;i<7;i++)
ds[i].KhoiTao(i,i);
for(int i=0;i<7;i++)
{
cout<<" ";
ds[i].InDiem();
}

cout<<endl<<"***TRUY XUAT THEO YEU CAU:"<<endl;
cout<<"- Truy xuat gia tri X cua a: "<<endl;
cout<<"X= "<<a.GiaTriX();

cout<<endl;

cout<<"- Truy xuat gia tri X cua con tro P: "<<endl;
cout<<"X= "<<p->GiaTriX();

cout<<endl;

cout<<"- Truy xuat gia tri X cua phan tu ds[4]: "<<endl;
cout<<"X= "<<ds[4].GiaTriX();

cout<<endl;

cout<<"Tao mot ban sao b giong a: "<<endl;
Diem b=a;  //Don gian la gang cho b gia tri bang a
b.InDiem();

getch();
}














/*
Name: Quan ly thong tin Sinh vien
Copyright: None
Author: Tran Anh
Date: 20/01/15 01:53
Description: http:/code.tavn.net
*/
#include<conio.h>
#include<stdio.h>
#include<iostream>
using namespace std;

class SinhVien
{
private:
    char mssv[15];
    char *hoten;
    int namsinh;
    float diem;
public:
    SinhVien();
    SinhVien(char*, char*, int,float);
    ~SinhVien();
    void Nhap();
    void Inthongtin();
    char* XepLoai();
    float GiaTriDiem();
};


SinhVien::SinhVien()
{
    strcpy(mssv,"");
    hoten=new char[200];
    strcpy(hoten,"");
    namsinh=1900;
    diem=0;
}

SinhVien::SinhVien(char* ms, char* ht, int ns, float d)
{
    strcpy(mssv,ms);
    hoten= new char[200];
    strcpy(hoten,ht);
    namsinh=ns;
    diem=d;
}

SinhVien::~SinhVien()
{
    delete[] mssv;
    delete hoten;

}

void SinhVien::Nhap()
{
    cout<<"Ma so sinh vien: ";
    cin>>mssv;

    cout<<"Ho va ten: ";
    cin.ignore();
    cin.getline(hoten,199);
    cout<<"Nam sinh: ";
    cin>>namsinh;
    cout<<"Diem trung binh: ";
    cin>>diem;
}

void SinhVien::Inthongtin()
{
    cout<<endl;
    cout<<"Ten: "<<hoten<<"   |   "<<mssv<<"   |   "<<namsinh<<"    |    "<<diem<< endl;

}

float SinhVien::GiaTriDiem()
{
    return diem;
}
char* SinhVien::XepLoai()
{
    char* xl;
    xl= new char[10];
    if(diem<=10) strcpy(xl,"Gioi");
    if(diem<8) strcpy(xl,"Kha");
    if(diem<6.5) strcpy(xl,"Trung Binh");
    if(diem<5) strcpy(xl,"Yeu");
    return xl;
}
main()
{

    SinhVien a;
  //  a.Nhap();
    //a.Inthongtin();

    SinhVien b("13D480201016","Tran Anh", 1999, 4.0);
    b.Inthongtin();

    cout<<"\nGia tri diem b: "<<b.GiaTriDiem();
    cout<<"\nXep loai: "<<b.XepLoai();

    getch();

}












/*
    Name: Class phong hoc
    Authr: Tran Anh
    Copyright: None
    Communication: http://code.tavn.net

*/
#include<conio.h>
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;

class Room
{
private:
    char RoomCode[20];
    char* RoomName;
    int Quantity;
public:
    Room();
    void InpRoom();
    void OutRoom();
};

Room::Room()
{
    strcpy(RoomCode,"");
    RoomName=new char[255];
    strcpy(RoomName,"");
    Quantity=0;
}

void Room::InpRoom()
{
    cout<<"\t- Nhap ma phong: ";
    cin>>RoomCode;
    cin.ignore();
    cout<<"\t- Nhap ten phong: ";
    cin.getline(RoomName,100);
    cout<<"\t- Nhap so luong cho ngoi: ";
    cin>>Quantity;
}

void Room::OutRoom()
{
    cout<<"\n\t=> Phong "<<RoomCode<<" : "<<RoomName<<" : "<<Quantity<<" cho ngoi"<<endl;
}

main()
{
    Room a;
    a.InpRoom();
    a.OutRoom();

    getch();
    return 0;
}










/*
    Name: Quan ly mon an
    Authr: Tran Anh
    Copyright: None
    Communication: http://code.tavn.net

*/

#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<string.h>
using namespace std;

class MonAn
{
private:
    char MaMonAn[];
    char* TenMonAn;
    int SoLuong;
    int DonGia;
public:
    MonAn();
    void NhapTT();
    void InTT();
    int GetDonGia();
};

MonAn::MonAn()
{
    strcpy(MaMonAn,"");
    TenMonAn=new char[200];
    strcpy(TenMonAn,"");
    SoLuong=0;
    DonGia=0;
}

void MonAn::NhapTT()
{
    cout<<"\tNhap thong tin mon:"<<endl;
    cout<<"Ma mon: "; cin>>MaMonAn;
    fflush(stdin);
    cout<<"Ten mon: "; cin.getline(TenMonAn,200);
    cout<<"Don gia: "; cin>>DonGia;
}



void MonAn::InTT()
{

    cout<<MaMonAn<<"\t"<<TenMonAn<<"\t\t"<<"( "<<DonGia<<" dong)"<<endl;
}

int MonAn::GetDonGia()
{
    return DonGia;
}


main()
{

    MonAn a[100];
    char key;
     int i=0;

    cout<<"\t\tNHAP THONG TIN NHIEU MON AN\n";
    cout<<"\t i: Nhap so moi"<<endl;
    cout<<"\t p: In lai danh sach mon an da nhap"<<endl;
    cout<<"\t q: In ra danh sach mon an theo thu tu tang dan cua don gia\n";



    while(1){
        fflush(stdin);
      cout<<"\n- Chon thao tac: "; cin>>key;


            if(key=='i')
            {
                i++;
                a[i].NhapTT();
            }


            if(key=='p')
            {
                int n=i;
                cout<<"\n\t Danh sach mon an da nhap:"<<endl;
                for(int i=1;i<=n;i++)
                    a[i].InTT();
            }


            if(key=='q')
            {
                int n=i;
                cout<<"\n\t Danh sach "<<n<<" mon an da nhap:"<<endl;

                for(int i=1;i<n;i++)
                    for(int j=i+1;j<=n;j++)
                        if((a[i].GetDonGia())>(a[j].GetDonGia()))
                            {
                                MonAn k;
                                k=a[i];
                                a[i]=a[j];
                                a[j]=k;
                            }


                for(int i=1;i<=n;i++)
                    a[i].InTT();
                 
                    i=0; // tra don hang ve khong khi da t
            }

    }
    getch();
}




Key:
 

0 comments:

Post a Comment

Popular Posts