<< Chapter < Page Chapter >> Page >

{

assert(Index>= 0&&Index<Length&&SubLength>= 0);

String *SubPtr = new String;

assert(SubPtr != 0);

if ((SubLength == 0) || (Index + SubLength>Length))

SubPtr->Length = Length - Index + 1;

else

SubPtr->Length = SubLength + 1;

delete SubPtr->Ptr;

SubPtr->Ptr = new char[SubPtr->Length];

assert(SubPtr->Ptr != 0);

strncpy(SubPtr->Ptr,&Ptr[Index], SubPtr->Length);

SubPtr->Ptr[SubPtr->Length] = '\0';

return *SubPtr;

}

int String::GetLength() const

{return Length;

}

ostream&operator<<(ostream&Output, const String&S)

{

Output<<S.Ptr;

return Output;

}

istream&operator>>(istream&Input, String&S)

{

char Temp[100];

Input>>setw(100)>>Temp;

S = Temp;

return Input;

}

int main()

{

String S1("happy"), S2(" birthday"), S3;

cout<<"S1 is \""<<S1<<"\"; S2 is \""<<S2

<<"\"; S3 is \""<<S3<<'\"'<<endl

<<"The results of comparing S2 and S1:"<<endl

<<"S2 == S1 yields "<<(S2 == S1)<<endl

<<"S2 != S1 yields "<<(S2 != S1)<<endl

<<"S2>S1 yields "<<(S2>S1)<<endl

<<"S2<S1 yields "<<(S2<S1)<<endl

<<"S2>= S1 yields "<<(S2>= S1)<<endl

<<"S2<= S1 yields "<<(S2<= S1)<<endl;

cout<<"Testing !S3:"<<endl;

if (!S3)

{

cout<<"S3 is empty; assigning S1 to S3;"<<endl;

S3 = S1;

cout<<"S3 is \""<<S3<<"\""<<endl;

}

cout<<"S1 += S2 yields S1 = ";

S1 += S2;

cout<<S1<<endl;

cout<<"S1 += \" to you\" yields"<<endl;

S1 += " to you";

cout<<"S1 = "<<S1<<endl;

cout<<"The substring of S1 starting at"<<endl

<<"location 0 for 14 characters, S1(0, 14), is: "

<<S1(0, 14)<<endl;

cout<<"The substring of S1 starting at"<<endl

<<"location 15, S1(15, 0), is: "

<<S1(15, 0)<<endl; // 0 is "to end of string"

String *S4Ptr = new String(S1);

cout<<"*S4Ptr = "<<*S4Ptr<<endl;

cout<<"assigning *S4Ptr to *S4Ptr"<<endl;

*S4Ptr = *S4Ptr;

cout<<"*S4Ptr = "<<*S4Ptr<<endl;

delete S4Ptr;

S1[0] = 'H';

S1[6] = 'B';

cout<<"S1 after S1[0] = 'H' and S1[6]= 'B' is: "<<S1<<endl;

cout<<"Attempt to assign 'd' to S1[30] yields:"<<endl;

S1[30] = 'd'; //Loi: Chi so vuot khoi mien!!!

return 0;

}

Chúng ta chạy ví dụ 4.18 , kết quả ở hình 4.23

Hình 4.23: Kết quả của ví dụ 4.18

Lớp date

Ví dụ 4.19:  

#include<iostream.h>

class Date

{

private:

int Month;

int Day;

int Year;

static int Days[]; //Mang chua so ngay trong thang

void HelpIncrement(); //Ham tang ngay len mot

public:

Date(int M = 1, int D = 1, int Y = 1900);

void SetDate(int, int, int);

Date operator++(); //Tien to

Date operator++(int); //Hau to

const Date&operator+=(int);

int LeapYear(int); //Kiem tra nam nhuan

int EndOfMonth(int); //Kiem tra cuoi thang

friend ostream&operator<<(ostream&, const Date&);

};

int Date::Days[] = {31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31};

Date::Date(int M, int D, int Y)

{

SetDate(M, D, Y);

}

void Date::SetDate(int MM, int DD, int YY)

{

Month = (MM>= 1&&MM<= 12) ? MM : 1;

Year = (YY>= 1900&&YY<= 2100) ? YY : 1900;

if (Month == 2&&LeapYear(Year))

Day = (DD>= 1&&DD<= 29) ? DD : 1;

else

Day = (DD>= 1&&DD<= Days[Month-1]) ? DD : 1;

}

