<< 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 we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
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
characteristics of micro business
for teaching engĺish at school how nano technology help us
How can I make nanorobot?
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
how can I make nanorobot?
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
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?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
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
Got questions? Join the online conversation and get instant answers!
Jobilize.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?