Thứ Năm, 8 tháng 3, 2012

Xây dựng lớp date trong C++


Xây dựng lớp date1 có các thành phần sau:


- Các thuộc tính : day, month, year
- Hàm thiết lập có tham số mặc định
- Hàm kiểm tra năm nhuận
- Hàm kiểm tra ngày cuối tháng
- Nạp chồng toán tử tăng (++)
- Nạp chồng toán tử (+ =)
- Nạp chồng toán tử xuất (<<)


Viết chương trình kiểm tra


#include <iostream.h>
#include <conio.h>
class date1
{
  private:
    int day;
    int month;
    int year;
    static const int days[];
    void helpIncrement();
  public:
    date1(int d=1,int m=1,int y=1900);
    void setdate(int d,int m,int y);
    date1 &operator++();
    date1 &operator+=(int addday);
    bool leapyear(int y);//kiem tra nam nhuan
    bool endofmonth(int d);//kiem tra ngay cuoi thang
    friend ostream & operator<<(ostream & out,date1 &date);

};

const int date1::days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

date1::date1(int d,int m,int y)
{
   setdate(d,m,y);
}

void date1::setdate(int d,int m,int y)
{
    month=(m>=1 && m<=12)? m:1;
    year=(y>=1900 && y<=2100)?y:1900;
      if( month==2 && leapyear(y))
        day=(d>=1 && d<=29)? d:1;
      else
        day=(d>=1 && d<=days[month])? d:1;
}

bool date1::leapyear(int y)
{
       if ((y%400 ==0)||(y%100 !=0 && y%4==0))
           return true;
       else
           return false;
}

bool date1::endofmonth(int d)
{
     if(month==2 && leapyear(year))
       return d==29;
     else
       return d==days[month];
}

void date1::helpIncrement()
{
    if(month==2 && leapyear(year))//ngay cuoi nam
       {
          day=1;
          month=1;
          ++year;
       }
   else
     if(endofmonth(day))//ngay cuoi thang
       {
        day=1;
        ++month;
       }
     else
       ++day;
}

date1 &date1::operator++()
{
   helpIncrement();
   return * this;
}

date1 & date1::operator+=(int addday)
{
  for(int i=0;i<addday;i++)
    {
       helpIncrement();
    }
  return *this;
}

ostream & operator<<(ostream & out,date1 & date)
{
   char * monthname[13]={" " , "January","February","Match","April","May",
   "June","July","August","September","October","November","December"};

   out<< date.day <<"/"<<monthname[date.month]<<" / " <<date.year<<endl;
   return out;
}
void main()
 {
    date1 d1,d(23,5,2006);
    cout<<d1;
    cout<<d;
    cout<<d++;
    cout<<d;
    d+=2;
    cout<<d;
    getch();
 }

Xây dựng lớp complex có dạng real+ image*i trong C++

Tạo lớp complex (real + image *i ) có các thành phần sau:
- Các thuộc tính : real , image;
- Hàm tạo có sử dụng tham số mặc định
- Nạp chồng các toán tử sau:
+ Toán tử cộng (+) 
+ Toán tử cộng thành phần real của số phức b lên x (b.real + x)
+ Toán tử trừ (-)
+ Toán tử nhập (>>)
+ Toán tử xuất (<<)

#include <iostream.h>
#include <conio.h>
class complex
{
  private:
    float real,image;
  public:
    complex(float r=0,float i=0);
    complex operator+(complex c);
    complex operator-(complex c);
    friend complex operator+(float x,complex c);
    friend ostream& operator<<(ostream &out,complex c);
    friend istream& operator>>(istream &inp,complex& c);
};
 
complex::complex(float r,float i)
{
   real=r;
   image=i;
}
 
complex complex::operator+(complex c)
{
    complex temp;
    temp.real=real+c.real;
    temp.image=image+c.image;
    return temp;
}
 
complex complex::operator-(complex c)
{
   complex temp;
   temp.real=real-c.real;
   temp.image=image-c.image;
   return temp;
}
 
complex operator+(float x, complex c)
{
   complex temp;
   temp.real=c.real+x;
   temp.image=c.image;
   return temp;
}
 
ostream& operator<<(ostream &out,complex c)
{
   cout<<c.real<<"+"<<c.image<<"i"<<endl;
   return out;
}
 
