<< Chapter < Page Chapter >> Page >

25: {

26: cout<<"X:";

27: In>>P.X;

28: cout<<"Y:";

29: In>>P.Y;

30: return In; //Cho phép cin>>a>>b>>c;

31: }

32:

33: int main()

34: {

35: Point P;

36: cin>>P;

37: cout<<"Point:"<<P;

38: return 0;

39: }

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

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

Một số ví dụ

Lớp string

Ví dụ 4.18: Chúng ta sẽ xây dựng một lớp xử lý việc tạo và thao tác trên các chuỗi (string). C++ không cài sẵn kiểu dữ liệu chuỗi. Nhưng C++ cho phép chúng ta thêm kiểu chuỗi như một lớp thông qua cơ chế đa năng hóa.

#include<iostream.h>

#include<iomanip.h>

#include<string.h>

#include<assert.h>

class String

{

private:

char *Ptr; //Con tro tro den diem bat dau cua chuoi

int Length; //Chieu dai chuoi

public:

String(const char * = ""); //Constructor chuyen doi

String(const String&); //Constructor sao chep

~String(); //Destructor

const String&operator=(const String&); //Phep gan

String&operator+=(const String&); //Phep noi

int operator!() const; //Kiem tra chuoi rong

int operator==(const String&) const;

int operator!=(const String&) const;

int operator<(const String&) const;

int operator>(const String&) const;

int operator>=(const String&) const;

int operator<=(const String&) const;

char&operator[](int); //Tra ve ky tu tham chieu

String&operator()(int, int); //Tra ve mot chuoi con

int GetLength() const;

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

friend istream&operator>>(istream&, String&);

};

//Constructor sao chep: Chuyen doi char * thanh String

String::String(const char *S)

{

cout<<"Conversion constructor: "<<S<<endl;

Length = strlen(S);

Ptr = new char[Length + 1];

assert(Ptr != 0);

strcpy(Ptr, S);

}

String::String(const String&Copy)

{

cout<<"Copy constructor: "<<Copy.Ptr<<endl;

Length = Copy.Length;

Ptr = new char[Length + 1];

assert(Ptr != 0);

strcpy(Ptr, Copy.Ptr);

}

//Destructor

String::~String()

{

cout<<"Destructor: "<<Ptr<<endl;

delete [] Ptr;

}

const String&String::operator=(const String&Right)

{

cout<<"operator= called"<<endl;

if (&Right != this)

{

delete [] Ptr;

Length = Right.Length;

Ptr = new char[Length + 1];

assert(Ptr != 0);

strcpy(Ptr, Right.Ptr);

}

else

cout<<"Attempted assignment of a String to itself"<<endl;

return *this;

}

String&String::operator+=(const String&Right)

{

char *TempPtr = Ptr;

Length += Right.Length;

Ptr = new char[Length + 1];

assert(Ptr != 0);

strcpy(Ptr, TempPtr);

strcat(Ptr, Right.Ptr);

delete [] TempPtr;

return *this;

}

int String::operator!() const

{

return Length == 0;

}

int String::operator==(const String&Right) const

{

return strcmp(Ptr, Right.Ptr) == 0;

}

int String::operator!=(const String&Right) const

{

return strcmp(Ptr, Right.Ptr) != 0;

}

int String::operator<(const String&Right) const

{

return strcmp(Ptr, Right.Ptr)<0;

}

int String::operator>(const String&Right) const

{

return strcmp(Ptr, Right.Ptr)>0;

}

int String::operator>=(const String&Right) const

{

return strcmp(Ptr, Right.Ptr)>= 0;

}

int String::operator<=(const String&Right) const

{

return strcmp(Ptr, Right.Ptr)<= 0;

}

char&String::operator[](int Subscript)

{

assert(Subscript>= 0&&Subscript<Length);

return Ptr[Subscript];

}

String&String::operator()(int Index, int SubLength)

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