<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.:As*** - As247:. &#187; C++</title>
	<atom:link href="http://as247.vui360.com/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://as247.vui360.com/blog</link>
	<description>Welcome to Thế Anh&#039;s blog</description>
	<lastBuildDate>Tue, 20 Dec 2011 02:30:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Loại bỏ dấu tiếng Việt trong tên file</title>
		<link>http://as247.vui360.com/blog/loai-bo-dau-tieng-viet-trong-ten-file/</link>
		<comments>http://as247.vui360.com/blog/loai-bo-dau-tieng-viet-trong-ten-file/#comments</comments>
		<pubDate>Wed, 12 May 2010 08:29:53 +0000</pubDate>
		<dc:creator>As</dc:creator>
				<category><![CDATA[ICT]]></category>
		<category><![CDATA[Kinh nghiệm]]></category>
		<category><![CDATA[AutoIt]]></category>
		<category><![CDATA[Bỏ dấu]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Files]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Loại bỏ]]></category>
		<category><![CDATA[Nhạc phim]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Tiếng Việt]]></category>

		<guid isPermaLink="false">http://as247.vui360.com/blog/?p=1049</guid>
		<description><![CDATA[Chả là có cái list nhạc phim, muốn upload lên nhưng mà tên file thì bằng tiếng Việt có dấu, host thì host windows nên không up lên được Tìm phần mềm mãi cũng không có Thử dùng php nhưng php chạy trên windows cũng không get đc tên file tiếng Việt, rồi thử dùng [...]]]></description>
			<content:encoded><![CDATA[<p>Chả là có cái list nhạc phim, muốn upload lên nhưng mà tên file thì bằng tiếng Việt có dấu, host thì host windows nên không up lên được  <img src='http://as247.vui360.com/blog/wp-includes/images/smilies/2.gif' alt=':-(' class='wp-smiley' />  Tìm phần mềm mãi cũng không có  <img src='http://as247.vui360.com/blog/wp-includes/images/smilies/18.gif' alt='#:-s' class='wp-smiley' />  Thử dùng php nhưng php chạy trên windows cũng không get đc tên file tiếng Việt, rồi thử dùng java thấy cũng không ăn thua,&#8230; nghĩ đến C++ nhưng mà nhớ là ngày xưa lập trình C++ thì hình như ko hỗ trợ unicode(utf-8) và lại trên máy cũng ko cài IDE cho C++ nên thôi ko thử nữa,&#8230; rồi dùng thử AutoIT nhưng cũng chưa thạo cho lắm, nên phải lấy danh sách file qua console và cũng bị lỗi dấu&#8230; Cuối cùng spam cả list yahoo để hỏi, một đứa bạn trả lời là dùng java được đấy, sao mình dùng mãi không được nhỉ  <img src='http://as247.vui360.com/blog/wp-includes/images/smilies/102.gif' alt='~X(' class='wp-smiley' />  lúc đầu là in ra danh sách các file thấy nó toàn bị lỗi rồi thử print 1 đoạn chữ tiếng Việt ra thấy nó cũng lỗi, đoán là do cái phần hiển thị output nó ko hỗ trợ unicode giống như chạy qua cmd vậy nên cũng thử cả System.out.print(filename.equals(&#8220;nhạc.mp3&#8243;)) nhưng nó vẫn print ra false  <img src='http://as247.vui360.com/blog/wp-includes/images/smilies/10_as.gif' alt='@@' class='wp-smiley' />  không nghĩ đến trường hợp cái editor ko hỗ trợ unicode vì vẫn gõ tiếng Việt trên đó đều đều + encoding của file lại là utf-8. Hỏi lại đứa bạn lần nữa thì nó bảo phải dùng eclipse ấy, jcreator ko được đâu và phải chỉnh lại bảng mã soạn thảo cho cái eclipse là unicode. Lúc này mới thử kiểm tra bảng mã của cái jcreator thì thấy là nó ko có unicode  <img src='http://as247.vui360.com/blog/wp-includes/images/smilies/2.gif' alt=':-(' class='wp-smiley' />  vậy mà mình cứ nghĩ là nó đặt sẵn unicode rồi tại vì vẫn gõ tiếng Việt trên đó đều mà  <img src='http://as247.vui360.com/blog/wp-includes/images/smilies/22.gif' alt=':-|' class='wp-smiley' /> <span id="more-1049"></span></p>
<p>Và đây là đoạn mã nguồn để bỏ dấu trong tên file. Copy các file cần bỏ dấu vào thư mục input rồi chạy là xong  <img src='http://as247.vui360.com/blog/wp-includes/images/smilies/4.gif' alt=':d' class='wp-smiley' />  các file đã được bỏ dấu sẽ nằm ở thư mục output  <img src='http://as247.vui360.com/blog/wp-includes/images/smilies/4.gif' alt=':d' class='wp-smiley' />  . Ai thích dùng ngay thì tải <a href="http://as247.vui360.com/download/?file=Rename-1.1.zip">Rename-1.1.zip</a> về chạy run.bat<br />
Các phiên bản:</p>
<p style="padding-left: 30px;">- 1.0: <a href="http://as247.vui360.com/download/?file=Rename.rar">Rename.rar</a></p>
<p style="padding-left: 30px;">- 1.1: <a href="http://as247.vui360.com/download/?file=Rename-1.1.zip">Rename-1.1.zip</a></p>
<pre class="brush: java">
import java.io.File;
class Rename{
public Rename(String source,String dest){
FileRename(source,dest);
}
public static void main (String[] args) {
new Rename(&quot;input&quot;,&quot;output&quot;);

}
public void FileRename(String source,String dest){
File fromDir=new File(source);
File destDir=new File(dest);
if(!fromDir.isDirectory())
if(!fromDir.mkdir()){
System.out.println(&quot;Cound not create dir &quot;+source);
}
if(!destDir.isDirectory())
if(!destDir.mkdir()){
System.out.println(&quot;Cound not create dir &quot;+dest);
}
String [] files=fromDir.list();
int stop=files.length;
for(int i=0;i&lt;stop;i++){
String newname=rename(files[i]);
File file=new File(source+&quot;/&quot;+files[i]);
File newFileName=new File(dest+&quot;/&quot;+newname);
if(file.renameTo(newFileName))
System.out.println(&quot;Renamed &quot;+files[i]+&quot; to &quot;+ newname);
}
}
public String rename(String file){
file=file.replace(&#039;à&#039;,&#039;a&#039;);file=file.replace(&#039;á&#039;,&#039;a&#039;);file=file.replace(&#039;ả&#039;,&#039;a&#039;);file=file.replace(&#039;ã&#039;,&#039;a&#039;);
file=file.replace(&#039;ạ&#039;,&#039;a&#039;);file=file.replace(&#039;ă&#039;,&#039;a&#039;);file=file.replace(&#039;ằ&#039;,&#039;a&#039;);file=file.replace(&#039;ắ&#039;,&#039;a&#039;);
file=file.replace(&#039;ẳ&#039;,&#039;a&#039;);file=file.replace(&#039;ẵ&#039;,&#039;a&#039;);file=file.replace(&#039;ặ&#039;,&#039;a&#039;);file=file.replace(&#039;â&#039;,&#039;a&#039;);
file=file.replace(&#039;ầ&#039;,&#039;a&#039;);file=file.replace(&#039;ấ&#039;,&#039;a&#039;);file=file.replace(&#039;ẩ&#039;,&#039;a&#039;);file=file.replace(&#039;ẫ&#039;,&#039;a&#039;);
file=file.replace(&#039;ậ&#039;,&#039;a&#039;);file=file.replace(&#039;À&#039;,&#039;a&#039;);file=file.replace(&#039;Á&#039;,&#039;a&#039;);file=file.replace(&#039;Ả&#039;,&#039;a&#039;);
file=file.replace(&#039;Ã&#039;,&#039;a&#039;);file=file.replace(&#039;Ạ&#039;,&#039;a&#039;);file=file.replace(&#039;Ă&#039;,&#039;a&#039;);file=file.replace(&#039;Ằ&#039;,&#039;a&#039;);
file=file.replace(&#039;Ắ&#039;,&#039;a&#039;);file=file.replace(&#039;Ẳ&#039;,&#039;a&#039;);file=file.replace(&#039;Ẵ&#039;,&#039;a&#039;);file=file.replace(&#039;Ặ&#039;,&#039;a&#039;);
file=file.replace(&#039;Â&#039;,&#039;a&#039;);file=file.replace(&#039;Ầ&#039;,&#039;a&#039;);file=file.replace(&#039;Ấ&#039;,&#039;a&#039;);file=file.replace(&#039;Ẩ&#039;,&#039;a&#039;);
file=file.replace(&#039;Ẫ&#039;,&#039;a&#039;);file=file.replace(&#039;Ậ&#039;,&#039;a&#039;);file=file.replace(&#039;đ&#039;,&#039;d&#039;);file=file.replace(&#039;Đ&#039;,&#039;d&#039;);
file=file.replace(&#039;è&#039;,&#039;e&#039;);file=file.replace(&#039;é&#039;,&#039;e&#039;);file=file.replace(&#039;ẻ&#039;,&#039;e&#039;);file=file.replace(&#039;ẽ&#039;,&#039;e&#039;);
file=file.replace(&#039;ẹ&#039;,&#039;e&#039;);file=file.replace(&#039;ê&#039;,&#039;e&#039;);file=file.replace(&#039;ề&#039;,&#039;e&#039;);file=file.replace(&#039;ế&#039;,&#039;e&#039;);
file=file.replace(&#039;ể&#039;,&#039;e&#039;);file=file.replace(&#039;ễ&#039;,&#039;e&#039;);file=file.replace(&#039;ệ&#039;,&#039;e&#039;);file=file.replace(&#039;È&#039;,&#039;e&#039;);
file=file.replace(&#039;É&#039;,&#039;e&#039;);file=file.replace(&#039;Ẻ&#039;,&#039;e&#039;);file=file.replace(&#039;Ẽ&#039;,&#039;e&#039;);file=file.replace(&#039;Ẹ&#039;,&#039;e&#039;);
file=file.replace(&#039;Ê&#039;,&#039;e&#039;);file=file.replace(&#039;Ề&#039;,&#039;e&#039;);file=file.replace(&#039;Ế&#039;,&#039;e&#039;);file=file.replace(&#039;Ể&#039;,&#039;e&#039;);
file=file.replace(&#039;Ễ&#039;,&#039;e&#039;);file=file.replace(&#039;Ệ&#039;,&#039;e&#039;);file=file.replace(&#039;ì&#039;,&#039;i&#039;);file=file.replace(&#039;í&#039;,&#039;i&#039;);
file=file.replace(&#039;ỉ&#039;,&#039;i&#039;);file=file.replace(&#039;ĩ&#039;,&#039;i&#039;);file=file.replace(&#039;ị&#039;,&#039;i&#039;);file=file.replace(&#039;Ì&#039;,&#039;i&#039;);
file=file.replace(&#039;Í&#039;,&#039;i&#039;);file=file.replace(&#039;Ỉ&#039;,&#039;i&#039;);file=file.replace(&#039;Ĩ&#039;,&#039;i&#039;);file=file.replace(&#039;Ị&#039;,&#039;i&#039;);
file=file.replace(&#039;ò&#039;,&#039;o&#039;);file=file.replace(&#039;ó&#039;,&#039;o&#039;);file=file.replace(&#039;ỏ&#039;,&#039;o&#039;);file=file.replace(&#039;õ&#039;,&#039;o&#039;);
file=file.replace(&#039;ọ&#039;,&#039;o&#039;);file=file.replace(&#039;ô&#039;,&#039;o&#039;);file=file.replace(&#039;ồ&#039;,&#039;o&#039;);file=file.replace(&#039;ố&#039;,&#039;o&#039;);
file=file.replace(&#039;ổ&#039;,&#039;o&#039;);file=file.replace(&#039;ỗ&#039;,&#039;o&#039;);file=file.replace(&#039;ộ&#039;,&#039;o&#039;);file=file.replace(&#039;ơ&#039;,&#039;o&#039;);
file=file.replace(&#039;ờ&#039;,&#039;o&#039;);file=file.replace(&#039;ớ&#039;,&#039;o&#039;);file=file.replace(&#039;ở&#039;,&#039;o&#039;);file=file.replace(&#039;ỡ&#039;,&#039;o&#039;);
file=file.replace(&#039;ợ&#039;,&#039;o&#039;);file=file.replace(&#039;Ò&#039;,&#039;o&#039;);file=file.replace(&#039;Ó&#039;,&#039;o&#039;);file=file.replace(&#039;Ỏ&#039;,&#039;o&#039;);
file=file.replace(&#039;Õ&#039;,&#039;o&#039;);file=file.replace(&#039;Ọ&#039;,&#039;o&#039;);file=file.replace(&#039;Ô&#039;,&#039;o&#039;);file=file.replace(&#039;Ồ&#039;,&#039;o&#039;);
file=file.replace(&#039;Ố&#039;,&#039;o&#039;);file=file.replace(&#039;Ổ&#039;,&#039;o&#039;);file=file.replace(&#039;Ỗ&#039;,&#039;o&#039;);file=file.replace(&#039;Ộ&#039;,&#039;o&#039;);
file=file.replace(&#039;Ơ&#039;,&#039;o&#039;);file=file.replace(&#039;Ờ&#039;,&#039;o&#039;);file=file.replace(&#039;Ớ&#039;,&#039;o&#039;);file=file.replace(&#039;Ở&#039;,&#039;o&#039;);
file=file.replace(&#039;Ỡ&#039;,&#039;o&#039;);file=file.replace(&#039;Ợ&#039;,&#039;o&#039;);file=file.replace(&#039;ù&#039;,&#039;u&#039;);file=file.replace(&#039;ú&#039;,&#039;u&#039;);
file=file.replace(&#039;ủ&#039;,&#039;u&#039;);file=file.replace(&#039;ũ&#039;,&#039;u&#039;);file=file.replace(&#039;ụ&#039;,&#039;u&#039;);file=file.replace(&#039;ư&#039;,&#039;u&#039;);
file=file.replace(&#039;ừ&#039;,&#039;u&#039;);file=file.replace(&#039;ứ&#039;,&#039;u&#039;);file=file.replace(&#039;ử&#039;,&#039;u&#039;);file=file.replace(&#039;ữ&#039;,&#039;u&#039;);
file=file.replace(&#039;ự&#039;,&#039;u&#039;);file=file.replace(&#039;Ù&#039;,&#039;u&#039;);file=file.replace(&#039;Ú&#039;,&#039;u&#039;);file=file.replace(&#039;Ủ&#039;,&#039;u&#039;);
file=file.replace(&#039;Ũ&#039;,&#039;u&#039;);file=file.replace(&#039;Ụ&#039;,&#039;u&#039;);file=file.replace(&#039;Ư&#039;,&#039;u&#039;);file=file.replace(&#039;Ừ&#039;,&#039;u&#039;);
file=file.replace(&#039;Ứ&#039;,&#039;u&#039;);file=file.replace(&#039;Ử&#039;,&#039;u&#039;);file=file.replace(&#039;Ữ&#039;,&#039;u&#039;);file=file.replace(&#039;Ự&#039;,&#039;u&#039;);
file=file.replace(&#039;ỳ&#039;,&#039;y&#039;);file=file.replace(&#039;ý&#039;,&#039;y&#039;);file=file.replace(&#039;ỷ&#039;,&#039;y&#039;);file=file.replace(&#039;ỹ&#039;,&#039;y&#039;);
file=file.replace(&#039;ỵ&#039;,&#039;y&#039;);file=file.replace(&#039;Y&#039;,&#039;y&#039;);file=file.replace(&#039;Ỳ&#039;,&#039;y&#039;);file=file.replace(&#039;Ý&#039;,&#039;y&#039;);
file=file.replace(&#039;Ỷ&#039;,&#039;y&#039;);file=file.replace(&#039;Ỹ&#039;,&#039;y&#039;);file=file.replace(&#039;Ỵ&#039;,&#039;y&#039;);
return file;
}
}
</pre>
<p>Còn đây là thành quả</p>
<p><strong>Nhạc phim</strong></p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="300" height="300" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashvars" value="&amp;config=http://as247.vui360.com/blog/wp-content/uploads/player/config.xml&amp;file=http://as247.vui360.com/blog/wp-content/uploads/player/list/nhacphim.xml" /><param name="src" value="http://as247.vui360.com/blog/wp-content/uploads/player/mp3player.swf" /><param name="wmode" value="transparent" /><param name="quality" value="high" /></object></p>
]]></content:encoded>
			<wfw:commentRss>http://as247.vui360.com/blog/loai-bo-dau-tieng-viet-trong-ten-file/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Ép kiểu trong C++</title>
		<link>http://as247.vui360.com/blog/ep-kieu-trong-c/</link>
		<comments>http://as247.vui360.com/blog/ep-kieu-trong-c/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 14:45:33 +0000</pubDate>
		<dc:creator>As</dc:creator>
				<category><![CDATA[ICT]]></category>
		<category><![CDATA[Kinh nghiệm]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://as247.vui360.com/blog/?p=447</guid>
		<description><![CDATA[Ép các kiểu có sẵn trong C++: int, float, long, double&#8230; cái này dễ chẳng nói làm gì Mỗi class (lớp) có thể được xem như một kiểu dữ liệu mới, trong khi tính toán đôi khi chúng ta cần phải ép kiểu từ một lớp về các kiểu có sẵn. Ví dụ như chúng ta xây dựng lớp [...]]]></description>
			<content:encoded><![CDATA[<p>Ép các kiểu có sẵn trong C++: <strong>int</strong>, <strong>float, long, double</strong>&#8230; cái này dễ chẳng nói làm gì  <img src='http://as247.vui360.com/blog/wp-includes/images/smilies/4.gif' alt=':d' class='wp-smiley' /> </p>
<p>Mỗi<strong> class </strong>(lớp)<strong> </strong>có thể được xem như một kiểu dữ liệu mới, trong khi tính toán đôi khi chúng ta cần phải ép kiểu từ một lớp về các kiểu có sẵn. Ví dụ như chúng ta xây dựng lớp SOTHUC (số thực) chẳng hạn với các phép toán &#8220;cộng&#8221;, &#8220;trừ&#8221;, &#8220;nhân&#8221;, &#8220;chia&#8221; rõ ràng chúng ta không thể thực hiện các phép toán vừa xây xựng giữa kiểu SOTHUC với các kiểu có sẵn như <strong>int</strong>, <strong>float</strong>,&#8230; để làm được điều này chúng ta có 2 cách: nạp chồng thêm các toán tử để cộng kiểu SOTHUC với các kiểu có sẵn, hoặc &#8220;ép kiểu&#8221; <strong>class</strong> SOTHUC (số thực) về kiểu <strong>int</strong>, <strong>float</strong>&#8230; để thực hiện tính toán.</p>
<p>Để ép kiểu với <strong>class</strong>: Ép từ <strong>class</strong> về 1 kiểu có sẵn hoặc ép từ 1 kiểu có sẵn về <strong>class</strong> với trình biên dịch của <a href="http://blogfiles.vui360.com/prog/devcpp-4.9.9.2_setup.zip">Dev-C++</a> ta làm như sau:<span id="more-447"></span></p>
<p> - Ép từ class về 1 kiểu có sẵn:</p>
<blockquote><p><strong>class</strong> &lt;tên lớp&gt;<br />
{<br />
   &lt;kiểu dữ liệu&gt; tênbiến;<br />
   <strong>public</strong>:<br />
            &#8230;<br />
            <strong>operator</strong> &lt;kiểu dữ liệu có sẵn&gt;()<br />
            {<br />
                   &#8230;.<br />
                   <strong>return</strong> &lt;Giá trị có kiểu dữ liệu cần chuyển về&gt;;<br />
            }<br />
}</p></blockquote>
<p>Sử dụng như sau:</p>
<p> &lt;tên lớp&gt; ex;//đối tượng ex thuộc lớp đã khai báo</p>
<p>bây giờ cần ép đối tượng ex về 1 kiểu có sẵn trong C++ ta dùng như sau:</p>
<p>&lt;kiểu dữ liệu có sẵn&gt; ex1;</p>
<p>ex1=&lt;kiểu dữ liệu có sẵn&gt; (ex);/*thực hiện ép kiểu của ex về &lt;kiểu dữ liệu có sẵn&gt;(chính là kiểu của ex1) sau đó gán cho ex1*/<br />
Hoặc dùng<br />
ex1=ex; /*Chương trình sẽ tự động ép kiểu của ex về &lt;kiểu dữ liệu có sẵn&gt;(chính là kiểu của ex1) sau đó gán cho ex1*/</p>
<p>Để dễ hiểu hơn, xem ví dụ sau:</p>
<p>Giả sử ta xây dựng 1 lớp số nguyên lớn như sau:</p>
<blockquote><p><strong>class</strong> largeInt<br />
{<br />
   <strong>char</strong> *value;<br />
   <strong>public:</strong><br />
          largeInt();<br />
          largeInt(<strong>const</strong> largeInt &amp; );<br />
          ~largeInt();  <br />
          <strong>friend</strong> istream &amp;<strong>operator</strong>&gt;&gt;(istream &amp;, largeInt &amp; );<br />
          <strong>friend</strong> ostream &amp;<strong>operator</strong>&lt;&lt;(ostream &amp;, <strong>const</strong> largeInt &amp; );<br />
          largeInt <strong>operator</strong>+(<strong>const </strong>largeInt &amp; );<br />
          largeInt <strong>operator</strong>-(<strong>const </strong>largeInt &amp; );<br />
          largeInt <strong>operator</strong>-();<br />
          largeInt <strong>operator</strong>*(<strong>const</strong> char &amp; );<br />
          largeInt <strong>operator</strong>*(<strong>const </strong>largeInt &amp; );<br />
          largeInt <strong>operator</strong>/(<strong>const </strong>largeInt &amp; );<br />
          <strong>operator</strong> <strong>int</strong>();//ep sang kieu <strong>int</strong><br />
};</p></blockquote>
<p>các toán tử +,-,*,/ mà ta đã nạp chồng chỉ sử dụng đc cho 2 đối tượng của cùng lớp largeInt:</p>
<p>Khi chưa có toán tử ép kiểu:</p>
<blockquote><p>largeInt x1,x2,x; cin&gt;&gt;x1&gt;&gt;x2; <strong>int</strong> y,y1;<br />
x=x1+x2<em>;// thực hiện được</em><br />
y=x1; <em>//không thực hiện được</em><br />
y=x1+9; <em>//không thực hiện được</em><br />
y=9+x1; <em>//không thực hiện được</em><br />
x=y1; <em>//không thực hiện được</em></p></blockquote>
<p>Khi đã có toán tử ép kiểu:</p>
<blockquote><p>largeInt x1,x2,x; cin&gt;&gt;x1&gt;&gt;x2; <strong>int</strong> y,y1;<br />
x=x1+x2; <em>//thực hiện được</em><br />
y=x1; <em>//thực hiện được chương trình sẽ tự động ép kiểu x1 về <strong>int</strong><br />
</em>y=x1+9; <em>/*không thực hiện được, để thực hiện đc cần ép kiểu của x1 về <strong>int:</strong></em><strong> </strong>y=<strong>int</strong>(x1)+9<strong> </strong><em>*/<br />
</em>y=9+x1<em>;/* Cái này thì thực hiện được, chương trình tự động ép kiểu của x1 về <strong>int</strong>*/</em><br />
x=y1; <em>//<strong>vẫn không thực hiện được</strong></em></p></blockquote>
<p>- Ép từ 1 kiểu có sẵn về một <strong>class</strong>(lớp) chính là kiểu dữ liệu mà ta định nghĩa:</p>
<p>C++ không cho phép định nghĩa thêm toán tử mới vì vậy ta không thể áp dụng cách trên để ép kiểu cho trường hợp này:</p>
<p>Cách khai báo:</p>
<blockquote><p><strong>class</strong> largeInt<br />
{<br />
   <strong>char</strong> *value;<br />
   <strong>public:</strong><br />
          &#8230;<br />
          <strong>operator</strong> <strong>int</strong>();//ep sang kieu <strong>int</strong><br />
          <strong>operator</strong> largeInt(); //Không hợp lệ         <br />
};</p></blockquote>
<p>Là không hợp lệ và bị lỗi ở <strong>operator</strong> largeInt();</p>
<p>Vậy phải làm sao để ép một kiểu dữ liệu sẵn có về một lớp? Chúng ta có hai cách: Dùng hàm bạn(<strong>friend</strong>) để ép kiểu và dùng hàm tạo (constructor) để ép kiểu. Nếu dùng hàm bạn để ép kiểu thì tên hàm không được trùng với tên lớp, đây là điểm yếu của cách này, giả sử trong lớp số nguyên lớn(largeInt) ở trên, để ép một số x thuộc kiểu <strong>int </strong>về kiểu largeInt chúng ta chỉ có thể sử dụng largeint(x), convertToLargeInt(x)&#8230; chứ không thể sử dụng largeInt(x), thật bất tiện. Vì vậy chúng ta sử dụng constructor để ép kiểu, cách khai báo như sau:</p>
<blockquote><p><strong>class</strong> &lt;tên lớp&gt;<br />
{<br />
   &lt;kiểu dữ liệu&gt; tênbiến;<br />
   <strong>public</strong>:<br />
            &#8230;<br />
            &lt;tên lớp&gt;(&lt;kiểu dữ liệu có sẵn&gt; ) <br />
              {<br />
                      cout&lt;&lt;&#8221;ép kiểu dữ liệu có sẵn về kiểu lớp&#8221;;<br />
              }<br />
}</p></blockquote>
<p>Quay trở lại ví dụ về lớp số nguyên lớn ở trên ta thấy</p>
<p>Khi chưa có constructor &#8221;ép kiểu&#8221;</p>
<blockquote><p>largeInt x1,x2,x; <strong>int</strong> y,y1;<br />
&#8230;<br />
x=y1; <em>//<strong> không thực hiện được</strong></em></p></blockquote>
<p>Khi đã có constructor &#8220;ép kiểu&#8221;</p>
<blockquote><p>largeInt x1,x2,x; <strong>int</strong> y,y1;<br />
&#8230;<br />
x=y1; <em>//<strong> Thực hiện được,</strong> chương trình tự động ép kiểu <strong>int </strong>về largeInt</em></p></blockquote>
<p>Lưu ý: constructor chỉ tự động ép kiểu trong khi gán, vì vậy</p>
<blockquote><p>largeInt x1,x2,x; <strong>int</strong> y,y1,y2;<br />
&#8230;<br />
x=x1+y1;//không thực hiện đc<br />
x=x1+largeInt(y1);//thực hiện được<br />
x=y1+x1;/* thực hiện được, <strong>tự động</strong> ép x1 về <strong>int</strong>, tính tổng, <strong>tự động</strong> ép kết quả sang largeInt và gán cho x*/<br />
x=y1+y2;//thực hiện được</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://as247.vui360.com/blog/ep-kieu-trong-c/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