istream& operator>>(istream &inp,complex &c)
{
  cout<<" Nhap vao phan thuc:";
  cin>>c.real;
  cout<<" Nhap vao phan ao:";
  cin>>c.image;
  return inp;
}
 
void main()
{
    complex c,c1,c2,c3;
    float x;
    cin>>c;
    cin>>c1;
    c2=c+c1;
    cout<<" Cong hai so phuc :"<<c2;
    c3=c1-c;
    cout<<" Tru hai so phuc :"<<c3;
    cout<<" Nhap vao x :";
    cin>>x;
    c3=c3+x;
    cout<< c3;
    getch();
}

Xây dựng lớp Phânsố trong C++


Tạo lớp Phân số có các thành phần sau:
- Các thuộc tính: ts,ms;
- Hàm tạo có sử dụng tham số mặc định
- Nạp chồng các toán tử sau:
+ Toán tử cộng (+)
+ Toán tử trừ (-)
+ Toán tử nhân ( * )
+ Toán tử chia (/)
+ Toán tử nhập (>>)
+ Toán tử xuất (<<)


#include "iostream.h"
#include "conio.h"
#include "math.h"
class PS
{
     int t,m;
     public:
     friend ostream& operator<<(ostream& os, PS p);
     friend istream& operator>>(istream& is, PS &p);
     friend PS rutgon(PS p);
     PS operator+(PS p);
     PS operator-(PS p);
     PS operator*(PS p);
     PS operator/(PS p);
};

int USCLN(int x,int y)
{
   if(x==0)
     return y;
   if (y==0)
       return x;
   while (x!=y)
      {
        if(x>y)
         x-=y;
        else
         y-=x;
      }
}

ostream& operator<<(ostream& os, PS p)
{
           os<<p.t<</<<p.m<<endl;
           return os;
}

istream& operator>>(istream& is, PS &p)
{
      is>>p.t>>p.m;
      return is;
}

PS rutgon(PS p)
{
      PS q;
      int x;
      x=uscln(p.t,p.m);
      q.t=p.t/x;
      q.m=p.m/x;
       return q;
}

PS PS::operator+(PS p)
{
      PS q;
      q.t=t*p.m+p.t*m;
      q.m=m*p.m;
      return rutgon(q);
}

PS PS::operator-(PS p)
{
    PS q;
   q.t=t*p.m-p.t*m;
   q.m=m*p.m;
   return rutgon(q);
}

PS PS::operator*(PS p)
{
    PS q;
    q.t=t*p.t;
    q.m=m*p.m;
    return rutgon(q);
}

PS PS::operator/(PS p)
{
    PS q;
    q.t=t*p.m;
    q.m=m*p.t;
    return rutgon(q);
}

void main()
{
     PS p1,p2,p;
    cout<<"Nhap p1=";cin>>p1;
    cout<<"Nhap p2=";cin>>p2;
    p=p1+p2;
   cout<<"p="<<p<<endl;
   getch();
}

Xây dựng lớp Nhanvien trong C+

Xây dựng lớp nhanvien có các thành phần sau:
- Các thuộc tính: tendem, ho, ngaysinh, ngayvaolam. Chú ý : sử dụng con trỏ char cho tendem, ho và ngaysinh, ngayvaolam có kiểu lớp date1 vừa xây dựng ở bài tập trước.
- Hàm tạo
- Hàm trả về tendem
- Hàm trả về ho
- Hàm huỷ
- Hàm hiển thị

File employee.h

#ifndef employee1_h
#define employee1_h
#include "date1.h"
class employee1
{
  private :
    char * firstname;
    char * lastname;
    date1 * birthdate;
    date1 * hiredate;
  public:
    employee1(char *,char *,int,int,int,int,int,int);
    void print();
    char *get_firstname();
    char *get_lastname();
    ~employee1();
};
#endif

File employee.cpp

#include <iostream.h>
#include <conio.h>
#include "employee1.h"
#include <string.h>
#include "date1.h"
 
employee1::employee1(char * fn,char *ln,int bd,int bm,int by,int hd,int hm,int hy)
{
  firstname=new char[strlen(fn)+1];
  strcpy(firstname,fn);
 
  lastname=new char[strlen(fn)+1];
  strcpy(lastname,ln);
  birthdate=new date1(bd,bm,by);
  hiredate=new date1(hd,hm,hy);
 
}
char *employee1::get_firstname()
{
    return firstname;
}
 
