Thứ Tư, 30 tháng 12, 2009

// // Leave a Comment

C++ Keywords: typedef





An Alias for a Known Type

Trong C++, bạn có thể khai báo sử dụng một

trong các loại dữ liệu:

#include <iostream>

using namespace std;



int main()

{

int number = 248;

return 0;

}


Theo cách tương tự như vậy, thì bạn có thể khai báo một mảng
hoặc một con trỏ.
Bạn cũng có thể khai báo nhiều biến cùng
trên một dòng. Ví dụ:


#include <iostream>

#include <string>

using namespace std;



int main()

{

int number = 248;

double *distance = new double(390.82);

string countries[] = { "Ghana", "Angola", "Togo",

"Tunisia", "Cote d'Ivoire" };



cout << "Number: " << number << endl;

cout << "Distance: " << *distance << endl;

cout << "Countries:\n";

for(int i = 0; i < sizeof(countries) / sizeof(string); i++)

cout << '\t' << countries[i] << endl;



cout << endl;

return 0;

}

Nếu bạn dự định sử dụng kiểu dữ liệu giống nhau cho nhiều biến,
thì bạn
có thể thay đổi tên của nó. Trong C++, từ khoá typedef
cho phép bạn tạo một
bí danh cho một kiểu dữ liệu. Cú pháp khai
báo như sau:



typedef [attributes] DataType AliasName;


Từ khoá typedef là bắt buộc, còn thuộc tính thì không.



Theo sau typedef là một loại kiểu dữ liệu như int, short,

signed, unsigned, char,signed char, unsigned char, double, long,

hoặc long double. Kiểu dữ liệu có thể là một lớp hoặc được cung

cấp bởi một thư viện của trình biên dịch C++.

Ví dụ, nó có thể là lớp string. Kiểu dữ liệu cũng có thể là một

con trỏ, trỏ tới một kiểu chưa biết.

Cạnh bên phải của kiểu dữ liệu, bạn sẽ phải nhập tên mà tên đó

sẽ được sử dụng cho các loại kiểu dữ liệu hoặc con trỏ. Ví dụ:


#include <iostream>

#include <string>

using namespace std;



typedef short SmallNumber;

typedef unsigned int Positive;

typedef double* PDouble;

typedef string FiveStrings[5];



int main()

{



cout << endl;

return 0;

}




  1. Trong typedef short SmallNumber, SmallNumber được sử dụng như một kiểu dữ liệu để khai báo cho kiểu số nguyên nhỏ.

  2. Ở dòng thứ hai, typedef unsigned int Positive, Từ Positive

    được sử dụng như một biến thuộc kiểu unsigned int.


  3. Sau đó là typedef double* PDouble, thì tên PDouble được sử dụng để khai báo một con trỏ trỏ tơí double.


  4. Bằng việc định nghĩa typedef string FiveStrings[5], FiveStrings được sử dụng để khai báo một mảng gồm 5 string mà mỗi string thuộc kiểu string.



Dựa vào nguyên tắc này, mà bạn có thể khai báo nhiều biến cho

nhiều kiểu mà bạn tự định nghĩa. Ví dụ:

#include <iostream>

#include <string>

using namespace std;



typedef short SmallNumber;

typedef unsigned int Positive;

typedef double* PDouble;

typedef string FiveStrings[5];



int main()

{

SmallNumber temperature = -248;

Positive height = 1048;

PDouble distance = new double(390.82);

FiveStrings countries = { "Ghana", "Angola", "Togo",

"Tunisia", "Cote d'Ivoire" };



cout << "Temperature: " << temperature << endl;

cout << "Height: " << height << endl;

cout << "Distance: " << *distance << endl;

cout << "Countries:\n";

for(int i = 0; i < sizeof(countries) / sizeof(string); i++)

cout << '\t' << countries[i] << endl;



cout << endl;

return 0;

}


Kết quả thu được:

Temperature: -248

Height: 1048

Distance: 390.82

Countries:

Ghana

Angola

