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

what are the products of Nano chemistry?
Maira Reply
There are lots of products of nano chemistry... Like nano coatings.....carbon fiber.. And lots of others..
learn
Even nanotechnology is pretty much all about chemistry... Its the chemistry on quantum or atomic level
learn
Google
da
no nanotechnology is also a part of physics and maths it requires angle formulas and some pressure regarding concepts
Bhagvanji
Preparation and Applications of Nanomaterial for Drug Delivery
Hafiz Reply
revolt
da
Application of nanotechnology in medicine
what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
Damian
yes that's correct
Professor
I think
Professor
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
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
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
if virus is killing to make ARTIFICIAL DNA OF GRAPHENE FOR KILLED THE VIRUS .THIS IS OUR ASSUMPTION
Anam
analytical skills graphene is prepared to kill any type viruses .
Anam
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
Hafiz
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
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