char *employee1::get_lastname()
{
    return lastname;
}
 
employee1::~employee1()
{
  cout<<"ham huy duoc goi!"<<endl;
  delete firstname;
  delete lastname;
  delete birthdate;
  delete hiredate;
  getch();
}
 
void employee1::print()
{
  cout << " Ho va ten la:"<<lastname<<"  " << firstname <<endl;
  cout<<" Ngay sinh la:";
   birthdate->print();
  cout<<" Ngay dau di lam la:";
  hiredate->print();
}
 
void main()
{
  employee1 e(" Van Dong "," Pham ",3,4,1983,4,3,2004);
  e.print();
  employee1 * e1=new employee1(" Van A ", " Nguyen ",20,4,1988,7,9,2006);
  cout<<"Ten la:"<<e1->get_firstname()<<endl;
  delete e1;
  getch();
}

Xây dựng lớp point trong C++


Tạo lớp điểm (point) có thuộc tính là các toạ độ x, y.
Xây dựng các hàm cần thiết như nhập và hiển thị các toạ độ, tính khoảng cách giữa hai điểm.
Viết chương trình nhập vào n điểm từ bàn phím.Tìm và hiển thị khoảng cách lớn nhất giữa hai điểm trong n điểm đó.

Xem thêm: http://kenhdaihoc.com/forum/showthread.php?t=2813


Tạo lớp điểm (point) có thuộc tính là các toạ độ x, y.
Xây dựng các hàm cần thiết như nhập và hiển thị các toạ độ, tính khoảng cách giữa hai điểm.
Viết chương trình nhập vào n điểm từ bàn phím.Tìm và hiển thị khoảng cách lớn nhất giữa hai điểm trong n điểm đó.


#include "conio.h"
#include "math.h"
class point
{
    int x,y;
    public:
    void nhap(){
    cout<<"x=";cin>>x;
    cout<<"y=";cin>>y;
}
void in()
{
   cout<<" x= "<<x<<" y= "<<y;
}
double khoangcach(point d)
{
    return sqrt(pow((x-d.x),2)+pow((y-d.y),2));
}
};
void main()
{
      point a[10];
      int i,j,n,imax, jmax;
      double max;
      cout<<" Nhap so diem ";cin>>n;
      for(i=0;i<n;++i) a[i].nhap();
      max=0;
      for(i=0;i<n-1;++i)
     for(j=i+1;j<n;++j)
          if(a[i].khoangcach(a[j])>max)
         {
            max=a[i].khoangcach(a[j]);
            imax=i; jmax=j;
         }
    cout<<"Khoang cach max:"<<max<<endl;
    cout<<"imax="<<(imax+1)<<" jmax="<<(jmax+1);
 getch();
}

Xây dựng lớp vector sử dụng hàm tạo và hàm huỷ trong C++


Tạo một lớp vector gồm có các thành phần sau:
- Các thuộc tính : float * v; int n
- Hàm thiết lập không tham số
- Hàm thiết lập một tham số
- Hàm thiết lập hai tham số
- Hàm hiển thị
- Hàm huỷ
Viết một chương trình kiểm tra.

#include <iostream.h>
#include <conio.h>
class vector
{
   private:
     int n;
     float *v;
   public:
     vector();//ham thiet lap khong tham so
     vector(int size);//ham thiet lap mot tham so
     vector(int size,float * a);//ham thiet lap hai tham so
     void display();
     ~vector();//ham huy
};

vector::vector()
{
   cout<<" Su dung ham thiet lap khong tham so :"<<endl;
   cout<<" Tao mot doi tuong lop vecto co dia chi tai:"<<this<<endl;
   cout<<" Nhap vao so toa do :";
   cin>>n;
   v=new float[n];
   cout<<" Xin cap phat mot vung bo nho cho "<<n<<" so thuc "<<" tai dia chi la: "<<v<<endl;
   for(int i=0;i<n;i++)
    {
      cout<<" Toa do thu "<<(i+1)<<":";
      cin>>v[i];
    }
}
 vector::vector(int size)
{  cout<<" Su dung ham thiet lap mot tham so :"<<endl;
   cout<<" Tao mot doi tuong lop vecto co dia chi tai:"<<this<<endl;
   n=size;
   v=new float[n];
   cout<<" Xin cap phat mot vung bo nho cho "<<n<<"so thuc"<<" tai dia chi la:"<<v<<endl;
   for(int i=0;i<n;i++)
    {
      cout<<" Toa do thu "<<(i+1)<<":";
      cin>>v[i];
    }
}

vector::vector(int size,float * a)
{
   cout<<" Su dung ham thiet lap hai tham so :"<<endl;
   cout<<" Tao mot doi tuong lop vecto co dia chi tai:"<<this<<endl;
   n=size;
   v=new float[n];
   cout<<" Xin cap phat mot vung bo nho cho "<<n<<" so thuc"<<" tai dia chi la:"<<v<<endl;
   for(int i=0;i<n;i++)
      v[i]=a[i];
}
vector::~vector()
{
   cout<<" Giai phong "<<v<<" cua doi tuong co dia chi tai "<<this<<endl;
   delete v;
   getch();
}
void vector::display()
{
  cout<<" Hien thi doi tuong co dia chi tai:"<<this<<endl;
  for(int i=0;i<n;i++)
     cout<<"  " <<v[i]<<"  ";
     cout<<endl;
}
 void main()
{
     vector s;
     s.display();
     vector s1(5);
     s1.display();
     float a[5]={1,2,3,4,5};
     vector s2(5,a);
     s2.display();
     getch();
}

Xây dựng lớp tamgiác trong C++

Xây dựng một lớp tamgiac có các thành phần sau:
- Các thuộc tính là các cạnh a, b, c
- Các hàm thành phần bao gồm:
+ Hàm nhập giá trị cho các cạnh (Kiểm tra tính hợp lệ đảm bảo là 3 cạnh của một tam giác)
+ Hàm tính diện tích tam giác
+ Hàm kiểm tra tam giác(đều, vuông cân, cân, vuông, thường)
+ Hàm hiển thị thông tin( diện tích, tính chất tam giác)
Viết một chương trình kiểm tra.
Trích từ: http://kenhdaihoc.com/forum/showthread.php?t=2811

#include <iostream.h>
#include <conio.h>
#include <math.h>
class tamgiac
{
   private:
     int a,b,c;
      float dientich();
     int kttamgiac();
   public:
     void nhap();
     void in();
};
void tamgiac::nhap()
{
   do
    {
       cout<<"Nhap canh a:";cin>>a;
       cout<<"Nhap canh b:";cin>>b;
         cout<<"Nhap canh c:";cin>>c;
    }while(a+b<c||a+c<b||b+c<a);
}
float tamgiac::dientich()
{
         float p;
            p=(a+b+c)/2;
         return (sqrt(p*(p-a)*(p-b)*(p-c)));
}
 int tamgiac::kttamgiac()
 {
   if(a==b||b==c||c==a)
         if(a==b&&b==c)
          return 1;
       else
            if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b)
           return 2;
         else
              return 3;
  else
     if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b)
              return 4;
     else
        return 5;
 }
 void tamgiac::in()
 {
    cout<<"Dien tich tam giac la:"<<dientich()<<endl;
   int kt=kttamgiac();
   switch(kt)
     {
        case 1:
              cout<<" Tam giac deu"<<endl;
              break;


        case 2:
               cout<<" Tam giac vuong can"<<endl;
               break;
        case 3:
                    cout<<" Tam giac can "<<endl;
               break;
        case 4:
               cout<<" Tam giac vuong"<<endl;
               break;
         case 5:
                    cout<<" Tam giac thuong"<<endl;
               break;
    }
 
}
 void main()
 {
    tamgiac a;
    a.nhap();
     a.in();
    getch();
 }

Tạo một lớp số phức có dạng a+bi
- Thành phần dữ liệu bao gồm: phần thực(a) và phần ảo(b)
- Các hàm thành viên:
+ Hàm đặt giá trị cho phần thực(a) và phần ảo (b) của số phức
+ Hàm cộng hai số phức
+ Hàm hiển thị số phức dưới dạng (a+bi);
Viết các hàm thực hiện trong chương trình chính làm các công việc sau:
1. Nhập các phần tử cho mảng các số phức
2. Hàm cộng hai mảng số phức
3. Hiển thị các phần tử của mảng
Trích từ: http://kenhdaihoc.com/forum/showthread.php?t=2810