Togo

Tunisia

Cote d'Ivoire
















Type-Defining a Function





Từ khoá typedef cũng có thể được sử dụng để định nghĩa

bí danh cho một hàm. Trong trường hợp này,

bạn phải xác định kiểu trả về của hàm và kiểu của nó

của các đối số (arguments), nếu có. Một nguyên tác khác

là định nghĩa phải chỉ định rằng nó là một con trỏ hàm.

Xem ví dụ sau:

#include <iostream>

#include <string>

using namespace std;



typedef short SmallNumber;

typedef unsigned int Positive;

typedef double* PDouble;

typedef string FiveStrings[5];

typedef double (*Addition)(double value1, double value2);



double Add(double x, double y)

{

double result = x + y;

return result;

}



int main()

{

SmallNumber temperature = -248;

Positive height = 1048;

PDouble distance = new double(390.82);

FiveStrings countries = { "Ghana", "Angola", "Togo",

"Tunisia", "Cote d'Ivoire" };

Addition plus;



cout << "Temperature: " << temperature << endl;

cout << "Height: " << height << endl;

cout << "Distance: " << *distance << endl;

cout << "Countries:\n";

for(int i = 0; i < sizeof(countries) / sizeof(string); i++)

cout << '\t' << countries[i] << endl;



plus = Add;

cout << "3855.06 + 74.88 = " << plus(3855.06, 74.88) << endl;

return 0;

}


Kết quả thu được như sau:

Temperature: -248

Height: 1048

Distance: 390.82

Countries:

Ghana

Angola

Togo

Tunisia

Cote d'Ivoire

3855.06 + 74.88 = 3929.94















The Attribute of a Type-Definition

Từ khoá typedef có thể được theo bởi một thuộc tính

trước khi nhập kiểu dữ liệu. Trong hình thức đơn giản nhất,

thuộc tính có được đó ở một cấp độ truy cập như: public, private,

hoặc protected.

Ví dụ:

#include <iostream>

#include <string>

using namespace std;



typedef public short SmallNumber;

typedef private unsigned int Positive;

typedef protected double* PDouble;

typedef public string FiveStrings[5];

typedef private double (*Addition)(double value1, double value2);



double Add(double x, double y)

{

double result = x + y;

return result;

}



int main()

{

SmallNumber temperature = -248;

Positive height = 1048;

PDouble distance = new double(390.82);

FiveStrings countries = { "Ghana", "Angola", "Togo",

"Tunisia", "Cote d'Ivoire" };

Addition plus;



cout << "Temperature: " << temperature << endl;

cout << "Height: " << height << endl;

cout << "Distance: " << *distance << endl;

cout << "Countries:\n";

for(int i = 0; i < sizeof(countries) / sizeof(string); i++)

cout << '\t' << countries[i] << endl;



plus = Add;

cout << "3855.06 + 74.88 = " << plus(3855.06, 74.88) << endl << endl;

return 0;

}


Trong C++, thuộc tính của typedef thường được áp dụng

như là một lớp(class), một cấu trúc (struct) hoặc một

kiểu liệt kê (enum). Ví dụ:

#include <iostream>

#include <string>

using namespace std;



typedef struct Student;

typedef class Country;




typedef public short SmallNumber;

typedef private unsigned int Positive;

typedef protected double* PDouble;

typedef public string FiveStrings[5];

typedef private double (*Addition)(double value1, double value2);



struct Student

{

string FirstName;

string LastName;

};



typedef struct _Empl

{

string FullName;

double HourlySalary;

}Employee;



class Country

{

string Name;

string Capital;

string Code;

};



double Add(double x, double y)

{

double result = x + y;

return result;

}



typedef enum EmplStatus { esFullTime, esPartTime, esContractor };

typedef Student *PStudent;

typedef Country *PCountry;



int main()

{

Student pupil;

Country pais;

EmplStatus emplst;

PStudent ptrStd = new Student;

PCountry pPais = new Country;



return 0;

}

0 comments: