C++Builder ,OOP

489 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
489
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C++Builder ,OOP

  1. 1. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫ﺳﺌﻮال‬ ‫ﺑﺮﻧﺎﻣﻪ ﻧﻮﻳﺴﻲ ﺷﻲ ﮔﺮا ﻳﻌﻨﻲ ﭼﻪ و ﭼﻪ ﺗﻔﺎوﺗﻲ ﺑﺎ ﺑﺮﻧﺎﻣﻪ ﻧﻮﻳﺴﻲ ﺳﺎﺧﺖ ﻳﺎﻓﺘﻪ دارد؟‬ ‫ﻣﻨﻈﻮر از ﻛﻼس در ﺑﺮﻧﺎﻣﻪ ﻧﻮﻳﺴﻲ ﺷﻲ ﮔﺮا ﭼﻴﺴﺖ‬‫وﻗﺘﻲ ﮔﻔﺘﻪ ﻣﻲ ﺷﻮد ﻛﻪ ﻳﻚ زﺑﺎن ﺷﻲ ﮔﺮا ﻫﺴﺖ ﻣﻨﻈـﻮر اﻳـﻦ اﺳـﺖ ﻛـﻪ اﻳـﻦ زﺑـﺎن ﺳـﻪ وﻳﮋﮔـﻲ زﻳـﺮ را ﭘﺸـﺘﻴﺒﺎﻧﻲ ﻣـﻲ ﻛﻨـﺪ:‬ ‫1.ﻛﭙﺴﻮﻟﻪ ﺳﺎزي:‪encapsulation‬‬ ‫2.وراﺛﺖ:‪Inheritance‬‬ ‫3.ﭼﻨﺪ رﻳﺨﺘﻲ:‪polymorphism‬‬ ‫ﻛﭙﺴﻮﻟﻪ ﺳﺎزي:‬‫ﺑﺮﻧﺎﻣﻪ ﻧﻮﻳﺴﻲ ﺷﻲ ﮔﺮا )‪ Oriented Programming Object‬ﻳﺎ ‪ (OOP‬داده ﻫﺎ )ﺧﺼﻮﺻﻴﺎت( و ﺗﻮاﺑﻊ)رﻓﺘﺎر( را در‬‫ﺑﺴﺘﻪ ﻫﺎﺋﻲ ﺑﻪ ﻧﺎم ﻛﻼس ﻣﺤﺼﻮر ﻣﻲ ﻛﻨﺪ.و از اﻳﻦ ﻃﺮﻳﻖ ﺟﺰﺋﻴﺎت ﭘﻴﺎده ﺳﺎزي در داﺧﻞ ﺧﻮد ﻛﻼس ﻫﺎ ﭘﻨﻬﺎن ﻫﺴﺘﻨﺪ.)ﻓﻘﻂ اﺷﻴﺎء‬‫ﻛﻼ ﺳﻬﺎي دﻳﮕﺮ ﻣﻲ داﻧﻨﺪ ﻛﻪ ﻓﻼن ﺷﻲ از ﻓﻼن ﻛﻼس ،ﻓﻼن رﻓﺘﺎر را اﻧﺠﺎم ﻣﻴﺪﻫﺪ وﻟﻲ ﻧﻤﻲ داﻧﻨﺪ ﻛﻪ اﻳﻦ ﺷﻲ ﭼﮕﻮﻧﻪ اﻳﻦ رﻓﺘـﺎر‬ ‫را اﻧﺠﺎم ﻣﻲ دﻫﺪ(‬ ‫وراﺛﺖ:‬‫ﻳﻌﻨﻲ ﻳﻚ ﻛﻼس از ﻳﻚ ﻛﻼس دﻳﮕﺮ ارث ﻣﻲ ﺑﺮد.ارث ﺑﺮي ﺻﻮرﺗﻲ از ﻗﺎﺑﻠﻴﺖ اﺳﺘﻔﺎده ﻣﺠﺪد اﺳﺖ.ﻓﺮض ﻛﻨﻴﺪ ﻳﻚ ﻛﻼس ﺑـﻪ ﻧـﺎم‬‫داﻧﺸﺠﻮ دارﻳﺪ ﻛﻪ رﻓﺘﺎر و ﺧﺼﻮﺻﻴﺎﺗﻲ را ﺑﺮاﻳﺶ ﺗﻌﺮﻳﻒ ﻛﺮده اﻳﺪ ﺣﺎﻻ ﻣﻲ ﺧﻮاﻫﻴﺪ ﻳﻚ ﻛﻼس داﻧﺸﺠﻮي ﻛﺎرﺷﻨﺎﺳﻲ ﺗﻌﺮﻳﻒ ﻛﻨﻴـﺪ‬‫. ﻓﻜﺮ ﻣﻲ ﻛﻨﻴﺪ ﻛﺪام روش ﻣﻨﺎﺳﺒﺘﺮ اﺳﺖ : ﻳﻚ ﻛﻼس ﻛﺎﻣﻼ ﺟﺪﻳﺪ ﺗﻌﺮﻳﻒ ﻛﻨﻴﺪ ﻳﺎ اﻳﻨﻜﻪ ﻛﻼس داﻧﺸﺠﻮي ﻛﺎرﺷﻨﺎﺳﻲ را ﻧـﻮﻋﻲ از‬ ‫داﻧﺸﺠﻮ ﻗﺮار ﺑﺪﻫﻴﺪ.‬‫اﮔﺮ روش دوم را اﻧﺘﺨﺎب ﻛﻨﻴﺪ ﻳﻚ ﺑﺮﻧﺎﻣﻪ ﻧﻮﻳﺲ ﺣﺮﻓﻪ اي ﻫﺴﺘﻴﺪ. ﺑﺎ اﻳﻦ ﻛﺎر ﻛﻼس داﻧﺸﺠﻮي ﻛﺎرﺷﻨﺎﺳﻲ از ﻛﻼس داﻧﺸـﺠﻮ ارث‬‫ﻣﻲ ﺑﺮد ﻳﻌﻨﻲ ﻛﻼس داﻧﺸﺠﻮي ﻛﺎرﺷﻨﺎﺳﻲ ﺗﻤﺎم ﺧﺼﻮﺻﻴﺎت ﻳﻚ داﻧﺸﺠﻮ را ﺑﻪ ارث ﻣﻲ ﺑﺮد و ﻋﻼوه ﺑﺮ آن ﻣﻲ ﺗﻮاﻧﻴﺪ ﺧﺼﻮﺻـﻴﺎت‬ ‫و رﻓﺘﺎر ﻫﺎي دﻳﮕﺮي را ﻋﻼوه ﺑﺮ ﺧﺼﻮﺻﻴﺎت داﻧﺸﺠﻮ ،ﻛﻪ ﻣﺨﺘﺺ ﻳﻚ داﻧﺸﺠﻮي ﻛﺎرﺷﻨﺎﺳﻲ اﺳﺖ ﺑﻪ آن اﺿﺎﻓﻪ ﻛﻨﻴﺪ.‬‫در اﻳﻦ ﺣﺎﻟﺖ ﺑﻪ ﻛﻼس داﻧﺸﺠﻮ >ﻛﻼس واﻟﺪ‪ class parent‬ﻳﺎ ﭘﺎﻳﻪ <و ﺑﻪ ﻛﻼس داﻧﺸـﺠﻮي ﻛﺎرﺷﻨﺎﺳـﻲ >ﻛـﻼس ﻣﺸـﺘﻖ‬ ‫ﺷﺪه‪ < drived class‬ﮔﻔﺘﻪ ﻣﻲ ﺷﻮد .دو ﻧﻮع ارث ﺑﺮي دارﻳﻢ:ﻳﮕﺎﻧﻪ و ﭼﻨﺪ ﮔﺎﻧﻪ‬ ‫ﭼﻨﺪ رﻳﺨﺘﻲ:‬‫ﻓﺮض ﻛﻨﻴﺪ ﻣﺠﻤﻮﻋﻪ اي از ﻛﻼس ﻫﺎي ﻫﻨﺪﺳﻲ ﻣﺜﻞ داﻳﺮه و ﻣﺜﻠﺚ و ﻣﺴﺘﻄﻴﻞ دارﻳﺪ ﻛﻪ ﻫﻤـﻪ از ﻛـﻼس ﭘﺎﻳـﻪ‪ shape‬ﻣﺸـﺘﻖ‬‫ﺷﺪه ، ﻫﺮ ﻛﺪام از اﻳﻦ ﻛﻼﺳﻬﺎ ﻓﺮﻣﻮل رﻳﺎﺿﻲ ﺧﺎص ﺧﻮد را ﺑﺮاي ﻣﺤﺎﺳﺒﻪ ﻣﺴـﺎﺣﺖ دارﻧـﺪ. ﺣـﺎل ﻓـﺮض ﻛﻨﻴـﺪ در ﻛـﻼس واﻟـﺪ،‬‫رﻓﺘﺎر)ﺗﺎﺑﻊ( ‪ area‬ﺗﻌﺮﻳﻒ ﺷﺪه ﺑﺎﺷﺪ در ﻧﺘﻴﺠﻪ ﻫﺮ ﻛﺪام از ﻛﻼﺳﻬﺎي ﻣﺸﺘﻖ ﺷﺪه ﺗﺎﺑﻊ‪ area‬ﻣﺨﺼـﻮص ﺑـﻪ ﺧﻮدﺷـﺎن را دارﻧـﺪ‬‫وﻟــﻲ ﻧــﺎم ﺗﻤــﺎم آﻧﻬــﺎ ﻫﻤــﺎن ‪ area‬ﻣﺮﺑــﻮط ﺑــﻪ ﻛــﻼس واﻟــﺪ اﺳــﺖ.اﻳــﻦ اﻣﻜــﺎن ﺑــﺎ اﺳــﺘﻔﺎده از ﭘﺸــﺘﻴﺒﺎﻧﻲ ﻳــﻚ زﺑــﺎن از‬ ‫‪ polymorphism‬ﺑﻪ وﺟﻮد ﻣﻲ آﻳﺪ.‬ ‫ﺗﻌﺮﺑﻒ ﺷﻲ:‬ ‫ﻳﻚ ﺷﻲ ﺷﺎﻣﻞ ﻣﺠﻤﻮﻋﻪ اي از ﻣﺘﺪﻫﺎ و در ﺑﺮﺧﻲ ﻣﻮارد ﺷﺎﻣﻞ وﻳﮋﮔﻲ ﻫﺎ و رﺧﺪادﻫﺎﺳﺖ‬ ‫ﺗﻌﺮﻳﻒ ﻛﻨﺘﺮل:‬ ‫ﻛﻨﺘﺮل ﺑﻪ اﺷﻴﺎﻳﻲ ﮔﻔﺘﻪ ﻣﻲ ﺷﻮد ﻛﻪ ﺑﺮ روي ﻓﺮم ﻧﻤﺎﻳﺎن ﻣﻲ ﺷﻮد و در زﻣﺎن اﺟﺮا ﻛﺎرﺑﺮ ﻣﻲ ﺗﻮاﻧﺪ ﺑﺎ آﻧﻬﺎ ﺗﻌﺎﻣﻞ داﺷﺘﻪ ﺑﺎﺷﺪ‬ ‫ﺗﻌﺮﻳﻒ ﻛﺎﻣﭙﻮﻧﻨﺖ:‬ ‫ﺻﻔﺤﻪ 1‬
  2. 2. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫ﻛﺎﻣﭙﻮﻧﻨﺖ ﻫﺎ اﺷﻴﺎي ﺧﺎﺻﻲ ﻫﺴﺘﻨﺪ ﻛﻪ در زﻣﺎن ﻃﺮاﺣﻲ ﻣﻲ ﺗﻮان وﻳﮋﮔﻲ ﻫﺎي آﻧﻬﺎ را دﺳﺘﻜﺎري و ﺗﻐﻴﻴﺮ داد‬ ‫وﻳﮋﮔﻲ :‬ ‫ﻣﺠﻤﻮﻋﻪ اي از داده ﻫﺎﺳﺖ ﻛﻪ ﻳﻚ ﺷﻲ در ﻃﻮل دوره ﺣﻴﺎت ﺧﻮد ﻧﮕﻬﺪاري ﻣﻲ ﻛﻨﺪ‬ ‫ﻣﺘﺪ :‬ ‫ﻋﺒﺎرت اﺳﺖ از ﻣﺠﻤﻮﻋﻪ اي از اﻋﻤﺎل ﻛﻪ ﻳﻚ ﺷﻲ ﻗﺎدر ﺑﻪ اﻧﺠﺎم آن ﻣﻲ ﺑﺎﺷﺪ‬ ‫رﺧﺪاد:‬ ‫ﻋﺒﺎرت اﺳﺖ از ﻣﺠﻤﻮﻋﻪ اي از وﻗﺎﻳﻊ ﻛﻪ ﻳﻚ ﺷﻲ ﻗﺎدر ﺑﻪ ﺗﺸﺨﻴﺺ آن ﻣﻲ ﺑﺎﺷﺪ‬ ‫ﺗﻌﺮﻳﻒ ﻛﻼس‬‫ﻛﻼس ﻳﻚ ﺳﺎﺧﺘﺎر داده اي ﭘﻴﭽﻴﺪه اﺳﺖ ﻛﻪ ﺷﺎﻣﻞ ﻣﺠﻤﻮﻋﻪ اي از داده ﻫﺎ و ﺗﻮاﺑﻊ ﻣﻮرد ﻧﻴﺎز ﺑـﺮاي ﻛـﺎر روي داده ﻫـﺎ‬‫ﻣﻲ ﺑﺎﺷﺪ . ﻛﻼس ﻫﺎ ﺑﻪ ﻣﺎ اﻳﻦ اﻣﻜﺎن را ﻣﻲ دﻫﻨﺪ ﺗﺎ ﺑﺘﻮاﻧﻴﻢ ﻣﺠﻤﻮﻋـﻪ داده و ﺗﻮاﺑـﻊ و ﺗﻜﻨﻮﻟـﻮژي ﻛـﺎر ﺑـﺎ آن داده را ﺑـﻪ ﺻـﻮرت‬‫ﻛﭙﺴﻮﻟﻪ درآورﻳﻢ . ﺑﻪ داده ﻫﺎ و ﺗﻮاﺑﻊ ﻳﻚ ﻛﻼس اﻋﻀﺎي ﻛﻼس ﻣﻲ ﻛﻮﻳﻨﺪ. اﻳﻦ اﻋﻀﺎ ﻣﻲ ﺗﻮاﻧﻨﺪ ﺑﻪ ﺻﻮرت ﺧﺼﻮﺻﻲ ، ﻋﻤﻮﻣﻲ و ﻳﺎ‬ ‫ﻣﺠﺎزي ﺑﺎﺷﻨﺪ.‬‫ﻳﻚ ﻋﻀﻮ ﺧﺼﻮﺻﻲ ﻣﺨﺘﺺ ﻫﻤﺎن ﻛﻼﺳﻲ اﺳﺖ ﻛﻪ ﻣﺘﻌﻠﻖ ﺑﻪ آن اﺳﺖ و دﺳﺘﺮﺳﻲ ﺑﻪ آن در ﺧﺎرج از ﻣﺤـﺪوده ﻛـﻼس‬‫اﻣﻜﺎن ﭘﺬﻳﺮ ﻧﻴﺴﺖ )ﻓﻘﻂ ﺗﻮاﺑﻊ ﻋﻀﻮ ﻛﻼس ﻣﻲ ﺗﻮاﻧﻨﺪ ﺑﻪ ﻋﻀﻮﻫﺎي ﺧﺼﻮﺻـﻲ ﻛـﻼس دﺳﺘﺮﺳـﻲ داﺷـﺘﻪ ﺑﺎﺷـﻨﺪ( ﺑﺨـﺶ ﺗﻌـﺎرﻳﻒ‬‫ﺧﺼﻮﺻﻲ ﻳﻚ ﻛﻼس ﺑﺎ ﻛﻠﻴﺪ واژه ‪ private‬ﺷﺮوع ﻣﻲ ﺷﻮد . در ﺻﻮرﺗﻲ ﻛﻪ ﻋﻀﻮ ﻋﻤـﻮﻣﻲ ﻣـﻲ ﺗﻮاﻧـﺪ ﺑـﻪ ارث داده ﺷـﻮد و‬‫دﺳﺘﺮﺳﻲ ﺑﻪ آن ﻫﻢ در ﻛﻠﻴﻪ ﻣﺸﺘﻘﺎت ﻛﻼس اﻣﻜﺎن ﭘﺬﻳﺮ اﺳﺖ ﺑﺨﺶ ﺗﻌﺎرﻳﻒ ﻋﻤﻮﻣﻲ ﻳﻚ ﻛﻼس ﺑﺎ ﻛﻠﻴﺪ واژه ‪ public‬ﺷـﺮوع‬ ‫ﻣﻲ ﺷﻮد.‬ ‫ﻳﻚ ﻋﻀﻮ ﻣﺠﺎزي ﻣﻲ ﺗﻮاﻧﺪ ﺗﻮﺳﻂ وارث روﻧﻮﻳﺴﻲ ﺷﻮد و ﻋﻤﻠﻜﺮد آن ﻣﻄﺎﺑﻖ ﻣﻴﻞ و ﺧﻮاﺳﺖ وارث ﺗﻐﻴﻴﺮ داده ﺷﻮد.‬ ‫ﺗﻌﺎرﻳﻒ ﻣﺠﺎزي ﻳﻚ ﻛﻼس ﺑﺎ ﻛﻠﻴﺪ واژه ‪ virtual‬ﺷﺮوع ﻣﻲ ﺷﻮد‬ ‫ﻋﻀﻮ ﻫﺎي ﺳﺎزﻧﺪه و ﻣﺨﺮب در ﺑﻚ ﻛﻼس :‬ ‫ﻋﻀﻮ ﺳﺎزﻧﺪه در ﻳﻚ ﻛﻼس وﻇﻴﻔﻪ ﻣﻘﺪار دﻫﻲ اوﻟﻴﻪ ﻣﻘﺎدﻳﺮ ﻣﺮﺑﻮط ﺑﻪ داده ﻫﺎي ﻛﻼس را ﺑﺮ ﻋﻬﺪه دارد ﻧﺎم ﺳﺎزﻧﺪه‬ ‫)‪ (constructor‬ﺑﺎ ﻧﺎم ﻛﻼس ﻳﻜﺴﺎن اﺳﺖ . ﻳﻚ ﻛﻼس ﻣﻲ ﺗﻮاﻧﺪ ﺑﻴﺶ از ﻳﻚ ﺳﺎزﻧﺪه داﺷﺘﻪ ﺑﺎﺷﺪ‬ ‫ﻋﻀﻮب ﻣﺨﺮب )‪ (destructor‬در ﻳﻚ ﻛﻼس وﻇﻴﻔﻪ آزاد ﺳﺎزي ﺣﺎﻓﻈﻪ در اﺧﺘﻴﺎر ﮔﺮﻓﺘﻪ ﺷﺪه ﺗﻮﺳﻂ داده ﻫﺎي ﻛﻼس را‬ ‫ﺑﺮﻋﻬﺪه دارد. ﻧﺎم ﻋﻀﻮ ﻣﺨﺮب ﻫﻤﺎن ﻧﺎم ﻛﻼس اﺳﺖ ﻛﻪ ﻋﻼﻣﺖ ~ ﻗﺒﻞ از آن ﻗﺮار ﮔﺮﻓﺘﻪ اﺳﺖ‬ ‫ﻫﺮ دوي اﻳﻦ ﻋﻀﻮ ﻫﺎ در زﻳﺮ ﻣﺠﻤﻮﻋﻪ اﻋﻀﺎي ﻋﻤﻮﻣﻲ ﻛﻼس ﻗﺮار دارﻧﺪ.‬ ‫ﺻﻔﺤﻪ 2‬
  3. 3. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫ﭘﺮﺳﺶ ﻫﺎي ﭘﺎﻳﺎن درس‬ ‫1 - ﻛﺪام ﮔﺰﻳﻨﻪ در ﻣﻮرد ﻳﻚ زﺑﺎن ﺷﻲ ﮔﺮا ﺻﺤﻴﺢ ﻣﻲ ﺑﺎﺷﺪ‬‫د (‬ ‫ب ( ﭘﺸﺘﻴﺒﺎﻧﻲ از ﭼﻨﺪ رﻳﺨﺘﻲ ج ( ﭘﺸﺘﻴﺒﺎﻧﻲ از ﻛﭙﺴﻮﻟﻪ ﺳﺎزي‬ ‫اﻟﻒ( ﭘﺸﺘﻴﺒﺎﻧﻲ از وراﺛﺖ‬ ‫ﻫﻤﻪ ﻣﻮارد‬ ‫2 - ﻛﺪام ﮔﺰﻳﻨﻪ در ﻣﻮرد ﺗﻌﺮﻳﻒ ﺷﻲ ﺻﺤﻴﺢ ﻧﻤﻲ ﺑﺎﺷﺪ‬ ‫اﻟﻒ( دﺳﺘﺮﺳﻲ ﺑﻪ اﻋﻀﺎي ﺧﺼﻮﺻﻲ اﺷﻴﺎ ﺗﻮﺳﻂ ﺳﺎﻳﺮ اﺷﻴﺎء دﻳﮕﺮ اﻣﻜﺎن ﭘﺬﻳﺮ ﻧﻤﻲ ﺑﺎﺷﺪ‬ ‫ب ( ﻳﻚ ﺷﻲ ﺷﺎﻣﻞ ﻣﺠﻤﻮﻋﻪ اي از ﻣﺘﺪﻫﺎ و در ﺑﺮﺧﻲ ﻣﻮارد ﺷﺎﻣﻞ وﻳﮋﮔﻲ ﻫﺎ و رﺧﺪادﻫﺎﺳﺖ‬ ‫ج ( ﻳﻚ ﺷﻲ ﻫﻤﺎﻧﻨﺪ ﻳﻚ ﻣﺘﻐﻴﺮ ﻋﻤﻮﻣﻲ اﺳﺖ‬ ‫د ( دﺳﺘﺮﺳﻲ ﺑﻪ اﻋﻀﺎي ﻋﻤﻮﻣﻲ اﺷﻴﺎ ﺗﻮﺳﻂ ﺳﺎﻳﺮ اﺷﻴﺎء دﻳﮕﺮ اﻣﻜﺎن ﭘﺬﻳﺮ ﻣﻲ ﺑﺎﺷﺪ‬ ‫3 - ﺗﻮاﺑﻊ ﻋﻀﻮ در ﻛﻼس .........‬‫ب ( ﻓﻘﻂ ﺑﻪ اﻋﻀﺎي ﻋﻤﻮﻣﻲ دﺳﺘﺮﺳﻲ دارﻧﺪ‬ ‫اﻟﻒ( ﺑﻪ ﻫﻤﻪ اﻋﻀﺎي ﻛﻼس دﺳﺘﺮﺳﻲ دارﻧﺪ‬ ‫د ( ﻓﻘﻂ ﺑﻪ اﻋﻀﺎي ﺧﺼﻮﺻﻲ دﺳﺘﺮﺳﻲ‬ ‫ج ( ﻓﻘﻂ ﺑﻪ اﻋﻀﺎي ﻋﻤﻮﻣﻲ و ﻣﺠﺎزي دﺳﺘﺮﺳﻲ دارﻧﺪ‬ ‫دارﻧﺪ‬ ‫4 - ﻋﻀﻮ ﻋﻤﻮﻣﻲ در ﻛﻼس .........‬ ‫ب ( در ﻣﺸﺘﻘﺎت ﻛﻼس ﻣﻲ ﺗﻮان آن را‬ ‫اﻟﻒ( در ﺗﻤﺎم ﻣﺸﺘﻘﺎت ﻛﻼس در دﺳﺘﺮس ﻣﻲ ﺑﺎﺷﺪ‬ ‫روﻧﻮﻳﺴﻲ ﻛﺮد‬ ‫د ( ﻓﻘﻂ در داﺧﻞ ﻓﻀﺎي ﻧﺎم ﻓﻌﻠﻲ در دﺳﺘﺮس ﻣﻲ‬ ‫ج ( ﻓﻘﻂ در داﺧﻞ ﻛﻼس در دﺳﺘﺮس ﻣﻲ ﺑﺎﺷﺪ‬ ‫ﺑﺎﺷﺪ‬ ‫5- ﻋﻀﻮ ﻣﺠﺎزي در ﻛﻼس .........‬ ‫اﻟﻒ( در ﻛﻼس ﻫﺎي ﻣﺸﺘﻖ ﺷﺪه ﻗﺎﺑﻞ اﺳﺘﻔﺎده اﺳﺖ‬ ‫ب ( ﻣﻲ ﺗﻮاﻧﺪ ﭘﺎراﻣﺘﺮﻫﺎ و ﺷﻜﻞ ﻛﻠﻲ آن ﺗﻮﺳﻂ وارث ﺗﻐﻴﻴﺮ داده ﺷﻮد‬ ‫ج ( وارث ﻧﻤﻲ ﺗﻮاﻧﺪ ﺳﺎﺧﺘﺎر داﺧﻠﻲ ﻋﻀﻮ ﻣﺠﺎزي را ﻣﻄﺎﺑﻖ ﺳﻠﻴﻘﻪ ﺧﻮدش ﺗﻐﻴﻴﺮ دﻫﺪ‬ ‫د ( اﻟﻒ و ج‬ ‫6- ﻋﻀﻮ ﻋﻤﻮﻣﻲ در ﻛﻼس ﺑﺎ ﻛﻠﻴﺪ واژه ......... ﻣﺸﺨﺺ ﻣﻲ ﺷﻮد‬ ‫7- ﻋﻀﻮ ﺧﺼﻮﺻﻲ در ﻛﻼس ﺑﺎ ﻛﻠﻴﺪ واژه ......... ﻣﺸﺨﺺ ﻣﻲ ﺷﻮد‬ ‫8- ﻋﻀﻮ ﻣﺠﺎزي در ﻛﻼس ﺑﺎ ﻛﻠﻴﺪ واژه ......... ﻣﺸﺨﺺ ﻣﻲ ﺷﻮد‬ ‫9- ﻣﺠﻤﻮﻋﻪ اي از داده ﻫﺎﺳﺖ ﻛﻪ ﻳﻚ ﺷﻲ در ﻃﻮل دوره ﺣﻴﺎت ﺧﻮد ﻧﮕﻬﺪاري ﻣﻲ ﻛﻨﺪ.‬ ‫01 -.... ﻋﺒﺎرت اﺳﺖ از ﻣﺠﻤﻮﻋﻪ اي از اﻋﻤﺎل ﻛﻪ ﻳﻚ ﺷﻲ ﻗﺎدر ﺑﻪ اﻧﺠﺎم آن ﻣﻲ ﺑﺎﺷﺪ.‬ ‫11 - .... ﻣﺠﻤﻮﻋﻪ اي از رﺧﺪادﻫﺎ / وﻗﺎﻳﻊ اﺳﺖ ﻛﻪ ﻳﻚ ﺷﻲ ﻗﺎدر ﺑﻪ ﺗﺸﺨﻴﺺ آن ﻣﻲ ﺑﺎﺷﺪ.‬ ‫ﺻﻔﺤﻪ 3‬
  4. 4. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫ﺳﺎﺧﺘﺎر ﻛﻠﻲ ﺗﻌﺮﻳﻒ ﻛﻼس در ++‪ c‬ﺑﻪ ﺷﻜﻞ زﻳﺮ ﻣﻲ ﺑﺎﺷﺪ‬‫;} >‪<classkey> <classname> <baselist> { <member list‬‬ ‫>‪ : <classkey‬ﻳﻜﻲ از ﻛﻠﻴﺪ واژه ﻫﺎي ‪ struct ، class‬و ﻳﺎ ‪ union‬ﻣﻲ ﺑﺎﺷﺪ >‪: <classname‬‬ ‫ﺗﻌﻴﻴﻦ ﻛﻨﻨﺪه ﻧﺎم ﻛﻼس اﺳﺖ . اﻳﻦ ﻧﺎم ﺑﺎﻳﺪ در ﻣﺤﺪوده )‪ (scope‬ﺧﻮدش ﻳﻜﺘﺎ ﺑﺎﺷﺪ‬ ‫>‪ : <baselist‬ﻟﻴﺴﺖ ﻛﻼس ﻫﺎي ﭘﺎﻳﻪ اي اﺳﺖ ﻛﻪ ﻛﻼس ﻣﻮرد ﻧﻈﺮ از آﻧﻬﺎ ﻣﺸﺘﻖ ﺷﺪه اﺳﺖ >‪<baselist‬‬ ‫اﺧﺘﻴﺎري اﺳﺖ و ﻣﻲ ﺗﻮاﻧﺪ اﺳﺘﻔﺎده ﻧﺸﻮد‬ ‫>‪ : <member list‬ﺗﻮاﺑﻊ و داده ﻫﺎي ﻣﺮﺑﻮط ﺑﻪ ﻛﻼس را ﺗﻌﺮﻳﻒ ﻣﻲ ﻛﻨﺪ‬ ‫ﻧﻜﺘﻪ1 : در داﺧﻞ ﻳﻚ ﻛﻼس ﺑﻪ داده ﻫﺎ ، )داده ﻋﻀﻮ ( ‪ Data Members‬و ﺑﻪ ﺗﻮاﺑﻊ ﻣﺮﺑﻮط ﺑﻪ داده ﻫﺎ، )ﺗﻮاﺑﻊ ﻋﻀﻮ(‬‫‪ Member Functions‬ﻣﻲ ﮔﻮﻳﻴﻢ. در واﻗﻊ ﻣﻲ ﺗﻮان ﮔﻔﺖ ﻛﺎر ﺗﻮاﺑﻊ ﻳﻚ ﻛﻼس ﺑﺮ روي داده ﻫﺎي ﻫﻤﺎن ﻛﻼس اﺳﺖ .‬ ‫ﻧﻜﺘﻪ 2: ﮔﺬاﺷﺘﻦ ﻋﻼﻣﺖ ﺳﻤﻲ ﻛﺎﻟﻦ ); ( اﻧﺘﻬﺎﻳﻲ در ﺗﻌﺮﻳﻒ ﻛﻼس اﻟﺰاﻣﻲ اﺳﺖ.‬ ‫ﻣﺜﺎل : ﺗﻌﺮﻳﻒ ﻳﻚ ﻛﻼس ﺑﻪ ﻧﺎم ‪Test‬‬‫‪class‬‬ ‫‪Test‬‬‫{‬‫…‬‫;}‬ ‫ﻧﻤﻮﻧﻪ ﺳﺎزي از ﻛﻼس‬‫ﺑﺮاي اﺳﺘﻔﺎده از ﻛﻼس ﺑﺎﻳﺪ ﻧﻤﻮﻧﻪ اي از ﻛﻼس را اﻳﺠﺎد ﻛﻨﻴﺪ. اﻳﺠﺎد ﻧﻤﻮﻧﻪ اي از ﻛﻼس را ﻧﻤﻮﻧﻪ ﺳﺎزي و ﻧﻤﻮﻧﻪ اﻳﺠﺎد ﺷﺪه از‬ ‫ﻛﻼس را ﺷﻲ ﻣﻲ ﮔﻮﻳﻨﺪ.‬ ‫ﺷﻜﻞ ﻛﻠﻲ دﺳﺘﻮر اﻳﺠﺎد ﻧﻤﻮﻧﻪ از ﻳﻚ ﻛﻼس ﺑﻪ ﺻﻮرت زﻳﺮ ﻣﻲ ﺑﺎﺷﺪ‬ ‫در ﺻﻮرﺗﻲ ﻛﻪ ﺑﺨﻮاﻫﻴﻢ ﻣﺘﻐﻴﺮ ﻧﻤﻮﻧﻪ از ﻧﻮع اﺷﺎره ﮔﺮ ﺑﺎﺷﺪ از ﻓﺮم زﻳﺮ اﺳﺘﻔﺎده ﻣﻲ ﻛﻨﻴﻢ‬‫;)(‪ClassName *objectName=new ClassName‬‬ ‫در ﺻﻮرﺗﻲ ﻛﻪ ﺑﺨﻮاﻫﻴﻢ ﻣﺘﻐﻴﺮ ﻧﻤﻮﻧﻪ از ﻧﻮع اﺷﺎره ﮔﺮ ﻧﺒﺎﺷﺪ از ﻓﺮم زﻳﺮ اﺳﺘﻔﺎده ﻣﻲ ﻛﻨﻴﻢ‬‫;‪ClassName objectName‬‬ ‫ﻛﻪ در آن ‪ className‬ﻧﺎم ﻛﻼس و ‪ objectName‬ﻧﺎم ﻧﻤﻮﻧﻪ / ﺷﻲ ﻣﻲ ﺑﺎﺷﺪ.‬ ‫ﻣﺜﺎل : اﻳﺠﺎد ﺷﻲ ي ﺑﻪ ﻧﺎم ‪ objTest‬از ﻛﻼس ‪Test‬‬‫;)(‪Test prtTest=new Test‬‬ ‫ﻣﺜﺎل : اﻳﺠﺎد ﺷﻲ ي ﺑﻪ ﻧﺎم ‪ objTest‬از ﻛﻼس ‪Test‬‬‫;)(‪Test objTest‬‬ ‫دﺳﺘﺮﺳﻲ ﺑﻪ اﻋﻀﺎي اﺷﻴﺎء‬‫ﭘﺲ از اﻳﻨﻜﻪ ﻳﻚ ﻧﻤﻮﻧﻪ از ﻳﻚ ﻛﻼس اﻳﺠﺎد ﺷﻮد اﻋﻀﺎي ﻋﻤﻮﻣﻲ )‪ (public‬آن ﻛﻼس از ﻃﺮﻳﻖ ﺷﻲ ﻗﺎﺑﻞ دﺳﺘﺮﺳﻲ ﻣﻲ‬ ‫ﺑﺎﺷﺪ. ﺑﺮاي دﺳﺘﺮﺳﻲ ﺑﻪ اﻳﻦ اﻋﻀﺎ از ﻧﺎم ﺷﻲ / ﻧﻤﻮﻧﻪ ﺑﻪ ﻫﻤﺮاه ﻳﻚ ﻧﻘﻄﻪ و ﺳﭙﺲ ﻧﺎم ﻋﻀﻮ ﻋﻤﻮﻣﻲ ﻣﻮرد ﻧﻈﺮ ﻋﻤﻞ ﻣﻲ ﻛﻨﻴﻢ‬‫ﻣﺜﺎل : ﻓﺮض ﻛﻨﻴﺪ ﻛﻼس ‪ Test‬داراي ﻳﻚ ﻋﻀﻮ ﻋﻤﻮﻣﻲ ﺑﻪ ﻧﺎم ‪ x‬ﺑﺎﺷﺪ و ﺷﻲ ‪ objTest‬از ﻧﻮع ﻛﻼس ‪Test‬‬ ‫ﺗﻌﺮﻳﻒ ﺷﺪه ﺑﺎﺷﺪ. در اﻳﻦ ﺻﻮرت ﺑﺮاي دﺳﺘﺮﺳﻲ ﺑﻪ ﻋﻀﻮ ‪ x‬ﺑﻪ ﺷﻜﻞ زﻳﺮ ﻋﻤﻞ ﻣﻲ ﻛﻨﻴﻢ‬‫‪objTest.x‬‬‫ﻣﺜﺎل : ﻓﺮض ﻛﻨﻴﺪ ﻛﻼس ‪ Test‬داراي ﻳﻚ ﻋﻀﻮ ﻋﻤﻮﻣﻲ ﺑﻪ ﻧﺎم ‪ x‬ﺑﺎﺷﺪ و ﺷﻲ ‪ ptrTest‬ي اﺷﺎره‬‫ﮔﺮي)‪ (Pointer‬از ﻧﻮع ﻛﻼس ‪ Test‬ﺗﻌﺮﻳﻒ ﺷﺪه ﺑﺎﺷﺪ. در اﻳﻦ ﺻﻮرت ﺑﺮاي دﺳﺘﺮﺳﻲ ﺑﻪ ﻋﻀﻮ ‪ x‬ﺑﻪ ﺷﻜﻞ زﻳﺮ‬ ‫ﻋﻤﻞ ﻣﻲ ﻛﻨﻴﻢ‬‫‪ptrTest->x‬‬ ‫ﺻﻔﺤﻪ 4‬
  5. 5. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫اﻋﻀﺎي ﻛﻼس‬ ‫اﻋﻀﺎي ﻛﻼس ﻣﻲ ﺗﻮاﻧﺪ ﻳﻜﻲ از ﻣﻮارد ﺛﺎﺑﺖ ﻫﺎ ، ﻓﻴﻠﺪ ﻫﺎ ، ﺧﻮاص ، ﻣﺘﺪ ﻫﺎ ، ﺳﺎزﻧﺪه ﻫﺎ ، ﻣﺨﺮب ﻫﺎ و ... ﺑﺎﺷﺪ‬ ‫ﻓﺮم ﻛﻠﻲ ﺗﻌﺮﻳﻒ اﻋﻀﺎي ﻛﻼس ﺑﻪ ﺷﻜﻞ زﻳﺮ ﻣﻲ ﺑﺎﺷﺪ‬‫[‬ ‫د‬ ‫]:‬ ‫‪classmembers‬‬ ‫ﺳﻄﺢ دﺳﺘﻴﺎﺑﻲ ﻣﻲ ﺗﻮاﻧﺪ ‪ public‬و ﻳﺎ ‪ private‬ﺑﺎﺷﺪ.‬ ‫ﺗﻌﺮﻳﻒ اﻋﻀﺎي ﺛﺎﺑﺖ‬ ‫اﻋﻀﺎي ﺛﺎﺑﺖ ﺑﺎ ﻛﻠﻴﺪ واژه ‪ const‬ﺗﻌﺮﻳﻒ ﻣﻲ ﺷﻮﻧﺪ‬ ‫ﻣﺜﺎل :‬‫‪class Test‬‬‫{‬ ‫:‪Private‬‬ ‫;01=‪static const int x‬‬ ‫:‪public‬‬ ‫;41.3=‪static const double PI‬‬‫;}‬ ‫ﻧﻜﺘﻪ : اﻣﻜﺎن ﺗﻐﻴﻴﺮ ﻣﻘﺎدﻳﺮ ﺛﺎﺑﺖ در اداﻣﻪ ﺑﺮﻧﺎﻣﻪ وﺟﻮد ﻧﺪارد.‬ ‫ﺗﻌﺮﻳﻒ ﻓﻴﻠﺪ ﻫﺎ‬‫ﻓﻴﻠﺪ ﻫﺎ ﻫﻤﺎﻧﻨﺪ ﻣﺘﻐﻴﺮ ﻫﺎي ﻣﻌﻤﻮﻟﻲ ﺗﻌﺮﻳﻒ ﻣﻲ ﺷﻮﻧﺪ و ﺑﺮاي ذﺧﻴﺮه داده ﻫﺎ ﺑﻪ ﻛﺎر ﻣﻲ روﻧﺪ. ﻧﺎم دﻳﮕﺮ ﻓﻴﻠﺪ ﻫﺎ ﻣﺘﻐﻴﺮ ﻧﻤﻮﻧﻪ اﺳﺖ.‬ ‫ﻓﺮم ﻛﻠﻲ ﺗﻌﺮﻳﻒ ﻓﻴﻠﺪ ﻫﺎ ﺑﻪ ﺷﻜﻞ زﻳﺮ ﻣﻲ ﺑﺎﺷﺪ‬‫[‬ ‫د‬ ‫]:‬ ‫ع داد‬ ‫]‪[static‬‬ ‫م‬ ‫;‬ ‫ﻣﺜﺎل :‬‫‪class Test‬‬‫{‬ ‫:‪private‬‬ ‫;‪int x‬‬ ‫;‪static int y‬‬ ‫:‪public‬‬ ‫;‪int z‬‬‫;}‬ ‫ﺗﻌﺮﻳﻒ ﻣﺘﺪ ﻫﺎ‬‫ﻣﺘﺪ ﻫﺎ ﻣﺠﻤﻮﻋﻪ اي از دﺳﺘﻮراﻟﻌﻤﻞ ﻫﺎ ﻫﺴﺘﻨﺪ ﻛﻪ ﻋﻤﻠﻴﺎﺗﻲ را ﺑﺮ روي داده ﻫﺎي ﻛﻼس اﻧﺠﺎم ﻣﻲ دﻫﻨﺪ. ﻓﺮم ﻛﻠﻲ ﺗﻌﺮﻳﻒ ﻣﺘﺪ ﺑﻪ‬ ‫ﺻﻮرت زﻳﺮ ﻣﻲ ﺑﺎﺷﺪ‬‫[‬ ‫د‬ ‫]:‬ ‫ع داد‬ ‫م‬ ‫ه [(‬ ‫را‬ ‫;)]‬ ‫ﻣﺜﺎل :‬‫‪class Test‬‬‫{‬ ‫:‪public‬‬ ‫)‪int Sum(int x,int y‬‬ ‫{‬ ‫;‪int s‬‬ ‫;‪s=x+y‬‬ ‫;‪return s‬‬ ‫}‬‫;}‬ ‫ﻣﺘﺪ ﻫﺎي ﻫﻤﻨﺎم‬ ‫دو ﻣﺘﺪ در ﺻﻮرﺗﻲ ﻣﻲ ﺗﻮاﻧﻨﺪ ﻫﻤﻨﺎم ﺑﺎﺷﻨﺪ ﻛﻪ از ﻧﻈﺮ ﺗﻌﺪاد ،ﺗﺮﺗﻴﺐ و ﻳﺎ ﻧﻮع ﭘﺎراﻣﺘﺮﻫﺎ ﺑﺎ ﻫﻢ ﻣﺘﻔﺎوت ﺑﺎﺷﻨﺪ‬ ‫ﺻﻔﺤﻪ 5‬
  6. 6. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫ﻣﺘﺪ ﻫﺎي ﺑﺎزﮔﺸﺘﻲ‬ ‫اﮔﺮ ﻳﻚ ﻣﺘﺪ ﻣﺠﺪدا ﺧﻮدش را ﻓﺮاﺧﻮاﻧﻲ ﻛﻨﺪ ﺑﻪ آن ﻣﺘﺪ ﺑﺎزﮔﺸﺘﻲ ﻣﻲ ﮔﻮﻳﻴﻢ.‬ ‫ﺗﻌﺮﻳﻒ وﻳﮋﮔﻲ ﻫﺎ / ﺧﻮاص‬‫از ﻧﻈﺮ ﻛﺪ ﺧﺎرج از ﻛﻼس وﻳﮋﮔﻲ ﻫﺎ ﻫﻤﺎﻧﻨﺪ ﻓﻴﻠﺪ ﻫﺎ ﻫﺴﺘﻨﺪ و ﻟﻲ از ﻧﻈﺮ ﻛﻼس ﻣﺘﻔﺎوت ﻣﻲ ﺑﺎﺷﻨﺪ. ﺧﺎﺻﻴﺖ ﻫﺎ دو ﻛﺎر را اﻧﺠﺎم‬ ‫ﻣﻲ دﻫﻨﺪ 1- داده ﻫﺎي ﺧﻮد را ﻣﻘﺪار ﻣﻲ دﻫﻨﺪ 2- ﻣﻘﺪار داده ﻫﺎ را ﺑﺎزﻳﺎﺑﻲ ﻣﻲ ﻛﻨﻨﺪ.‬ ‫ﺷﻜﻞ ﻛﻠﻲ)ﺳﺎده ﺷﺪه( ﺗﻌﺮﻳﻒ وﻳﮋﮔﻲ / ﺧﺎﺻﻴﺖ ﺑﻪ ﻓﺮم زﻳﺮ ﻣﻲ ﺑﺎﺷﺪ‬‫;}ﻣﺘﺪ ﻣﺪﻳﺮﻳﺖ ﺑﺨﺶ ﻧﻮﺷﺘﻦ=‪,write‬ﻣﺘﺪ ﻣﺪﻳﺮﻳﺖ ﺑﺨﺶ ﺧﻮاﻧﺪن=‪ = {read‬ﻧﺎم وﻳﮋﮔﻲ ﻧﻮع داده ﺧﺮوﺟﻲ ‪__property‬‬ ‫ﻣﻘﺪار دادن ﺑﻪ ﺧﺎﺻﻴﺖ در ﻗﺴﻤﺖ ‪ write‬و ﺑﺎزﻳﺎﺑﻲ ﻣﻘﺪار در ﻗﺴﻤﺖ ‪ read‬اﻧﺠﺎم ﻣﻲ ﺷﻮد.‬ ‫ﻣﺜﺎل :‬‫‪class Test‬‬‫{‬ ‫:‪private‬‬ ‫;‪int _y‬‬ ‫/* ﻣﺘﺪ ﻣﺪﻳﺮﻳﺖ ﺑﺨﺶ ﺧﻮاﻧﺪن*/ };‪int getData(){return _y‬‬ ‫/* ﻣﺘﺪ ﻣﺪﻳﺮﻳﺖ ﺑﺨﺶ ﻧﻮﺷﺘﻦ*/ };‪void setData(int x){ _y=x‬‬ ‫:‪public‬‬ ‫;}‪__property int y={read=getData,write=setData‬‬‫;}‬‫ﻧﻜﺘﻪ ﻗﺎﻳﻞ ﺗﻮﺟﻪ در ﻣﻮرد وﻳﮋﮔﻲ ﻫﺎ اﻳﻦ اﺳﺖ ﻛﻪ در ﻣﻮﻗﻊ ﻣﻘﺪار دﻫﻲ در ﺑﺨﺶ ‪ write‬ﻣﻲ ﺗﻮان ﻛﻨﺘﺮل ﻧﻤﻮد ﻛﻪ‬‫داده ﻫﺎي ﻏﻴﺮ ﻣﺠﺎز ﺗﻮﺳﻂ ﻛﺎرﺑﺮ وارد ﻧﺸﻮد. ﻧﻜﺘﻪ دﻳﮕﺮ اﻳﻨﻜﻪ وﻗﺘﻲ ﻛﺪ ﺧﺎرج از ﻛﻼس ﻣﻘﺪاري را ﺑﻪ ﻳﻚ ﺧﺎﺻﻴﺖ‬ ‫ﻧﺴﺒﺖ دﻫﺪ اﻳﻦ ﻣﻘﺪار ﺗﻮﺳﻂ ﭘﺎراﻣﺘﺮ ﺗﺎﺑﻊ ﻣﺮﺑﻮط ﺑﻪ ﺑﺨﺶ ‪ read‬ﻗﺎﻳﻞ دﺳﺘﺮﺳﻲ ﻣﻲ ﺑﺎﺷﺪ.‬‫ﻧﻜﺘﻪ : ﭼﻨﺎﻧﺠﻪ در ﺗﻌﺮﻳﻒ ﻳﻚ وﻳﮋﮔﻲ / ﺧﺎﺻﻴﺖ ﺑﺨﺶ ‪ read‬ﺣﺬف ﺷﻮد آن وﻳﮋﮔﻲ ﻓﻘﻂ ﻧﻮﺷﺘﻨﻲ و در ﺻﻮرﺗﻲ ﻛﻪ‬ ‫ﺑﺨﺶ ‪ write‬ﺣﺬف ﺷﻮد آن وﻳﮋﮔﻲ ﻓﻘﻂ ﺧﻮاﻧﺪﻧﻲ ﻣﻲ ﺑﺎﺷﺪ.‬ ‫ﻋﻀﻮ ﺳﺎزﻧﺪه ﻛﻼس‬‫ﻋﻀﻮ ﺳﺎزﻧﺪه ﻛﻼس ﻫﻤﻨﺎم ﺧﻮد ﻛﻼس ﻣﻲ ﺑﺎﺷﺪ و ﻣﻲ ﺗﻮاﻧﺪ داراي ﭘﺎراﻣﺘﺮ ﺑﺎﺷﺪ. ﻳﻚ ﻛﻼس ﻣﻲ ﺗﻮاﻧﺪ ﺑﻴﺶ از ﻳﻚ ﺳﺎزﻧﺪه داﺷﺘﻪ‬ ‫ﺑﺎﺷﺪ‬ ‫ﻣﺜﺎل :‬‫‪class Test‬‬‫{‬ ‫:‪private‬‬ ‫;‪int a,b‬‬ ‫:‪public‬‬ ‫};0=‪Test (){a=0;b‬‬ ‫};‪Test (int x,int y){a=x;b=y‬‬‫;}‬ ‫ﻋﻀﻮ ﻣﺨﺮب ﻛﻼس‬‫ﻋﻀﻮ ﻣﺨﺮب ﻛﻼس ﻫﻤﻨﺎم ﺧﻮد ﻛﻼس ﻣﻲ ﺑﺎﺷﺪ ﻛﻪ ﻗﺒﻞ از آن ﻋﻼﻣﺖ ~ ﻗﺮار دارد. ﻳﻚ ﻛﻼس ﻧﻤﻲ ﺗﻮاﻧﺪ ﺑﻴﺶ از ﻳﻚ ﻋﻀﻮ‬‫ﻣﺨﺮب داﺷﺘﻪ ﺑﺎﺷﺪ.ﻋﻀﻮ ﻣﺨﺮب در زﻣﺎﻧﻲ ﻛﻪ اﺳﺘﻔﺎده از ﺷﻲ اﻳﺠﺎد ﺷﺪه از ﻛﻼس ﺗﻤﺎم ﻣﻲ ﺷﻮد ﺑﻪ ﺻﻮرت ﺧﻮدﻛﺎر اﺟﺮا ﻣﻲ ﺷﻮد‬‫. ﺑﻨﺎﺑﺮاﻳﻦ ﺗﻤﺎم ﻣﻮاردي ﻛﻪ ﻗﺮار اﺳﺖ در ﭘﺎﻳﺎن ﻋﻤﺮ ﺷﻲ اﻧﺠﺎم ﺷﻮد در اﻳﻦ ﻗﺴﻤﺖ ﻗﺮار ﻣﻲ ﮔﻴﺮد. ﻣﺜﻼ ﻓﺮض ﻛﻨﻴﺪ ﻳﻚ ﻛﻼس‬‫ﺑﺮاي ﻛﺎر ﺑﺎ ﻓﺎﻳﻞ ﻫﺎ اﻳﺠﺎد ﻛﺮده ﺑﺎﺷﻴﺪ. ﻋﻀﻮ ﺳﺎزﻧﺪه در اﺑﺘﺪاي ﻛﺎر ﻣﻲ ﺗﻮاﻧﺪ ﻓﺎﻳﻞ را ﺑﺮاي ﻋﻤﻠﻴﺎت ﻣﻮرد ﻧﻈﺮ ﺑﺎز ﻛﻨﺪ و در اﻧﺘﻬﺎي‬ ‫ﻛﺎر ﻋﻀﻮ ﻣﺨﺮب ﺑﺎﻳﺪ ﻋﻤﻞ ﺑﺴﺘﻦ ﻓﺎﻳﻞ را اﻧﺠﺎم دﻫﺪ‬ ‫ﺻﻔﺤﻪ 6‬
  7. 7. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ : ‫ﻣﺜﺎل‬class Test{ Private: int a,b; public: ~Test () { … }}; ‫ﻣﺜﺎل : ﺗﻌﺮﻳﻒ ﻛﻼس ﺑﺮاي ﻛﺎر ذﺧﻴﺮه ﺳﺎزي ﻳﻚ آراﻳﻪ رﺷﺘﻪ اي 01 ﺗﺎﻳﻲ‬#include <vcl.h>#include <stdio.h>class Famille{private:AnsiString FNames[10];AnsiString GetName(int Index);void SetName(int, AnsiString);public:__property AnsiString Names[int Index] = {read=GetName,write=SetName};Famille(){}~Famille(){}};AnsiString Famille::GetName(int i){return FNames[i];}void Famille::SetName(int i,const AnsiString s){FNames[i]=s;}int main(){Famille C;C.Names[0]="Steve"; //calls Famille::SetName()C.Names[1]="Amy";C.Names[2]="Sarah";C.Names[3]="Andrew";for (int i = 0; i <= 3; i++){//calls Famille::GetName()puts(C.Names[i].c_str());}} 7 ‫ﺻﻔﺤﻪ‬
  8. 8. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫ﭘﺮﺳﺶ ﻫﺎي ﭘﺎﻳﺎن درس‬ ‫2- ﺑﻪ ﻳﻚ ﻣﺘﻐﻴﺮ ‪ Public‬ﻛﻪ در ﻳﻚ ﻛﻼس ﺗﻌﺮﻳﻒ ﺷﺪه ﺑﺎﺷﺪ................ ﻣﻲ ﮔﻮﻳﻨﺪ‬ ‫3- از ﻧﻈﺮ ........... ﺑﻴﻦ ﻓﻴﻠﺪ و وﻳﮋﮔﻲ ﺗﻔﺎوﺗﻲ وﺟﻮد ﻧﺪارد‬ ‫د (بوج‬ ‫ج ( ﻛﻼس وارث‬ ‫اﻟﻒ( ﻃﺮاح ﻛﻼس ب (اﺳﺘﻔﺎده ﻛﻨﻨﺪه از ﻛﻼس‬ ‫4- اﻳﺠﺎد ﻧﻤﻮﻧﻪ اي از ﻛﻼس را ............ و ﻧﻤﻮﻧﻪ اﻳﺠﺎد ﺷﺪه از ﻛﻼس را ........ ﻣﻲ ﮔﻮﻳﻨﺪ.‬ ‫د ( ﻧﻤﻮﻧﻪ ﺳﺎزي ،‬ ‫ج ( ﻧﻤﻮﻧﻪ ﺳﺎزي ، ﺷﻲ‬ ‫ب (اﻣﺘﻐﻴﺮ ﻧﻤﻮﻧﻪ ، ﻧﻤﻮﻧﻪ ﺳﺎزي‬ ‫اﻟﻒ( ﺗﻌﺮﻳﻒ ﺷﻲ ، ﻛﻼس‬ ‫ﻛﻨﺘﺮل‬ ‫5-....... ﺑﻪ ﻳﻚ ﻣﺘﻐﻴﺮ ‪ Public‬اﻃﻼق ﻣﻲ ﺷﻮد ﻛﻪ در ﻳﻚ ﻛﻼس ﺗﻌﺮﻳﻒ ﺷﺪه ﺑﺎﺷﺪ‬ ‫د(اﻳﺴﺘﺎ‬ ‫ج( ﻣﺘﺪ‬ ‫ب( وﻳﮋﮔﻲ‬ ‫اﻟﻒ( ﻓﻴﻠﺪ‬ ‫6 - ﻛﺪام ﮔﺰﻳﻨﻪ ﺻﺤﻴﺢ ﻧﻤﻲ ﺑﺎﺷﺪ‬ ‫اﻟﻒ( ﻋﻀﻮ ﺳﺎزﻧﺪه در ﻳﻚ ﻛﻼس وﻇﻴﻔﻪ ﻣﻘﺪار دﻫﻲ اوﻟﻴﻪ ﻣﻘﺎدﻳﺮ ﻣﺮﺑﻮط ﺑﻪ داده ﻫﺎي ﻛﻼس را ﺑﺮ ﻋﻬﺪه دارد‬ ‫ب ( اﻋﻀﺎي ﺛﺎﺑﺖ ﺑﺎ ﻛﻠﻴﺪ واژه ‪ Const‬ﺗﻌﺮﻳﻒ ﻣﻲ ﺷﻮﻧﺪ‬ ‫ج ( از ﻧﻈﺮ ﻛﺪ ﺧﺎرج از ﻛﻼس وﻳﮋﮔﻲ ﻫﺎ ﻫﻤﺎﻧﻨﺪ ﻓﻴﻠﺪ ﻫﺎ ﻫﺴﺘﻨﺪ‬ ‫د ( اﮔﺮ ﻳﻚ ﻣﺘﺪ ﻣﺠﺪدا ﺧﻮدش را ﻓﺮاﺧﻮاﻧﻲ ﻛﻨﺪ ﺑﻪ آن ﻣﺘﺪ ﻫﻤﻨﺎم ﻣﻲ ﮔﻮﻳﻴﻢ‬ ‫ﺗﻤﺮﻳﻦ 1 :‬‫ﻳﻚ ﻛﻼس ﺑﻨﻮﻳﺴﻴﺪ ﻛﻪ ﺑﺘﻮاﻧﺪ ﻋﻤﻞ ﺟﻤﻊ ، ﺿﺮب ، ﺗﻔﺮﻳﻖ ، ﺗﻘﺴﻴﻢ و ﺑﺎﻗﻴﻤﺎﻧﺪه ﺻﺤﻴﺢ را ﺑﺮ روي دو ﻋﺪد اﻧﺠﺎم دﻫﺪ.‬‫ﻛﻼس داراي دو ﺧﺎﺻﻴﺖ ﺑﻪ ﻧﺎم ﻫﺎي ‪ x,y‬ﺑﺮاي ﻣﺸﺨﺺ ﻧﻤﻮدن دو ﻋﺪد و 5 ﻣﺘﺪ ﺑﻪ ﻧﺎم ﻫﺎي ‪ add‬و ‪ sub‬و ‪ mul‬و‬ ‫‪ div‬و ‪ mod‬ﺑﺮاي اﻋﻤﺎل ﺟﻤﻊ ، ﺿﺮب ، ﺗﻔﺮﻳﻖ ، ﺗﻘﺴﻴﻢ و ﺑﺎﻗﻴﻤﺎﻧﺪه ﺻﺤﻴﺢ ﺑﺎﺷﺪ.‬ ‫ﺗﻤﺮﻳﻦ 2:‬‫ﻳﻚ ﻛﻼس ﺑﺮاي اﻧﺠﺎم ﻋﻤﻠﻴﺎت ﺟﻤﻊ ، ﺗﻔﺮﻳﻖ و ﺿﺮب دو ﻋﺪد ‪ n‬رﻗﻤﻲ و ﻣﺤﺎﺳﺒﻪ ﻓﺎﻛﺘﻮرﻳﻞ ﻳﻚ ﻋﺪد ‪ n‬رﻗﻤﻲ ﺑﺮ‬ ‫ﭘﺎﻳﻪ ﻛﺎر روي رﺷﺘﻪ ﻫﺎي ﻋﺪدي اﻳﺠﺎد ﻛﻨﻴﺪ‬ ‫ﺻﻔﺤﻪ 8‬
  9. 9. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ : ‫ﻣﺜﺎل‬class stars { int magnitude; // Data member int starfunc(void); // Member function}; ‫ﻣﺜﺎل : ﺗﻌﺮﻳﻒ ﻛﻼس ﺑﺮاي ذﺧﻴﺮه ﺳﺎزي ﻳﻚ آراﻳﻪ رﺷﺘﻪ اي 01 ﺗﺎﻳﻲ‬#include <vcl.h>#include <stdio.h>class Famille{private:AnsiString FNames[10];AnsiString GetName(int Index);void SetName(int, AnsiString);public:__property AnsiString Names[int Index] = {read=GetName,write=SetName};Famille(){}~Famille(){}};AnsiString Famille::GetName(int i){return FNames[i];}void Famille::SetName(int i,const AnsiString s){FNames[i]=s;}int main(){Famille C;C.Names[0]="Steve"; //calls Famille::SetName()C.Names[1]="Amy";C.Names[2]="Sarah";C.Names[3]="Andrew";for (int i = 0; i <= 3; i++){//calls Famille::GetName()puts(C.Names[i].c_str());}}An object of a class with only public members and no constructors or baseclasses (typically a structure) can be initialized with an initializerlist. If a class has a constructor, its objects must be either initializedor have a default constructor. The latter is used for objects notexplicitly initialized. 9 ‫ﺻﻔﺤﻪ‬
  10. 10. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬Objects of classes with constructors can be initialized with an expressionlist in parentheses. This list is used as an argument list to theconstructor. An alternative is to use an equal sign followed by a singlevalue. The single value can be the same type as the first argument acceptedby a constructor of that class, in which case either there are noadditional arguments, or the remaining arguments have default values. Itcould also be an object of that class type. In the former case, thematching constructor is called to create the object. In the latter case,the copy constructor is called to initialize the object. ‫ ﺑﺮاي وﺿﻮح و ﺧﻮاﻧﺎﻳﻲ ﻣﺜﺎل ﺣﺬف ﺷﺪه اﺳﺖ‬x ‫در ﻣﺜﺎل زﻳﺮ ﺑﺪﻧﻪ ﺗﺎﺑﻊ‬class X{ int i;public: X(); X(int x); X(const X&);};void main(){ X one; // default constructor invoked X two(1); // constructor X::X(int) is used X three = 1; // calls X::X(int) X four = one; // invokes X::X(const X&) for copy X five(two); // calls X::X(const X&)} ‫ﺳﺎزﻧﺪه ﻛﻼس ﻣﻲ ﺗﻮاﻧﺪ ﻣﻘﺎدﻳﺮ اﻋﻀﺎي ﻛﻼس را ﺑﻪ دو ﻃﺮﻳﻖ ﺑﻪ آﻧﻬﺎ ﻧﺴﺒﺖ دﻫﺪ‬ ‫ﺣﺎﻟﺖ اول : ﻣﻘﺎدﻳﺮ را ﺑﻪ ﺻﻮرت ﭘﺎراﻣﺘﺮ درﻳﺎﻓﺖ و ﻋﻤﻞ ﻣﻘﺪار دﻫﻲ را در ﺑﺪﻧﻪ ﺗﺎﺑﻊ ﺳﺎزﻧﺪه اﻧﺠﺎم دﻫﺪ‬class X{ int a, b;public: X(int i, int j) { a = i; b = j }}; ‫ﺣﺎﻟﺖ دوم : ﺑﺎ اﺳﺘﻔﺎده از ﻟﻴﺴﺘﻲ از ﻣﻘﺎدﻳﺮ ﻛﻪ ﻗﺒﻞ از ﺑﺪﻧﻪ ﺗﺎﺑﻊ ﻗﺮار ﻣﻲ ﮔﻴﺮد‬class X{//‫ ﻳﻚ ﻣﺘﻐﻴﺮ ﻣﺮﺟﻊ ﻣﻲ ﺑﺎﺷﺪ‬c ‫ﻧﻜﺘﻪ : ﻣﺘﻐﻴﺮ‬int a, b, &c;public: X(int i, int j) : a(i), b(j), c(a) {}};The initializer list is the only place to initialize a reference variable.In both cases, an initialization of X x(1, 2) assigns a value of 1 to x::aand 2 to x::b. The second method, the initializer list, provides amechanism for passing values along to base class constructors. 10 ‫ﺻﻔﺤﻪ‬
  11. 11. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬Base class constructors must be declared as either public or Note:protected to be called from a derived class.class base1{ int x;public: base1(int i) { x = i; }};class base2{ int x;public: base2(int i) : x(i) {}};class top : public base1, public base2{ int a, b;public: top(int i, int j) : base1(i*5), base2(j+i), a(i) { b = j;}};With this class hierarchy, a declaration of top one(1, 2) would result inthe initialization of base1 with the value 5 and base2 with the value 3.The methods of initialization can be intermixed.As described previously, the base classes are initialized in declarationorder. Then the members are initialized, also in declaration order,independent of the initialization list.class X{ int a, b;public: X(int i, j) : a(i), b(a+j) {}};With this class, a declaration of X x(1,1) results in an assignment of 1 tox::a and 2 to x::b.Base class constructors are called prior to the construction of any of thederived classes members. If the values of the derived class are changed,they will have no effect on the creation of the base class.class base{ int x;public: base(int i) : x(i) {}};class derived : base{ int a;public: derived(int i) : a(i*10), base(a) { } // Watch out! Base will be 11 ‫ﺻﻔﺤﻪ‬
  12. 12. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ // passed an uninitialized ’a’};With this class setup, a call of derived d(1) will not result in a value of10 for the base class member x. The value passed to the base classconstructor will be undefined.When you want an initializer list in a non-inline constructor, don’t placethe list in the class definition. Instead, put it at the point at which thefunction is defined.derived::derived(int i) : a(i){ . . .}/* Compile with bcc32 famille.cpp vcl.lib ole2w32.lib */#include <vcl.h>#include <stdio.h>class Famille{private:AnsiString FNames[10];AnsiString GetName(int Index);void SetName(int, AnsiString);public:__property AnsiString Names[int Index] = {read=GetName,write=SetName};Famille(){}~Famille(){}};AnsiString Famille::GetName(int i){return FNames[i];}void Famille::SetName(int i,const AnsiString s){FNames[i]=s;}int main(){Famille C;C.Names[0]="Steve"; //calls Famille::SetName()C.Names[1]="Amy";C.Names[2]="Sarah";C.Names[3]="Andrew";for (int i = 0; i <= 3; i++){//calls Famille::GetName()puts(C.Names[i].c_str()); 12 ‫ﺻﻔﺤﻪ‬
  13. 13. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬}} (‫ﻣﺸﺘﻖ ﮔﺮﻓﺘﻦ از ﻛﻼس )ﺗﻌﺮﻳﻒ ﻳﻚ ﻛﻼس ﺑﺮ روي ﻛﻼس دﻳﮕﺮي‬Base and derived class accessWhen you declare a derived class D, you list the base classes B1, B2, ...in a comma-delimited base-list:class-key D : base-list { <member-list> }D inherits all the members of these base classes. (Redefined base classmembers are inherited and can be accessed using scope overrides, ifneeded.) D can use only the public and protected members of its baseclasses. But, what will be the access attributes of the inherited membersas viewed by D? D might want to use a public member from a base class, butmake it private as far as outside functions are concerned. The solution isto use access specifiers in the base-list.Since a base class can itself be a derived class, the access Note:attribute question is recursive: you backtrack until you reach the basemostof the base classes, those that do not inherit.When declaring D, you can use the access specifier public, protected, orprivate in front of the classes in the base-list:class D : public B1, private B2, ... { . . .}These modifiers do not alter the access attributes of base members asviewed by the base class, though they can alter the access attributes ofbase members as viewed by the derived class.The default is private if D is a class declaration, and public if D is astruct declaration.Unions cannot have base classes, and unions cannot be used as base Note:classes.The derived class inherits access attributes from a base class as follows:public base class: public members of the base class are publicmembers of the derived class. protected members of the base class areprotected members of the derived class. private members of the base classremain private to the base class.protected base class: Both public and protected members of the baseclass are protected members of the derived class. private members of thebase class remain private to the base class.private base class: Both public and protected members of the baseclass are private members of the derived class. private members of the baseclass remain private to the base class.Note that private members of a base class are always inaccessible to memberfunctions of the derived class unless friend declarations are explicitlydeclared in the base class granting access. For example, 13 ‫ﺻﻔﺤﻪ‬
  14. 14. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬/* class X is derived from class A */class X : A { // default for class is private A . . .}/* class Y is derived (multiple inheritance) from B and C B defaults to private B */class Y : B, public C { // override default for C . . .}/* struct S is derived from D */struct S : D { // default for struct is public D . . .}/* struct T is derived (multiple inheritance) from D and E E defaults to public E */struct T : private D, E { // override default for D // E is public by default . . .}The effect of access specifiers in the base list can be adjusted by using aqualified-name in the public or protected declarations of the derivedclass. For example:class B { int a; // private by defaultpublic: int b, c; int Bfunc(void);};class X : private B { // a, b, c, Bfunc are now private in X int d; // private by default, NOTE: a is not // accessible in Xpublic: B::c; // c was private, now is public int e; int Xfunc(void);};int Efunc(X& x); // external to B and XThe function Efunc() can use only the public names c, e, and Xfunc().The function Xfunc() is in X, which is derived from private B, so it hasaccess toThe “adjusted-to-public” cThe “private-to-X” members from B: b and Bfunc()X’s own private and public members: d, e, and Xfunc() 14 ‫ﺻﻔﺤﻪ‬
  15. 15. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬However, Xfunc() cannot access the “private-to-B” member, a. 15 ‫ﺻﻔﺤﻪ‬
  16. 16. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬Friends of classesA friend F of a class X is a function or class, although not a memberfunction of X, with full access rights to the private and protected membersof X. In all other respects, F is a normal function with respect to scope,declarations, and definitions.Since F is not a member of X, it is not in the scope of X, and it cannot becalled with the x.F and xptr->F selector operators (where x is an X objectand xptr is a pointer to an X object).If the specifier friend is used with a function declaration or definitionwithin the class X, it becomes a friend of X.friend functions defined within a class obey the same inline rules asmember functions (see Inline functions). friend functions are not affectedby their position within the class or by any access specifiers. Forexample:class X { int i; // private to X friend void friend_func(X*, int);/* friend_func is not private, even though its declared in the private section */public: void member_func(int);};/* definitions; note both functions access private int i */void friend_func(X* xptr, int a) { xptr–>i = a; }void X::member_func(int a) { i = a; }X xobj;/* note difference in function calls */friend_func(&xobj, 6);xobj.member_func(6);You can make all the functions of class Y into friends of class X with asingle declaration:class Y; // incomplete declarationclass X { friend Y; int i; void member_funcX();};class Y; { // complete the declaration void friend_X1(X&); void friend_X2(X*);...};The functions declared in Y are friends of X, although they have no friendspecifiers. They can access the private members of X, such as i andmember_funcX. 16 ‫ﺻﻔﺤﻪ‬
  17. 17. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬It is also possible for an individual member function of class X to be afriend of class Y:class X {... void member_funcX();}class Y { int i; friend void X::member_funcX();...};Class friendship is not transitive: X friend of Y and Y friend of Z doesnot imply X friend of Z. Friendship is not inherited. 17 ‫ﺻﻔﺤﻪ‬
  18. 18. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫اﻳﺠﺎد ﻛﻼس ، اﺳﺘﻔﺎده از ﻳﻚ ﻛﻼس در ﻛﻼس دﻳﮕﺮ ، وراﺛﺖ از ﻳﻚ ﻛﻼس‬ : ‫اﻳﺠﺎد ﻛﻼس‬‫ ﺑﺮاي ﺑﺮﮔﺮداﻧﺪن‬SetDate ‫ و‬GetDate ‫ ﺑﺎ ﭼﻨﺪ ﺳﺎزﻧﺪه و دو ﻣﺘﺪ ﺑﻪ ﻧﺎم ﻫﺎي‬myDate ‫اﻳﺠﺎد ﻳﻚ ﻛﻼس ﺳﺎده ﺑﻪ ﻧﺎم‬ ‫و ﻣﻘﺪاردﻫﻲ ﻣﺘﻐﻴﺮﻫﺎي داﺧﻠﻲ ﻛﻼس‬class myDate{ private: int M, D, Y; public: myDate() { M = 0; D = 0; Y = 0; } myDate(int year) { M = 0; D = 0; Y = year; } myDate(int year, int month) { M = month; D = 0; Y = year; } myDate(int year, int month,int day) { M = month; D = day; Y = year; } String GetDate() { return String(Y) + "-" + String(M) + "-" + String(D); } String SetDate(int year, int month,int day) { M = month; D = day; Y = year; return GetDate(); }}; Person ‫ در ﻛﻼس‬myDate ‫اﺳﺘﻔﺎده از ﻛﻼس‬ • Person ‫ ﺑﺮاي ﺑﺮﮔﺮداﻧﺪن ﻣﻘﺎدﻳﺮ ﻓﻴﻠﺪ ﻫﺎي ﻛﻼس‬GetPersonInfo () ‫ﻧﻮﺷﺘﻦ ﻣﺘﺪ‬ •//---------------------------------------------------------------------------class Person{ private: String fname, lname; String getName(){return lname;} void setName(String value){lname=value;} String getFamily(){return fname;} void setFamily (String value){fname=value;} public: myDate birthDate; Person(){ Name =""; Family = ""; birthDate = myDate(); } Person(String name,String family,myDate BirthDate){ Name=name; Family = family; birthDate = BirthDate; } __property String Name={read=getName,write=setName}; __property String Family={read=getFamily,write=setFamily}; String GetPersonInfo(){ return Name+" ,"+Family+","+birthDate.GetDate(); }};//--------------------------------------------------------------------- 18 ‫ﺻﻔﺤﻪ‬
  19. 19. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫وراﺛﺖ‬ • Person ‫ ﺑﺮ روي ﻛﻼس ﭘﺎﻳﻪ‬Employee ‫اﻳﺠﺎد ﻛﻼس‬ • ‫ ﺗﻤﺎﻣﻲ وﻳﮋﮔﻲ ﻫﺎي ﻋﻤﻮﻣﻲ ﻛﻼس ﭘﺎﻳﻪ را ﺑﻪ ارث ﻣﻲ ﺑﺮد‬Employee ‫ﻛﻼس‬ • Employee ‫ ﺑﺮاي ﺑﺮﮔﺮداﻧﺪن ﻣﻘﺎدﻳﺮ ﻓﻴﻠﺪ ﻫﺎي ﻛﻼس‬GetEmployeeInfo () ‫اﻳﺠﺎد ﻣﺘﺪ‬ •//---------------------------------------------------------------------class Employee : public Person{ public: Employee() {} myDate Start; String GetEmployeeInfo() { return Person::GetPersonInfo()+","+Start.GetDate(); }};//--------------------------------------------------------------------- Employee ‫ﻧﺤﻮه اﺳﺘﻔﺎده از ﻛﻼس‬ Employee emp; emp.Name = "ali"; emp.Family = "ahmadi"; emp.birthDate =myDate(1360, 01, 01); emp.Start = myDate(1388, 01, 01); ShowMessage(emp.GetEmployeeInfo()); ‫ق‬ ‫ل‬ ‫و‬ ‫ﺗﻌﺮﻳﻒ و روﻧﻮﻳﺴﻲ ﻣﺘﺪ ﻫﺎي ﻣﺠﺎزي‬ ‫ ﺑﺮاي ﺑﺮﮔﺮداﻧﺪن ﻣﺴﺎﺣﺖ‬Area() ‫ ﺑﺎ ﻳﻚ ﻣﺘﺪ ﻣﺠﺎزي ﺑﻪ ﻧﺎم‬Shape ‫ﺗﻌﺮﻳﻒ ﻛﻼس‬//---------------------------------------------------------------------class Shape{ public: virtual double Area() { return 0; } //virtual method};//--------------------------------------------------------------------- ‫ ﺑﺎ روﻧﻮﻳﺴﻲ ﻣﺘﺪ ﻣﺠﺎزي ﻣﺤﻴﻂ ﺑﺮاي ﻣﺤﺎﺳﺒﻪ ﻣﺤﻴﻂ داﻳﺮه‬Circle ‫ﺗﻌﺮﻳﻒ ﻛﻼس‬class Circle : Shape{ private: double r; double get_r(void){return r;} void set_r(double x){r=x;} public: 19 ‫ﺻﻔﺤﻪ‬
  20. 20. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫;}‪__property double Radius={read=get_r,write=set_r‬‬ ‫‪double Area() { return r * r * 3.14159; }//override virtual method‬‬‫;}‬‫------------------------------------------------------------------//‬ ‫ﻧﺤﻮه اﺳﺘﻔﺎده از ﻛﻼس ‪Circle‬‬‫;‪Circle c‬‬‫;1=‪c.Radius‬‬‫;)(‪double A= c.Area‬‬‫;))‪ShowMessage(String(A‬‬ ‫ﻣﺜﺎل :‬ ‫ﻛﻼس ﻫﺎي ﺗﻌﺮﻳﻒ ﺷﺪه ﻗﺒﻠﻲ را ﻃﻮري ﺑﺎز ﻧﻮﻳﺴﻲ ﻛﻨﻴﺪ ﻛﻪ ﻫﺮ ﻛﻼس داراي ﻳﻚ ﻣﺘﺪ ﻣﺠﺎزي ﺑﻪ ﻧﺎم‬ ‫)(‪ ToString‬ﺑﺎﺷﺪ و اﻋﻤﺎل ﺧﻮاﺳﺘﻪ ﺷﺪه در زﻳﺮ را اﻧﺠﺎم دﻫﺪ‬ ‫ﻣﺘﺪ )(‪ ToString‬در ﻛﻼس ‪ myDate‬ﺑﺮاي ﺑﺎزﮔﺮداﻧﺪن ﺗﺎرﻳﺦ ﺑﻪ ﻓﺮﻣﺖ ‪yy-mm-dd‬‬ ‫•‬ ‫ﻣﺘﺪ )(‪ ToString‬در ﻛﻼس ‪ Person‬ﺑﺮاي ﺑﺎزﮔﺮداﻧﺪن ﻧﺎم ، ﻧﺎم ﺧﺎﻧﻮادﮔﻲ و ﺗﺎرﻳﺦ ﺗﻮﻟﺪ ﺷﺨﺺ ﺑﺎ‬ ‫•‬ ‫ﺟﺪاﻛﻨﻨﺪه ﻛﺎﻣﺎ‬ ‫ﻣﺘﺪ )(‪ ToString‬در ﻛﻼس ‪ Employee‬ﺑﺮاي ﺑﺎزﮔﺮداﻧﺪن ﻧﺎم ، ﻧﺎم ﺧﺎﻧﻮادﮔﻲ و ﺗﺎرﻳﺦ ﺗﻮﻟﺪ و ﺗﺎرﻳﺦ‬ ‫•‬ ‫اﺳﺘﺨﺪام ﻛﺎرﻣﻨﺪ ﺑﺎ ﺟﺪاﻛﻨﻨﺪه ﻛﺎﻣﺎ‬ ‫اﻳﺠﺎد ﻳﻚ ﻛﻼس ﺳﺎده ﺑﻪ ﻧﺎم ‪ Date‬ﺑﺎ ﭼﻨﺪ ﺳﺎزﻧﺪه و دو ﻣﺘﺪ ﺑﻪ ﻧﺎم ﻫﺎي ‪ GetDate‬و ‪ SetDate‬ﺑﺮاي ﺑﺮﮔﺮداﻧﺪن و‬ ‫ﻣﻘﺪاردﻫﻲ ﻣﺘﻐﻴﺮﻫﺎي داﺧﻠﻲ ﻛﻼس‬‫اﻳﺠﺎد ﻳﻚ ﻛﻼس ﺳﺎده ﺑﻪ ﻧﺎم ‪ myDate‬ﺑﺎ ﭼﻨﺪ ﺳﺎزﻧﺪه و دو ﻣﺘﺪ ﺑﻪ ﻧﺎم ﻫﺎي ‪ GetDate‬و ‪ SetDate‬ﺑﺮاي ﺑﺮﮔﺮداﻧﺪن‬ ‫و ﻣﻘﺪاردﻫﻲ ﻣﺘﻐﻴﺮﻫﺎي داﺧﻠﻲ ﻛﻼس‬‫‪class myDate‬‬‫{‬ ‫:‪private‬‬ ‫;‪int M, D, Y‬‬ ‫:‪public‬‬ ‫} ;0 = ‪myDate() { M = 0; D = 0; Y‬‬ ‫} ;‪myDate(int year) { M = 0; D = 0; Y = year‬‬ ‫} ;‪myDate(int year, int month) { M = month; D = 0; Y = year‬‬ ‫)‪myDate(int year, int month,int day‬‬ ‫} ;‪{ M = month; D = day; Y = year‬‬ ‫)(‪String GetDate‬‬ ‫{‬ ‫;)‪return String(Y) + "-" + String(M) + "-" + String(D‬‬ ‫}‬ ‫)‪String SetDate(int year, int month,int day‬‬ ‫{‬ ‫;‪M = month; D = day; Y = year‬‬ ‫;)(‪return GetDate‬‬ ‫}‬ ‫};)(‪virtual String ToString(){return GetDate‬‬‫;}‬ ‫اﺳﺘﻔﺎده از ﻛﻼس ‪ myDate‬در ﻛﻼس ‪Person‬‬ ‫•‬ ‫ﻧﻮﺷﺘﻦ ﻣﺘﺪ )( ‪ GetPersonInfo‬ﺑﺮاي ﺑﺮﮔﺮداﻧﺪن ﻣﻘﺎدﻳﺮ ﻓﻴﻠﺪ ﻫﺎي ﻛﻼس ‪Person‬‬ ‫•‬ ‫اﻳﺠﺎد ﻣﺘﺪ ﻣﺠﺎزي )(‪ ToString‬ﺑﺮاي ﺑﺮﮔﺮداﻧﺪن ﻣﻘﺎدﻳﺮ ﻓﻴﻠﺪ ﻫﺎي ﻛﻼس ‪Person‬‬ ‫•‬‫---------------------------------------------------------------------------//‬ ‫ﺻﻔﺤﻪ 02‬
  21. 21. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬class Person{ private: String fname, lname; String getName(){return lname;} void setName(String value){lname=value;} String getFamily(){return fname;} void setFamily (String value){fname=value;} public: myDate birthDate; Person(){ Name =""; Family = ""; birthDate = myDate(); } Person(String name,String family,myDate BirthDate){ Name=name; Family = family; birthDate = BirthDate; } __property String Name={read=getName,write=setName}; __property String Family={read=getFamily,write=setFamily}; String GetPersonInfo(){ return Name+" ,"+Family+","+birthDate.GetDate(); } virtual String ToString(){ return Name+" ,"+Family+","+ birthDate.ToString(); }};//--------------------------------------------------------------------- ‫وراﺛﺖ‬ • Person ‫ ﺑﺮ روي ﻛﻼس ﭘﺎﻳﻪ‬Employee ‫اﻳﺠﺎد ﻛﻼس‬ • ‫ ﺗﻤﺎﻣﻲ وﻳﮋﮔﻲ ﻫﺎي ﻋﻤﻮﻣﻲ ﻛﻼس ﭘﺎﻳﻪ را ﺑﻪ ارث ﻣﻲ ﺑﺮد‬Employee ‫ﻛﻼس‬ • Person ‫ ﺑﺮاي ﺑﺮﮔﺮداﻧﺪن ﻣﻘﺎدﻳﺮ ﻓﻴﻠﺪ ﻫﺎي ﻛﻼس‬GetEmployeeInfo () ‫اﻳﺠﺎد ﻣﺘﺪ‬ • Employee ‫ ﺑﺮاي ﺑﺮﮔﺮداﻧﺪن ﻣﻘﺎدﻳﺮ ﻓﻴﻠﺪ ﻫﺎي ﻛﻼس‬ToString() ‫روﻧﻮﻳﺴﻲ ﻣﺘﺪ ﻣﺠﺎزي‬ •//---------------------------------------------------------------------class Employee : public Person{ public: Employee() {} myDate Start; String GetEmployeeInfo() { return Person::GetPersonInfo()+","+Start.GetDate(); } virtual String ToString(){ return Person::ToString()+","+Start. ToString(); }};//--------------------------------------------------------------------- Employee ‫ﻧﺤﻮه اﺳﺘﻔﺎده از ﻛﻼس‬ Employee emp; emp.Name = "ali"; 21 ‫ﺻﻔﺤﻪ‬
  22. 22. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫;"‪emp.Family = "ahmadi‬‬ ‫;)10 ,10 ,0631(‪emp.birthDate =myDate‬‬ ‫;)10 ,10 ,8831(‪emp.Start = myDate‬‬ ‫;))(‪ShowMessage(emp.ToString‬‬ ‫ق‬ ‫ل‬ ‫و‬ ‫ﺗﻤﺮﻳﻦ:‬ ‫ﻣﺘﺪي ﺑﻪ ﻧﺎم ‪ DiffDate‬ﺑﻪ ﻛﻼس ‪ myDate‬اﺿﺎﻓﻪ ﻛﻨﻴﺪ ﻛﻪ ﺗﻔﺎﺿﻞ دو ﺗﺎرﻳﺦ ﺷﻤﺴﻲ را از ﻫﻢ ﻣﺤﺎﺳﺒﻪ ﻛﻨﺪ و ﺣﺎﺻﻞ را ﺑﻪ‬‫ﺻﻮرت روز و ﻣﺎه و ﺳﺎل ﺑﺮﮔﺮداﻧﺪ. )ﻧﻜﺘﻪ : اﻳﻦ ﻣﺘﺪ ﺑﺎﻳﺪ در ﺑﺨﺶ ‪ public‬ﻛﻼس ‪ myDate‬اﺿﺎﻓﻪ ﺷﻮد ﺗﺎ ﻗﺎﺑﻞ دﺳﺘﺮﺳﻲ ﺑﺎﺷﺪ(‬ ‫ﺟﻮاب( ﺑﺎ ﻓﺮض اﻳﻨﻜﻪ ﺗﺎرﻳﺦ 2‪ d‬ﺑﺰرﮔﺘﺮ از ﺗﺎرﻳﺦ 1‪ d‬ﺑﺎﺷﺪ ﻣﻲ ﺗﻮان ﺟﻮاب را ﺑﻪ ﺷﻜﻞ زﻳﺮ ﻧﻮﺷﺖ‬ ‫---------------------------------------------------------------------//‬ ‫)1‪myDate DiffDate(myDate d2, myDate d‬‬ ‫{‬ ‫)‪if (d1.D > d2.D‬‬ ‫{‬ ‫;03 : 13 ? )7 =< ‪d2.D += (d2.M‬‬ ‫;--‪d2.M‬‬ ‫)0 == ‪if (d2.M‬‬ ‫{‬ ‫;--‪d2.Y‬‬ ‫;21 = ‪d2.M‬‬ ‫}‬ ‫}‬ ‫)‪if (d1.M > d2.M‬‬ ‫{‬ ‫;--‪d2.Y‬‬ ‫;21 =+ ‪d2.M‬‬ ‫}‬ ‫;)‪return myDate(d2.Y - d1.Y, d2.M - d1.M, d2.D - d1.D‬‬ ‫}‬ ‫---------------------------------------------------------------------//‬ ‫راه ﺣﻞ دوم اﺳﺘﻔﺎده از ﻋﻤﻠﮕﺮ ؟ ﺑﻪ ﺟﺎي دﺳﺘﻮر ‪if‬‬ ‫---------------------------------------------------------------------//‬ ‫)1‪myDate DiffDate(myDate d2, myDate d‬‬ ‫{‬ ‫)‪if (d1.D > d2.D‬‬ ‫{‬ ‫;03 : 13 ? )7 =< ‪d2.D += (d2.M‬‬ ‫;--‪d2.M‬‬ ‫;)0 : 1 ? )0 == ‪d2.Y = d2.Y - ((d2.M‬‬ ‫;‪d2.M = (d2.M == 0) ? 12 : d2.M‬‬ ‫}‬ ‫)‪if (d1.M > d2.M‬‬ ‫{‬ ‫;--‪d2.Y‬‬ ‫;21 =+ ‪d2.M‬‬ ‫}‬ ‫;)‪return myDate(d2.Y - d1.Y, d2.M - d1.M, d2.D - d1.D‬‬ ‫ﺻﻔﺤﻪ 22‬
  23. 23. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ } //--------------------------------------------------------------------- ‫ ﺑﻪ ﺻﻮرت زﻳﺮ ﺧﻮاﻫﺪ ﺑﻮد‬myDate ‫در ﭘﺎﻳﺎن ﻛﺎر ﺷﻜﻞ ﻛﺎﻣﻞ ﻛﻼس‬//---------------------------------------------------------------------class myDate{ private: int M, D, Y; public: myDate() { M = 0; D = 0; Y = 0; } //-------------------------------------------------------- myDate(int year) { M = 0; D = 0; Y = year; } //-------------------------------------------------------- myDate(int year, int month) { M = month; D = 0; Y = year; } //-------------------------------------------------------- myDate(int year, int month,int day) { M = month; D = day; Y = year; } //-------------------------------------------------------- String GetDate() { return String(Y) + "-" + String(M) + "-" + String(D); } //-------------------------------------------------------- String SetDate(int year, int month,int day) { M = month; D = day; Y = year; return GetDate(); } //-------------------------------------------------------- myDate DiffDate(myDate d2, myDate d1) { if (d1.D > d2.D) { d2.D += (d2.M <= 7) ? 31 : 30; d2.M--; if (d2.M == 0) { d2.Y--; d2.M = 12; } } if (d1.M > d2.M) { d2.Y--; d2.M += 12; } return myDate(d2.Y - d1.Y, d2.M - d1.M, d2.D - d1.D); } //-------------------------------------------------------- virtual String ToString(){return GetDate();}};//-------------------------------------------------------- DiffDate ‫ و ﺗﻔﺎﺿﻞ دو ﺗﺎرﻳﺦ ﺑﻪ ﻛﻤﻚ ﻣﺘﺪ‬myDate ‫ﻧﺤﻮه اﺳﺘﻔﺎده از ﻛﻼس‬ myDate d,d1=myDate(88,01,01),d2=myDate(88,04,01); d=d.DiffDate(d2,d1); ShowMessage(d.ToString()); 23 ‫ﺻﻔﺤﻪ‬
  24. 24. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫ﺗﻤﺮﻳﻦ :‬‫:ﻳﻚ ﻣﺘﺪ دﻳﮕﺮ ﺑﻪ ﻧﺎم ‪ DiffDate‬ﺑﻪ ﻛﻼس ‪ myDate‬اﺿﺎﻓﻪ ﻛﻨﻴﺪ ﻛﻪ ﻳﻚ ﺗﺎرﻳﺦ و ﻳﻚ ﻣﻘﺪار ﻋﺪدي )ﺑﻪ ﻋﻨﻮان روز ( را درﻳﺎﻓﺖ‬ ‫و ﻣﻘﺪار ﻋﺪدي را از ﺗﺎرﻳﺦ ﻣﻮرد ﻧﻈﺮ ﻛﺴﺮ و ﻧﺘﻴﺠﻪ را ﺑﻪ ﻋﻨﻮان ﻳﻚ داده از ﻧﻮع ﻛﻼس ‪ myDate‬ﺑﺮﮔﺮداﻧﺪ‬ ‫ﺑﺮاي ﻣﺜﺎل اﮔﺮ از ﺗﺎرﻳﺦ ﺑﺮاﺑﺮ 22 آﺑﺎن 98 و روز ﺑﺮاﺑﺮ 01 ﺑﺎﺷﺪ ﺧﺮوﺣﻲ ﺑﺎﻳﺪ ﺑﺮاﺑﺮ ﺗﺎرﻳﺦ 21 آﺑﺎن 98 ﺑﺎﺷﺪ‬‫)‪myDate DiffDate(myDate d, int day‬‬‫{‬ ‫…‬‫}‬‫ﺑﺮﻧﺎﻣﻪ اي ﺑﻨﻮﻳﺴﻴﺪ ﻛﻪ ﺑﺎ اﺳﺘﻔﺎده از ﻛﻼس ‪ Employee‬ﻧﺎم، ﻧﺎم ﺧﺎﻧﻮادﮔﻲ،ﺗﺎرﻳﺦ ﺗﻮﻟﺪ و ﺗﺎرﻳﺦ ﺷﺮوع ﺑﻪ ﻛﺎر 01 ﻧﻔﺮ از ﭘﺮﺳﻨﻞ را‬ ‫ﮔﺮﻓﺘﻪ و ﺳﭙﺲ ﻧﻤﺎﻳﺶ دﻫﺪ.)راﻫﻨﻤﺎﻳﻲ : آراﻳﻪ اي از ﻧﻮع ‪ Employee‬ﺗﻌﺮﻳﻒ ﻛﻨﻴﺪ(‬ ‫ﺻﻔﺤﻪ 42‬
  25. 25. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫ﭘﺮﺳﺶ ﻫﺎي ﭘﺎﻳﺎن درس‬ ‫1- ﺑﻌﺪ از اﺟﺮاي ﻛﺎﻣﻞ دﺳﺘﻮرات زﻳﺮ ﻣﻘﺪار ﻣﺘﻐﻴﺮ ‪ k‬ﭼﻴﺴﺖ‬‫;5=‪int k‬‬‫;)++‪for(int i=0;i<10;i‬‬ ‫;++‪for(int j=5;j<20;j++) j‬‬ ‫د (5‬ ‫ج (6‬ ‫ب ( 02‬ ‫اﻟﻒ( 551‬ ‫2- ﺑﻌﺪ از اﺟﺮاي ﻛﺎﻣﻞ دﺳﺘﻮرات زﻳﺮ ﻣﻘﺪار ﻣﺘﻐﻴﺮ ‪ k‬ﭼﻴﺴﺖ‬‫;5=‪int k‬‬‫;)++‪for(int i=0;i<10;i‬‬ ‫;++‪for(int j=5;j<20;j++) k‬‬ ‫د (5‬ ‫ج (6‬ ‫ب ( 02‬ ‫اﻟﻒ( 551‬ ‫3- ﺧﺮوﺟﻲ ﻣﺘﺪ ‪ abc‬ﭼﻴﺴﺖ‬‫)‪int abc(int x, int y‬‬‫{‬ ‫;‪int t‬‬ ‫{ )‪while(x%y‬‬ ‫;‪t=y; y =x%y‬‬ ‫;‪x=t‬‬ ‫}‬ ‫;‪return y‬‬‫}‬ ‫( ﻣﺤﺎﺳﺒﻪ ب.م .م ﺑﻴﻦ دو ﻋﺪد ‪x ,y‬‬ ‫ب‬ ‫‪x ,y‬‬ ‫اﻟﻒ( ﻣﺤﺎﺳﺒﻪ ك.م .م ﺑﻴﻦ دو ﻋﺪد‬ ‫‪y‬‬ ‫د ( ﻣﺤﺎﺳﺒﻪ ﺧﺎرج ﻗﺴﻤﺖ ‪ x‬ﺑﺮ‬ ‫‪y‬‬ ‫ج ( ﻣﺤﺎﺳﺒﻪ ﺑﺎﻗﻴﻤﺎﻧﺪه ﺗﻘﺴﻴﻢ ‪ x‬ﺑﺮ‬ ‫4- ﻧﺘﻴﺠﻪ اﺟﺮاي ﻗﻄﻌﻪ ﻛﺪ زﻳﺮ ﭼﻴﺴﺖ‬‫{‪Class MyClass‬‬ ‫:‪Public‬‬ ‫};0==2%‪int a(int x){return x‬‬ ‫} ;0 ‪int b(int x){if(a(x)) return x; else return‬‬‫;}‬‫;‪MyClass x‬‬‫;)01(‪bool y=x.a‬‬ ‫اﻟﻒ( ﻣﺘﻐﻴﺮ ‪ y‬داراي ﻣﻘﺪار ‪ true‬ﻣﻲ ﺷﻮد‬ ‫ب ( ﺗﻌﺮﻳﻒ ﻛﻼس اﻳﺮاد دارد‬ ‫ج ( دﺳﺘﺮﺳﻲ ﺑﻪ ﻣﺘﺪ ‪ a‬ﺑﺎ ﺗﻮﺟﻪ ﺑﻪ ﺗﻌﺮﻳﻒ ﻛﻼس اﻣﻜﺎن ﭘﺬﻳﺮ ﻧﻴﺴﺖ و ﺑﺎﻳﺪ از ﻣﺘﺪ ‪ b‬ﺑﻪ ﺟﺎي آن اﺳﺘﻔﺎده ﺷﻮد‬ ‫د ( ﺑﺮﻧﺎﻣﻪ ﺑﺎ ﺧﻄﺎ ﻣﺘﻮﻗﻒ ﻣﻲ ﺷﻮد‬ ‫5- ﻳﻚ ﻛﻼس ﻃﺮاﺣﻲ ﻛﻨﻴﺪ ﻛﻪ :‬ ‫داراي دو وﻳﮋﮔﻲ ﺑﺮاي ﺗﻌﻴﻴﻦ دو ﻋﺪد ﺻﺤﻴﺢ ﺑﺎﺷﺪ‬ ‫•‬ ‫ﻣﺘﺪي ﺑﻪ ﻧﺎم ‪ BMM‬ﺑﺮاي ﺗﻌﻴﻴﻦ ﺑﺰرﮔﺘﺮﻳﻦ ﻣﻘﺴﻮم ﻋﻠﻴﻪ ﻣﺸﺘﺮك ﺑﻴﻦ دو ﻋﺪد ﻣﻮﺟﻮد در وﻳﮋﮔﻲ ﻫﺎي ﻓﻮق ﺑﺎﺷﺪ‬ ‫•‬ ‫ﻣﺘﺪي ﺑﻪ ﻧﺎم ‪ KMM‬ﺑﺮاي ﺗﻌﻴﻴﻦ ﻛﻮﭼﻜﺘﺮﻳﻦ ﻣﻀﺮب ﻣﺸﺘﺮك ﺑﻴﻦ دو ﻋﺪد ﻣﻮﺟﻮد در وﻳﮋﮔﻲ ﻫﺎي ﻓﻮق ﺑﺎﺷﺪ‬ ‫•‬ ‫6 - ﻣﺘﺪي ﺑﻨﻮﻳﺴﻴﺪ ﻛﻪ ﺑﺘﻮاﻧﺪ ﻳﻚ رﺷﺘﻪ ﻋﺪدي را درﻳﺎﻓﺖ و ﻣﺘﺘﻢ 9 آن را ﺑﺮﮔﺮداﻧﺪ.‬ ‫ﺻﻔﺤﻪ 52‬
  26. 26. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬‫7- ﻛﺪ ﻣﻠﻲ ﻳﻚ ﺷﻤﺎره 01 رﻗﻤﻲ اﺳﺖ ﻛﻪ رﻗﻢ ﺳﻤﺖ راﺳﺖ آن از روي ﺳﺎﻳﺮ ارﻗﺎم ﺑﺪﺳﺖ ﻣﻲ آﻳﺪ.ﺑﺪﻳﻦ ﺗﺮﺗﻴﺐ ﻛﻪ از‬‫ﺳﻤﺖ راﺳﺖ ارﻗﺎم ﻣﻮﺟﻮد در ﻣﻮﻗﻌﻴﺖ2 ﺗﺎ 01 آن در ﻣﻮﻗﻌﻴﺖ ﺧﻮدﺷﺎن ﺿﺮب ﻣﻲ ﺷﻮﻧﺪ و ﺳﭙﺲ ﻧﺘﻴﺠﻪ ﻫﺎ ﺑﺎ ﻫﻢ ﺟﻤﻊ‬‫ﺷﺪه و در ﻧﻬﺎﻳﺖ ﻋﺪد ﺣﺎﺻﻞ ﺑﺮ 11 ﺗﻘﺴﻴﻢ ﻣﻲ ﺷﻮد. اﮔﺮ ﺑﺎﻗﻴﻤﺎﻧﺪه ﻋﺪدي ﻛﻤﺘﺮ از 2 ﺑﺎﺷﺪ در اﻳﻦ ﺻﻮرت رﻗﻢ ﺳﻤﺖ‬‫راﺳﺖ ﺑﺎﻳﺪ ﺑﺮاﺑﺮ ﺑﺎﻗﻴﻤﺎﻧﺪه ﺑﺎﺷﺪ در ﻏﻴﺮ اﻳﻨﺼﻮرت رﻗﻢ ﺳﻤﺖ راﺳﺖ ﺑﺎﻳﺪ ﺑﺮاﺑﺮ ﻳﺎزده ﻣﻨﻬﺎي ﺑﺎﻗﻴﻤﺎﻧﺪه ﺑﺎﺷﺪ. ﻳﻚ ﻛﻼس‬‫ﻃﺮاﺣﻲ ﻛﻨﻴﺪ ﻛﻪ داراي ﻣﺘﺪي ﻋﻤﻮﻣﻲ ﺑﺮاي ﭼﻚ ﻛﺮدن اﻋﺘﺒﺎر ﻛﺪ ﻣﻠﻲ ﺑﺎﺷﺪ. )ﻛﺪ ﻣﻠﻲ را ﺑﮕﻴﺮد و در ﺻﻮرت اﻋﺘﺒﺎر ﻣﻘﺪار‬ ‫‪ true‬و در ﻏﻴﺮ اﻳﻨﺼﻮرت ﻣﻘﺪار ‪ false‬را ﺑﻪ ﻋﻨﻮان ﻧﺘﻴﺠﻪ ﺑﺮﮔﺮداﻧﺪ(‬‫ﻣﺜﺎل : ﻓﺮض ﻛﻨﻴﺪ ﻣﻲ ﺧﻮاﻫﻴﻢ اﻋﺘﺒﺎر ﻛﺪ ﻣﻠﻲ 3120288470 را ﺑﺮرﺳﻲ ﻛﻨﻴﻢ . در اﻳﻦ ﺻﻮرت ﻃﺒﻖ ﺟﺪول زﻳﺮ ارﻗﺎم 2 ﺗﺎ 01 ﻛﺪ‬ ‫را در ﻣﻮﻗﻌﻴﺖ ﺷﺎن ﺿﺮب ﻛﺮده و ﺣﺎﺻﻞ را ﺑﺎ ﻫﻢ‬ ‫ارﻗﺎم ﻛﺪ ﻣﻠﻲ‬ ‫0‬ ‫7‬ ‫4‬ ‫8‬ ‫8‬ ‫2‬ ‫3 1 2 0‬ ‫ﺟﻤﻊ ﻣﻲ ﻛﻨﻴﻢ )2+6+0+01+84+65+36+0 ﺑﺮاﺑﺮ‬ ‫ﻣﻮﻗﻌﻴﺖ رﻗﻢ‬ ‫01‬ ‫9‬ ‫8‬ ‫7‬ ‫6‬ ‫5‬ ‫1 2 3 4‬ ‫اﺳﺖ ﺑﺎ 712، ﺳﭙﺲ 712 را ﺑﺮ 11 ﺗﻘﺴﻴﻢ ﻣﻲ ﻛﻨﻴﻢ .‬ ‫ﺣﺎﺻﻞ ﺿﺮب‬ ‫0‬ ‫2 6 0 01 84 65 23 36‬ ‫ﺑﺎﻗﻴﻤﺎﻧﺪه اﻳﻦ ﺗﻘﺴﻴﻢ ﺑﺮاﺑﺮ 8 اﺳﺖ ﻛﻪ ﻛﻤﺘﺮ از 2‬ ‫ﻧﻴﺴﺖ ﭘﺲ رﻗﻢ ﺳﻤﺖ راﺳﺖ ﻛﺪ ﻣﻠﻲ ﺑﺎﻳﺪ 8-11 ﺑﺎﺷﺪ ﻛﻪ در ﻣﻮرد اﻳﻦ ﻛﺪ رﻗﻢ ﺳﻤﺖ راﺳﺖ 3 ﻣﻲ ﺑﺎﺷﺪ. ﺑﺎ اﻳﻦ ﺣﺴﺎب اﻳﻦ‬ ‫ﻛﺪ ﻣﻠﻲ ﺑﻪ ﻋﻨﻮان ﻳﻚ ﻛﺪ ﻣﻠﻲ ﻣﻌﺘﺒﺮ ﻣﻮرد ﻗﺒﻮل اﺳﺖ(‬ ‫روﻧﻮﻳﺴﻲ ﻋﻤﻠﮕﺮﻫﺎ‬ ‫ﺷﻜﻞ ﻛﻠﻲ روﻧﻮﻳﺴﻲ ﻋﻤﻠﮕﺮ ﻫﺎ ﺑﻪ ﺻﻮرت زﻳﺮ ﻣﻲ ﺑﺎﺷﺪ‬‫) >‪operator <operator symbol>( <parameters‬‬‫{‬ ‫;>‪<statements‬‬‫}‬ ‫ﻣﺜﺎل : ﺗﻌﺮﻳﻒ ﻋﻤﻠﮕﺮ ﻣﻨﻬﺎ ﺑﺮاي ﺗﻔﺎﺿﻞ دو ﺗﺎرﻳﺦ در ﻛﻼس ‪mydate‬‬‫)1‪myDate operator- (myDate d‬‬‫{‬ ‫;)1‪return DiffDate(myDate(Y,M,D),d‬‬‫}‬ ‫ﻧﺤﻮه اﺳﺘﻔﺎده از ﻋﻤﻠﮕﺮ ﻓﻮق‬‫;)10,40,88(‪myDate d,d1=myDate(88,01,01),d2=myDate‬‬‫;1‪d=d2-d‬‬‫;))(‪ShowMessage(d.ToString‬‬ ‫ﻣﺜﺎل : ﺗﻌﺮﻳﻒ ﻋﻤﻠﮕﺮ ﻣﻨﻬﺎ ﺑﺮاي ﻛﻢ ﻛﺮدن روز از ﻳﻚ ﺗﺎرﻳﺦ و ﺑﺪﺳﺖ آوردن ﺗﺎرﻳﺦ ﻣﻌﺎدل در ﻛﻼس ‪mydate‬‬‫---------------------------------------------------------------------//‬ ‫)‪myDate operator-(int count‬‬ ‫{‬ ‫{)0>‪while(count‬‬ ‫;--‪D‬‬ ‫;--‪count‬‬ ‫{)0==‪if(D‬‬ ‫;--‪M‬‬ ‫{)0==‪if(M‬‬ ‫;--‪Y‬‬ ‫;21=‪M‬‬ ‫}‬ ‫ﺻﻔﺤﻪ 62‬
  27. 27. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫;13=‪if(M>6) D=30; else D‬‬ ‫‪}//if‬‬ ‫‪}//while‬‬ ‫;)‪return myDate(Y,M,D‬‬ ‫}‬ ‫---------------------------------------------------------------------//‬ ‫ﻧﺤﻮه اﺳﺘﻔﺎده از ﻋﻤﻠﮕﺮ ﻓﻮق‬‫;)10,40,88(‪myDate d,d1=myDate‬‬‫;16-1‪d=d‬‬‫;))(‪ShowMessage(d.ToString‬‬ ‫ﺗﻤﺮﻳﻦ 1 : ﻋﻤﮕﺮﻫﺎي ++ و - - را ﺑﺮاي ﻛﻼس ‪ myDate‬ﺗﻌﺮﻳﻒ ﻛﻨﻴﺪ‬‫ﺗﻤﺮﻳﻦ 2 : ﻳﻚ ﻛﻼس ﺑﺮاي ﻛﺎر ﺑﺎ اﻋﺪاد ﻛﺴﺮي ﺗﻌﺮﻳﻒ ﻛﻨﻴﺪ ﻛﻪ داراي ﻋﻤﻠﮕﺮﻫﺎي + ، - ، * و / ﺑﺮاي ﺟﻤﻊ ، ﺗﻔﺮﻳﻖ ،‬ ‫ﺿﺮب و ﺗﻘﺴﻴﻢ اﻋﺪاد ﻛﺴﺮي ﺑﺎﺷﺪ.‬ ‫ﺻﻔﺤﻪ 72‬
  28. 28. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ : ‫رﺷﺘﻪ ﻫﺎ‬ ‫ﺑﻪ دﻧﺒﺎﻟﻪ اي از ﻛﺎراﻛﺘﺮﻫﺎ اﻃﻼق ﻣﻲ ﺷﻮد‬ ‫ و‬AnsiString ، ShortString: ‫ ﺳﻪ ﻧﻮع رﺷﺘﻪ ﻗﺎﺑﻞ ﺗﻌﺮﻳﻒ اﺳﺖ ﻛﻪ ﻋﺒﺎرﺗﻨﺪ از‬C++ Builder ‫در‬ WideString‫ﻛﺎرﺑﺮد‬ ‫ﺣﺎﻓﻈﻪ ﻣﻮد ﻧﻴﺎز‬ ‫ﺣﺪاﻛﺜﺮ ﻃﻮل‬ ‫ﻧﻮع‬‫ﺳﺎزﮔﺎري ﺑﺎ ﻧﮕﺎرش ﻫﺎي ﻗﺪﻳﻤﻲ‬ 2 to 256 bytes 255 characters ShortStringbackward compatibility‫ﻛﺎراﻛﺘﺮﻫﺎي 8 ﺑﻴﺘﻲ‬ 4 bytes to 2GB ~2^31 characters AnsiString8-bit (ANSI) characters‫ﻛﺎراﻛﺘﺮﻫﺎي ﻳﻮﻧﻲ ﻛﺪ‬ 4 bytes to 2GB ~2^30 characters WideStringUnicode characters; multi-user servers andmulti-languageapplications ‫( ﻧﻴﺰ اﻃﻼق ﻣﻲ ﺷﻮد‬Long String) ‫ در اﻏﻠﺐ ﻣﻮاﻗﻊ رﺷﺘﻪ ﺑﻠﻨﺪ‬AnsiString ‫ﺑﻪ ﻧﻮع‬ AnsiString ‫ﺑﺮرﺳﻲ ﻛﻼس‬ ‫ﺑﺮرﺳﻲ ﻣﺘﺪ ﻫﺎي ﻣﻬﻢ‬ AnsiString ‫ﻣﺘﺪ‬ ‫ اﻳﺠﺎد ﻣﻲ ﻛﻨﺪ. ﭘﺎراﻣﺘﺮ ورودي ﻣﻲ ﺗﻮاﻧﺪ ﺑﺮ ﺣﺴﺐ ﻣﻮرد ﻣﺘﻔﺎوت ﺑﺎﺷﺪ‬AnsiString ‫اﻳﻦ ﻣﺘﺪ ﻳﻚ ﻧﻤﻮﻧﻪ/ ﺷﻲ از ﻧﻮع ﻛﻼس‬ ‫ﻓﺮم ﻛﻠﻲ اﻳﻦ ﻣﺘﺪ ﺑﻪ ﺷﻜﻞ ﻫﺎي زﻳﺮ اﺳﺖ‬__fastcall AnsiString();__fastcall AnsiString(const char* src);__fastcall AnsiString(const AnsiString& src);__fastcall AnsiString(const char* src, unsigned int len);__fastcall AnsiString(const wchar_t* src);__fastcall AnsiString(int src);__fastcall AnsiString(double src);__fastcall AnsiString(char src);__fastcall AnsiString(short);__fastcall AnsiString(unsigned short);__fastcall AnsiString(unsigned int);__fastcall AnsiString(long);__fastcall AnsiString(unsigned long);__fastcall AnsiString(__int64);__fastcall AnsiString(unsigned __int64);__fastcall AnsiString(const WideString &src); : ‫ﻣﺜﺎل : ﺗﺒﺪﻳﻞ ﻋﺪد ﺑﻪ رﺷﺘﻪ‬int x=1234567;AnsiString s=AnsiString(x); 28 ‫ﺻﻔﺤﻪ‬
  29. 29. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫ﻣﺘﺪ ‪AnsiCompare‬‬ ‫اﻳﻦ ﻣﺘﺪ رﺷﺘﻪ ﻓﻌﻠﻲ را ﺑﺎ ﻳﻚ رﺷﺘﻪ دﻳﮕﺮ ﺑﺎ ﻫﻢ ﻣﻘﺎﻳﺴﻪ ﻣﻲ ﻛﻨﺪ و در ﺻﻮرﺗﻲ ﻛﻪ رﺷﺘﻪ ﻫﺎ ﺑﺎ ﻫﻢ ﺑﺮاﺑﺮ ﺑﺎﺷﻨﺪ ﻣﻘﺪار ﺻﻔﺮ و در‬ ‫ﺻﻮرﺗﻲ ﻛﻪ رﺷﺘﻪ رﺷﺘﻪ ﻓﻌﻠﻲ ﻛﻮﭼﻜﺘﺮ از رﺷﺘﻪ ورودي ﺑﺎﺷﺪ ﻣﻘﺪار ﻛﻤﺘﺮ ار ﺻﻔﺮ و در ﻏﻴﺮ اﻳﻦ ﺻﻮرت )رﺷﺘﻪ ﻓﻌﻠﻲ ﺑﺰرﮔﺘﺮ از‬ ‫رﺷﺘﻪ ورودي( ﻳﻚ ﻣﻘﺪار ﻣﺜﺒﺖ ﺑﻪ ﻋﻨﻮان ﻧﺘﻴﺠﻪ ﺑﺮﻣﻲ ﻛﺮداﻧﺪ.. )در اﻳﻦ ﻣﺘﺪ ﻋﻤﻞ ﺣﺴﺎس ﺑﻪ ﻣﺘﻦ اﺳﺖ(‬ ‫ﻓﺮم ﻛﻠﻲ اﻳﻦ ﻣﺘﺪ ﺑﻪ ﺷﻜﻞ زﻳﺮ اﺳﺖ‬‫;‪int __fastcall AnsiCompare(const AnsiString& rhs) const‬‬ ‫ﻣﺜﺎل :‬‫;"‪AnsiString s1="ali arash‬‬‫;)"‪int p=s1.AnsiCompare("arash‬‬ ‫در اﻳﻨﺠﺎ ﭼﻮن رﺷﺘﻪ ‪ ali arash‬از ﻧﻈﺮ ﻣﻮﻗﻌﻴﺖ در دﻳﻜﺸﻨﺮي ﺑﺎﻻﺗﺮ از ‪ arash‬ﻗﺮار ﻣﻲ ﮔﻴﺮد ﻧﺘﻴﺠﻪ 1- ﺑﻪ ﻋﻨﻮان ﺧﺮوﺟﻲ‬ ‫ﺑﺮﮔﺸﺖ داده ﻣﻲ ﺷﻮد‬ ‫ﻣﺘﺪ ‪AnsiCompareIC‬‬ ‫اﻳﻦ ﻣﺘﺪ ﻫﻤﺎﻧﻨﺪ ﻣﺘﺪ ‪ AnsiCompare‬ﻋﻤﻞ ﻣﻘﺎﻳﺴﻪ ﺑﻴﻦ رﺷﺘﻪ ﻓﻌﻠﻲ و ﻳﻚ رﺷﺘﻪ دﻳﮕﺮ را اﻧﺠﺎم ﻣﻲ دﻫﺪ ﺑﺎ اﻳﻦ ﺗﻔﺎوت ﻛﻪ در‬ ‫اﻳﻨﺠﺎ ﻋﻤﻞ ﻣﻘﺎﻳﺴﻪ ﺣﺴﺎس ﺑﻪ ﻣﺘﻦ ﻧﻴﺴﺖ‬ ‫ﻓﺮم ﻛﻠﻲ اﻳﻦ ﻣﺘﺪ ﺑﻪ ﺷﻜﻞ زﻳﺮ اﺳﺖ‬‫;‪int __fastcall AnsiCompareIC (const AnsiString& rhs) const‬‬ ‫ﻣﺘﺪ ‪AnsiPos‬‬ ‫اﻳﻦ ﻣﺘﺪ ﻣﻮﻗﻌﻴﺖ ﺷﺮوع ﻳﻚ رﺷﺘﻪ را داﺧﻞ رﺷﺘﻪ ﻓﻌﻠﻲ ﺑﺮﻣﻲ ﮔﺮداﻧﺪ. )ﺷﻤﺎره ﻣﻮﻗﻌﻴﺖ ﻫﺎ از 1 ﺷﺮوع ﻣﻲ ﺷﻮد ﻳﻌﻨﻲ 1 ﺑﺮاي‬‫ﻣﻮﻗﻌﻴﺖ اول و2 ﺑﺮاي ﻣﻮﻗﻌﻴﺖ دوﻣﻮ ...(. در ﺻﻮرﺗﻲ ﻛﻪ رﺷﺘﻪ ﻣﻮرد ﻧﻈﺮ در داﺧﻞ رﺷﺘﻪ اﺻﻠﻲ ﭘﻴﺪا ﻧﺸﻮد ﻣﻘﺪار ﺻﻔﺮ ﺑﺮﻛﺸﺖ داده‬ ‫ﻣﻲ ﺷﻮد.‬ ‫ﻓﺮم ﻛﻠﻲ اﻳﻦ ﻣﺘﺪ ﺑﻪ ﺷﻜﻞ زﻳﺮ اﺳﺖ‬‫;‪int __fastcall AnsiPos(const AnsiString& subStr) const‬‬ ‫ﻣﺜﺎل :‬‫;"‪AnsiString s1="ali arash‬‬‫;)"‪int p=s1. AnsiPos("arash‬‬ ‫در اﻳﻨﺠﺎ ﭼﻮن زﻳﺮ رﺷﺘﻪ ‪ arash‬از ﻣﻮﻗﻌﻴﺖ ﺷﻤﺎره 5 در رﺷﺘﻪ ‪ ali arash‬ﺷﺮوع ﻣﻲ ﺷﻮد ﻣﻘﺪار 5 ﺑﻪ ﻋﻨﻮان ﻣﻮﻗﻌﻴﺖ ﺷﺮوع‬ ‫ﺑﺮﮔﺸﺖ داده ﻣﻲ ﺷﻮد‬ ‫ﺻﻔﺤﻪ 92‬
  30. 30. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫ﻣﺘﺪ ‪Pos‬‬ ‫اﻳﻦ ﻣﺘﺪ ﻣﻮﻗﻌﻴﺖ ﺷﺮوع ﻳﻚ رﺷﺘﻪ را داﺧﻞ رﺷﺘﻪ ﻓﻌﻠﻲ ﺑﺮﻣﻲ ﮔﺮداﻧﺪ. )ﺷﻤﺎره ﻣﻮﻗﻌﻴﺖ ﻫﺎ از 1 ﺷﺮوع ﻣﻲ ﺷﻮد ﻳﻌﻨﻲ 1 ﺑﺮاي‬‫ﻣﻮﻗﻌﻴﺖ اول و2 ﺑﺮاي ﻣﻮﻗﻌﻴﺖ دوﻣﻮ ...(. در ﺻﻮرﺗﻲ ﻛﻪ رﺷﺘﻪ ﻣﻮرد ﻧﻈﺮ در داﺧﻞ رﺷﺘﻪ اﺻﻠﻲ ﭘﻴﺪا ﻧﺸﻮد ﻣﻘﺪار ﺻﻔﺮ ﺑﺮﻛﺸﺖ داده‬ ‫ﻣﻲ ﺷﻮد.‬ ‫ﻓﺮم ﻛﻠﻲ اﻳﻦ ﻣﺘﺪ ﺑﻪ ﺷﻜﻞ زﻳﺮ اﺳﺖ‬‫;‪int __fastcall Pos(const AnsiString& subStr) const‬‬ ‫ﻣﺜﺎل :‬‫;"‪AnsiString s1="ali arash‬‬‫;)"‪int p=s1.Pos("arash‬‬ ‫در اﻳﻨﺠﺎ ﭼﻮن زﻳﺮ رﺷﺘﻪ ‪ arash‬از ﻣﻮﻗﻌﻴﺖ ﺷﻤﺎره 5 در رﺷﺘﻪ ‪ ali arash‬ﺷﺮوع ﻣﻲ ﺷﻮد ﻣﻘﺪار 5 ﺑﻪ ﻋﻨﻮان ﻣﻮﻗﻌﻴﺖ ﺷﺮوع‬ ‫ﺑﺮﮔﺸﺖ داده ﻣﻲ ﺷﻮد‬ ‫ﻣﺘﺪ ‪c_str‬‬ ‫اﻳﻦ ﻣﺘﺪ رﺷﺘﻪ ﻣﻮﺟﻮد را ﺗﺒﺪﻳﻞ ﺑﻪ ﻳﻚ رﺷﺘﻪ ﻣﻨﺘﻬﻲ ﺑﻪ ﻛﺎراﻛﺘﺮ ‪ null‬ﻛﺮده و ﺑﻪ ﻋﻨﻮان ﺧﺮوﺟﻲ ﺑﺮﻣﻲ ﮔﺮداﻧﺪ‬ ‫ﻓﺮم ﻛﻠﻲ اﻳﻦ ﻣﺘﺪ ﺑﻪ ﺷﻜﻞ ﻫﺎي زﻳﺮ اﺳﺖ‬‫;‪char* __fastcall c_str() const‬‬ ‫ﻣﺜﺎل :‬‫;"‪AnsiString s1="ali arash‬‬‫;)(‪char *x=s1.c_str‬‬ ‫ﻣﺘﺪ ‪Length‬‬ ‫اﻳﻦ ﻣﺘﺪ ﻃﻮل رﺷﺘﻪ ﻣﻮﺟﻮد را ﻋﻨﻮان ﺧﺮوﺟﻲ ﺑﺮﻣﻲ ﮔﺮداﻧﺪ‬ ‫ﻓﺮم ﻛﻠﻲ اﻳﻦ ﻣﺘﺪ ﺑﻪ ﺷﻜﻞ ﻫﺎي زﻳﺮ اﺳﺖ‬‫;‪int __fastcall Length() const‬‬ ‫ﻣﺜﺎل : ﺑﺪﺳﺖ آوردن ﻃﻮل رﺷﺘﻪ‬‫;"‪AnsiString s1="ali arash‬‬‫;)(‪int l =s1.Length‬‬ ‫ﻣﺘﺪ ‪SubString‬‬ ‫اﻳﻦ ﻣﺘﺪ ﻳﻚ زﻳﺮ ﻣﺠﻤﻮﻋﻪ از رﺷﺘﻪ را ﺟﺪا ﻛﺮده و ﻋﻨﻮان ﺧﺮوﺟﻲ ﺑﺮﻣﻲ ﮔﺮداﻧﺪ‬ ‫ﻓﺮم ﻛﻠﻲ اﻳﻦ ﻣﺘﺪ ﺑﻪ ﺷﻜﻞ ﻫﺎي زﻳﺮ اﺳﺖ‬‫;‪AnsiString __fastcall SubString(int index, int count) const‬‬ ‫ﻣﺜﺎل : ﺟﺪاﻛﺮدن ﻗﺴﻤﺘﻲ از رﺷﺘﻪ )ﺟﺪا ﻛﺮدن ﻛﻠﻤﻪ ‪ name‬از رﺷﺘﻪ ‪( in the name of GOD‬‬‫;"‪AnsiString s1="in the name of GOD‬‬‫; )4,8(‪AnsiString s1 =s1.SubString‬‬ ‫ﺻﻔﺤﻪ 03‬
  31. 31. ‫آرش‬ ‫ﻣﺒﺎﺣﺚ وﻳﮋه‬ ‫آﻣﻮزﺷﻜﺪه ﻋﺎﻟﻲ ﻓﻨﻲ اﻟﺰﻫﺮا‬ ‫ﻣﺘﺪ ‪Trim‬‬ ‫اﻳﻦ ﻣﺘﺪ ﻛﻠﻴﻪ ﻓﺎﺻﻠﻪ ﻫﺎي ﺧﺎﻟﻲ ﻣﻮﺟﻮد در ﺳﻤﺖ ﭼﭗ و راﺳﺖ رﺷﺘﻪ را ﺣﺬف ﻛﺮده و رﺷﺘﻪ ﺟﺪﻳﺪي را ﺑﻪ ﻋﻨﻮان ﺧﺮوﺟﻲ ﺑﺮﻣﻲ‬ ‫ﮔﺮداﻧﺪ‬ ‫ﻓﺮم ﻛﻠﻲ اﻳﻦ ﻣﺘﺪ ﺑﻪ ﺷﻜﻞ ﻫﺎي زﻳﺮ اﺳﺖ‬‫;‪AnsiString __fastcall Trim() const‬‬ ‫ﻣﺜﺎل : ﺣﺬف ﻓﺎﺻﻠﻪ ﻫﺎي ﺧﺎﻟﻲ ﻣﻮﺟﻮد در ﺳﻤﺖ ﭼﭗ و راﺳﺖ رﺷﺘﻪ‬‫;" ‪AnsiString s1=" in the name of GOD‬‬‫;)(‪s1 =s1.Trim‬‬ ‫ﻣﺘﺪ ‪TrimLeft‬‬ ‫اﻳﻦ ﻣﺘﺪ ﻛﻠﻴﻪ ﻓﺎﺻﻠﻪ ﻫﺎي ﺧﺎﻟﻲ ﻣﻮﺟﻮد در ﺳﻤﺖ ﭼﭗ رﺷﺘﻪ را ﺣﺬف ﻛﺮده و رﺷﺘﻪ ﺟﺪﻳﺪي را ﺑﻪ ﻋﻨﻮان ﺧﺮوﺟﻲ ﺑﺮﻣﻲ ﮔﺮداﻧﺪ‬ ‫ﻓﺮم ﻛﻠﻲ اﻳﻦ ﻣﺘﺪ ﺑﻪ ﺷﻜﻞ ﻫﺎي زﻳﺮ اﺳﺖ‬‫;‪AnsiString __fastcall TrimLeft() const‬‬ ‫ﻣﺜﺎل : ﺣﺬف ﻓﺎﺻﻠﻪ ﻫﺎي ﺧﺎﻟﻲ ﻣﻮﺟﻮد در ﺳﻤﺖ ﭼﭗ رﺷﺘﻪ‬‫‪AnsiString s1=" in the name of GOD‬‬ ‫;"‬‫;)(‪s1 =s1.TrimLeft‬‬ ‫ﻣﺘﺪ ‪TrimRight‬‬ ‫اﻳﻦ ﻣﺘﺪ ﻛﻠﻴﻪ ﻓﺎﺻﻠﻪ ﻫﺎي ﺧﺎﻟﻲ ﻣﻮﺟﻮد در ﺳﻤﺖ راﺳﺖ رﺷﺘﻪ را ﺣﺬف ﻛﺮده و رﺷﺘﻪ ﺟﺪﻳﺪي را ﺑﻪ ﻋﻨﻮان ﺧﺮوﺟﻲ ﺑﺮﻣﻲ ﮔﺮداﻧﺪ‬ ‫ﻓﺮم ﻛﻠﻲ اﻳﻦ ﻣﺘﺪ ﺑﻪ ﺷﻜﻞ ﻫﺎي زﻳﺮ اﺳﺖ‬‫;‪AnsiString __fastcall Trim() const‬‬ ‫ﻣﺜﺎل : ﺣﺬف ﻓﺎﺻﻠﻪ ﻫﺎي ﺧﺎﻟﻲ ﻣﻮﺟﻮد در ﺳﻤﺖ راﺳﺖ رﺷﺘﻪ‬‫‪AnsiString s1=" in the name of GOD‬‬ ‫;"‬‫;)(‪s1 =s1.TrimRight‬‬ ‫ﻣﺘﺪ ‪WideChar‬‬ ‫اﻳﻦ ﻣﺘﺪ ﻛﺎراﻛﺘﺮﻫﺎي رﺷﺘﻪ ﻣﻮﺟﻮد را ﺗﺒﺪﻳﻞ ﺑﻪ ﻳﻮﻧﻲ ﻛﺪ ﻛﺮده و ﻧﺘﻴﺠﻪ را در ﻳﻚ ﺑﺎﻓﺮ ﻗﺮار ﻣﻲ دﻫﺪ‬ ‫ﻓﺮم ﻛﻠﻲ اﻳﻦ ﻣﺘﺪ ﺑﻪ ﺷﻜﻞ ﻫﺎي زﻳﺮ اﺳﺖ‬‫;‪wchar_t* __fastcall WideChar(wchar_t* dest, int destSize) const‬‬ ‫ﻣﺜﺎل :‬‫;"آﻣﻮزش زﺑﺎن ﺳﻲ"=1‪AnsiString s‬‬‫;)(‪int n=s1.Length‬‬ ‫;]‪wchar_t *x=new wchar_t[n‬‬ ‫;)‪x=s1.WideChar(x,n‬‬ ‫ﻧﻜﺘﻪ : اﻣﻜﺎن ﻛﺎر ﺑﺎ رﺷﺘﻪ ﻫﺎ ﺑﻪ ﺻﻮرت ﻳﻚ آراﻳﻪ ﻳﻚ ﺳﻄﺮي ﻫﻢ وﺟﻮد دارد ﺑﺮاي اﻳﻦ ﻣﻨﻈﻮر از ﻣﻲ ﺗﻮاﻧﻴﻢ از اﭘﺮاﺗﻮر ][ اﺳﺘﻔﺎده‬ ‫ﻛﻨﻴﻢ . ﺑﺮاي ﻣﺜﺎل اﮔﺮ در ﻣﺜﺎل ﻗﺒﻞ ﺑﺨﻮاﻫﻴﻢ ﺑﻪ ﻋﻨﺼﺮ ‪ i‬ام رﺷﺘﻪ 1‪ s‬دﺳﺘﺮﺳﻲ داﺷﺘﻪ ﺑﺎﺷﻴﻢ از ]‪ s1[i‬اﺳﺘﻔﺎده ﻛﻨﻴﻢ‬ ‫ﻣﺜﺎل : ﻣﻘﺪار دﻫﻲ ﻳﻚ آراﻳﻪ ﻛﺎراﻛﺘﺮي ﺑﺎ ﻛﺎراﻛﺘﺮﻫﺎي ﻳﻚ رﺷﺘﻪ ‪AnsiString‬‬‫;]5[‪char y‬‬‫)++‪for (int i=0;i<5;i‬‬ ‫ﺻﻔﺤﻪ 13‬

×