#include <iostream.h>#include <conio.h>class complex{   private:      float real,image;   public:      void setcomplex(float,float);        void add( complex );      void print();};void complex::setcomplex(float r,float i){    real=r;    image=i;}void complex::add(complex  s){    real+=s.real;    image+=s.image;}void complex::print(){   cout<<real<<"+"<<image<<"i"<<endl;} void sum( complex c1[],complex c2[], complex c[], int n){  for(int i=0;i<n;i++)    {      c[i].add(c1[i]);      c[i].add(c2[i]);    }}void nhap(complex c[],int n){    float real,image;   for(int i=0;i<n;i++)      {        cout<<"Nhap vao phan thuc cho so thu "<<(i+1)<<":";        cin>>real;        cout<<"Nhap vao phan ao cho so thu "<<(i+1)<<":";        cin>>image;        c[i].setcomplex(real,image);      }} void in(complex c[],int n){  for(int i=0;i<n;i++)    c[i].print();}void main(){   complex c1[20],c[20],c2[20];   int n;   cout<<"Nhap vao so phan tu:";   cin>>n;    cout<<"Nhap vao "<<n<<" so phuc(real,image) cho mang 1:"<<endl;   nhap(c1,n);   cout<<"Nhap vao "<<n<<" so phuc(real,image) cho mang 2:"<<endl;    nhap(c2,n);   cout<<"Cac so phuc thuoc mang 1 la:"<<endl;   in(c1,n);    cout<<"Cac so phuc thuoc mang 2 la:"<<endl;   in(c2,n);   sum(c1,c2,c,n);    cout<<"Cac so phuc thuoc mang c la:"<<endl;   in(c,n); getch();}

Xây dựng lớp thí sinh và lớp danh sách thí sinh trong C++


ĐỀ
Viết một chương trình xây dựng hai lớp: một lớp thí sinh và một lớp danh sách thí sinh. Trong đó lớp thí sinh có dữ liệu bao gồm các thông tin: số báo danh, điểm toán, điểm hoá, điểm lý. Lớp danh sách thí sinh có dữ liệu một mảng các thí sinh và số lượng phần tử thuộc mảng đó. Viết chương trình thực hiện các công việc sau:
1. Nhập và hiển thị một danh sách các thí sinh từ bàn phím
2. Sắp xếp danh sách các thí sinh theo thứ tự tăng dần về điểm số
3. Hiển thị thông tin của các sinh viên có tổng điểm trên 18

Trích từ: http://kenhdaihoc.com/forum/showthread.php?t=2809

Code

#include <iostream.h>
#include <conio.h>
class thisinh
{
   private:
        char sbd[25];
      float dtoan,dly,dhoa;
   public:
        void nhap();
      void in();
      float tdiem();
};
void thisinh::nhap()
{
  cout<<"Nhap vao so bao danh :";
  cin>>sbd;
  cout<<"Nhap diem toan :";
  cin>>dtoan;
  cout<<"Nhap diem ly:";
  cin>>dly;
  cout<<"Nhap diem hoa:";
  cin>>dhoa;
}
void thisinh::in()
{
  cout<<"So bao danh "<<sbd;
  cout<<" Diem toan:"<<dtoan<<" Diem ly:"<<dly<<" Diem hoa:"<<dhoa;
  cout<<" Tong diem: "<<tdiem()<<endl;
}

float thisinh::tdiem()
{
  return(dtoan+dly+dhoa);
}

class dsts
{
  private:
    int n;
     thisinh dsts[100];

  public:
     void nhapds();
      void inds();
     void sapxep();
     void tdtren18();
};

void dsts::nhapds()
{
   cout<<"Nhap vao so luong thi sinh:";
   cin>>n;
   for(int i=0;i<n;i++)
     dsts[i].nhap();
}

void dsts::inds()
{
  for(int i=0;i<n;i++)
    dsts[i].in();
}
void dsts::sapxep()
{
  for(int i=0;i<n-1;i++)
    for(int j=i+1;j<n;j++)
      if(dsts[i].tdiem()>dsts[j].tdiem())
        {
          thisinh temp;
          temp= dsts[i];
          dsts[i]=dsts[j];
             dsts[j]=temp;
        }
}

void dsts::tdtren18()
{
  for(int i=0;i<n;i++)
    if(dsts[i].tdiem()>18)
      dsts[i].in();
}

void main()
{
   dsts ds;
   ds.nhapds();
   cout<<"Sap xep danh sach cac thi sinh theo thu tu tang dan ve tong diem:"<<endl;
   ds.sapxep();
   ds.inds();
    cout<<"Danh sach cac thi sinh co tong diem tren 18 la:"<<endl;
   ds.tdtren18();
   getch();
}

