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

Questions & Answers

How we are making nano material?
LITNING Reply
what is a peer
LITNING Reply
What is meant by 'nano scale'?
LITNING Reply
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
Renato
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
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
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.
Bharti
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
Daniel
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
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
How can I make nanorobot?
Lily
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
NANO
how can I make nanorobot?
Lily
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
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.
Tarell
what is the actual application of fullerenes nowadays?
Damian
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.
Tarell
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.
Virgil
is Bucky paper clear?
CYNTHIA
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
NANO
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
Good
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?

Ask