<< 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: }


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.





class String



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

int Length; //Chieu dai chuoi


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);





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);



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)

Questions & Answers

how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
Do somebody tell me a best nano engineering book for beginners?
s. Reply
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
many many of nanotubes
what is the k.e before it land
what is the function of carbon nanotubes?
I'm interested in nanotube
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
preparation of nanomaterial
Victor Reply
Yes, Nanotechnology has a very fast field of applications and their is always something new to do with it...
Himanshu Reply
good afternoon madam
what is system testing
what is the application of nanotechnology?
In this morden time nanotechnology used in many field . 1-Electronics-manufacturad IC ,RAM,MRAM,solar panel etc 2-Helth and Medical-Nanomedicine,Drug Dilivery for cancer treatment etc 3- Atomobile -MEMS, Coating on car etc. and may other field for details you can check at Google
anybody can imagine what will be happen after 100 years from now in nano tech world
after 100 year this will be not nanotechnology maybe this technology name will be change . maybe aftet 100 year . we work on electron lable practically about its properties and behaviour by the different instruments
name doesn't matter , whatever it will be change... I'm taking about effect on circumstances of the microscopic world
how hard could it be to apply nanotechnology against viral infections such HIV or Ebola?
silver nanoparticles could handle the job?
not now but maybe in future only AgNP maybe any other nanomaterials
I'm interested in Nanotube
this technology will not going on for the long time , so I'm thinking about femtotechnology 10^-15
can nanotechnology change the direction of the face of the world
Prasenjit Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Berger describes sociologists as concerned with
Mueller Reply
Got questions? Join the online conversation and get instant answers!
QuizOver.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?