Xây dựng lớp hình tròn đơn giản trong C++


Chương trình dưới đây xây dựng một lớp hình tròn đơn giản có thành phần dữ liệu là bán kính r và có các phương thức như: nhập dữ liệu cho r, tính toán và hiển thị chu vi, diện tích của hình tròn đó

Trích từ: http://kenhdaihoc.com/forum/showthread.php?t=2808

CODE THAM KHẢO

#include <iostream.h> 
#include <conio.h>
#include <math.h>
#define PI 3.14
class hinhtron
{
   private:
        float r;
   public:
      void nhap();
        float chuvi();
      float dientich();
        void in();
};

void hinhtron::nhap()
{
  cout<<"Nhap vao ban kinh:";
  cin>>r;
}

float hinhtron::chuvi()
{
  return (2*PI*r);
}

float hinhtron::dientich()
{
  return (PI*pow(r,2));
}

void hinhtron::in()
{
    cout<<"Chu vi hinh tron la:"<<chuvi()<<endl;
   cout<<"Dien tich hinh tron la:"<<dientich()<<endl;
}
void main()
{
  hinhtron ht;
  ht.nhap();
  ht.in();
  getch();
}

Xây dựng lớp Time trong C++

Xây dựng một lớp Time mô tả các thông tin vê giờ, phút, giây.Lớp Time có các thành phần sau:
- Các thuộc tính mô tả giờ, phút, giây;
- Các hàm thành phần dùng để xác lập giá trị cho từng thành phần giờ, phút, giây(Có kiểm tra điều kiện giờ (0->23), phút(0->59), giây(0->59);
- Hàm thành phần setTime(int,int,int) để xác lập thời gian
- Hàm hiển thị giờ theo định dạng 24 tiếng (vd : 23:54:40);
- Hàm hiển thị giờ theo định dạng 12 tiếng( vd : 11:54:40 PM);
- Hàm tăngGiây()để tăng thời gian mỗi lần lên một giây. Chú ý các trường hợp tăng sang phút tiếp theo, tăng sang giờ tiếp theo,tăng sang ngày tiếp theo.
Viết chương trình chính khai báo một đối tượng thời gian là 23:59:58 và thực hiện tăng thời gian 5 giây đồng thời hiển thị thời gian cho mỗi lần tăng.

Trích từ:  http://kenhdaihoc.com/forum/showthread.php?t=2807

Code Tham khảo:
#include <iostream.h>
#include <conio.h>


class time
{
   private:
          int hour;
          int minute;
             int second;
          void incHour();
          void incMinute();
   public:
     void setTime(int,int=0,int=0);
     void setHour(int);
     void setMinute(int);
      void setSecond(int);
     void print12h();
     void print24h();
      void incSecond();
};


void time::incHour()
{
     hour++;
     if(hour==24) hour=0;


}
void time::incMinute()
{
      minute++;
     if(minute==60)
     {
      minute=0;
      incHour();
      }
}




void time::incSecond()
{
      second++;
     if(second==60)
       {
          second=0;
             incMinute();
       }




}
void time::setHour(int h)
{
  hour=(h>=0 &&h<24)? h:0;
}
void time::setMinute(int m)
{
  minute=(m>=0 &&m<60) ? m:0;
}
void time::setSecond(int s)
{
  second=(s>=0 &&s<60) ? s:0;
}
void time::setTime(int h,int m,int s)
{
    setHour(h);
    setMinute(m);
     setSecond(s);
}
void time::print24h()
{
  cout<<(hour<10 ? "0" : "")<<hour<<":"<<(minute <10 ? "0" : "")<<minute<<":";
  cout<<(second<10 ? "0" : "")<<second<<endl;
}
void time::print12h()
{
  cout<<(hour)<<":"<<(minute<10?"0":"")<<minute<<":";
  cout<<(second<10 ? "0":"")<<second<<(hour<12?" AM":"PM")<<endl;
}




void main()
{
  time t2;
  t2.setTime(23,59,58);
  t2.print12h();
  t2.print24h();
  for(int i=0;i<3;i++)
    {
      t2.incSecond();
      t2.print12h();
        t2.print24h();
    }


  getch();
}
Xem thêm: http://kenhdaihoc.com/forum/showthread.php?t=2807