Date Date::operator++()

{

HelpIncrement();

return *this;

}

Date Date::operator++(int)

{

Date Temp = *this;

HelpIncrement();

return Temp;

}

const Date&Date::operator+=(int AdditionalDays)

{

for (int I = 1; I<= AdditionalDays; I++)

HelpIncrement();

return *this;

}

int Date::LeapYear(int Y)

{

if (Y % 400 == 0 || (Y % 100 != 0&&Y % 4 == 0) )

return 1; //Nam nhuan

return 0; //Nam khong nhuan

}

int Date::EndOfMonth(int D)

{

if (Month == 2&&LeapYear(Year))

return D == 29;

return D == Days[Month-1];

}

void Date::HelpIncrement()

{

if (EndOfMonth(Day)&&Month == 12) //Het nam

{

Day = 1;

Month = 1;

++Year;

}

else

if (EndOfMonth(Day)) //Het thang

{

Day = 1;

++Month;

}

else

++Day;

}

ostream&operator<<(ostream&Output, const Date&D)

{

static char*MonthName[12]={"January","February","March","April","May",

"June","July", "August","September",

"October","November", "December" };

Output<<MonthName[D.Month-1]<<' '<<D.Day<<", "<<D.Year;

return Output;

}

int main()

{

Date D1, D2(12, 27, 1992), D3(0, 99, 8045);

cout<<"D1 is "<<D1<<endl

<<"D2 is "<<D2<<endl

<<"D3 is "<<D3<<endl<<endl;

cout<<"D2 += 7 is "<<(D2 += 7)<<endl<<endl;

D3.SetDate(2, 28, 1992);

cout<<" D3 is "<<D3<<endl;

cout<<"++D3 is "<<++D3<<endl<<endl;

Date D4(3, 18, 1969);

cout<<"Testing the preincrement operator:"<<endl

<<" D4 is "<<D4<<endl;

cout<<"++D4 is "<<++D4<<endl;

cout<<" D4 is "<<D4<<endl<<endl;

cout<<"Testing the postincrement operator:"<<endl

<<" D4 is "<<D4<<endl;

cout<<"D4++ is "<<D4++<<endl;

cout<<" D4 is "<<D4<<endl;

return 0;

}

Chúng ta chạy ví dụ 4.19 , kết quả ở hình 4.24

Hình 4.24: Kết quả của ví dụ 4.19

Bài tập

Bài 1: Xây dựng lớp Complex chứa các số phức gồm các phép toán: +, -, *, /, +=, -=, *=, /=, ==, !=,>,>=,<,<=.

Bài 2: Xây dựng lớp String để thực hiện các thao tác trên các chuỗi, trong lớp này có các phép toán:

Phép toán + để nối hai chuỗi lại với nhau.

Phép toán = để gán một chuỗi cho một chuỗi khác.

Phép toán [] truy cập đến một ký tự trong chuỗi.

Các phép toán so sánh: ==, !=,>,>=,<,<=

Bài 3: Xây dựng lớp ma trận Matrix gồm các phép toán cộng, trừ và nhân hai ma trận bất kỳ.

Bài 4: Xây dựng lớp Rational chứa các số hữu tỷ gồm các phép toán +, - , *, /, ==, !=,>,>=,<,<=.

Bài 5: Xây dựng lớp Time để lưu trữ giờ, phút, giây gồm các phép toán:

Phép cộng giữa dữ liệu thời gian và một số nguyên là số giây, kết quả là một dữ liệu thời gian.

Phép trừ giữa hai dữ liệu thời gian, kết quả là một số nguyên chính là số giây.

++ và – để tăng hay giảm thời gian xuống một giây.

Các phép so sánh.

Bài 6: Xây dựng lớp Date để lưu trữ ngày, tháng, năm gồm các phép toán:

Phép cộng giữa dữ liệu Date và một số nguyên là số ngày, kết quả là một dữ liệu Date.

Phép trừ giữa hai dữ liệu Date, kết quả là một số nguyên chính là số ngày.

++ và – để tăng hay giảm thời gian xuống một ngày.

Các phép so sánh.

Bài 7: Các số nguyên 32 bit có thể biểu diễn trong phạm vi từ 2147483648 đến 2147483647. Hãy xây dựng lớp HugeInt để biểu diễn các số nguyên 32 bit gồm các phép toán +, -, *, /

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Lập trình hướng đối tượng. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10794/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Lập trình hướng đối tượng' conversation and receive update notifications?

Ask