برنامه نويسي پيشرفته مقدمه By: Ali Bazghandi. C++ Programming: Ali Bazghandi2...

Post on 20-Jan-2016

224 views 0 download

Transcript of برنامه نويسي پيشرفته مقدمه By: Ali Bazghandi. C++ Programming: Ali Bazghandi2...

برنامه نويسي پيشرفته

مقدمه

By: Ali Bazghandi

C++ Programming: Ali Bazghandi 2

نکات مهم

مراجع•• C++ FOR DUMMIES 5TH EDITION by Stephen Randy Davis• PROGRAMMING WITH C++ by JOHN R. HUBBARD, Ph.D.

(SCHAUM’S OUTLINE)

ارزشيابي دانشجويان•تکاليف و کالسي−پروژه−ميانترم−پايان ترم−

C++ Programming: Ali Bazghandi 3

C++تاريخچه زبان

• C++ was written by  Bjarne Stroustrup  at Bell Labs during 1983-1985. C++ is an extension of C.  Prior to 1983, Bjarne Stroustrup added features to C and formed what he called "C with Classes". He had combined the Simula's use of classes and object-oriented features with the power and efficiency of C. The term C++ was first used in 1983.

Bjarne Stroustrup

Dennis MacAlistair Ritchie

C++ Programming: Ali Bazghandi 4

• C++ was developed significantly after its first release.1 In particular, "ARM C++" added exceptions and templates, and ISO C++ added RTTI, namespaces, and a standard library.1

• C++ was designed for the UNIX system environment. With C++ programmers could improve the quality of code they produced and reusable code was easier to write.

C++ Programming: Ali Bazghandi 5

• Bjarne Stroustrup had studied in the doctoral program at the Computing Laboratory at Cambridge University prior to joining Bell Labs. Now, Bell Labs no longer has that name since part of Bell Labs became AT&T Labs.  The other half became Lucent Bell labs.

• Prior to C++, C was a programming language developed at Bell Labs circa 1969-1973. The UNIX operating system was also being developed at Bell Labs at the same time. C was originally developed for and implemented on the UNIX operating system, on a PDP-11 computer by Dennis Ritchie.

C++ Programming: Ali Bazghandi 6

• He extended the B language by adding types in 1971. He called this NB for New B. Ritchie credited some of his inspiration from theAlgol68 language. Ritchie restructured the language and rewrote the compiler and gave his new language the name "C"  in 1972. 90% of UNIX was then written in C. The committee that wrote the 1989 ANSI Standard for C had started work on the C Standard project in 1983 after having been established by ANSI in that year. There were quite a number of versions of C at that time and a new Standard was necessary.

C++ Programming: Ali Bazghandi 7

• C is portable, not tied to any particular hardware or operating system. C combines the elements of high-level languages with the functionality of assembly language and has occasionally been referred to as a middle-level computer language. C makes it easy to adapt software for one type of computer to another.

• C was a direct descendant of the language B. The language B was developed by Ken Thompson in 1970 for the new UNIX OS. B was a descendant of the language BCPL designed by Martin Richards, a Cambridge University student visiting MIT.1

C++ Programming: Ali Bazghandi 8

Ken Thompson

Martin Richards

C++ Programming: Ali Bazghandi 9

++Cويژگي هاي

•C++. گراست شي زبان يک•C ++استاندارد و پايين سطح زبان .ANSIو ISOيک است مانند • پايين سطح زبانهاي به تشابه وجود هاي Cبا برنامه

. کند مي توليد سريع و کارا بسيارجهت • توسعه قدرت داراي گرا شي زبان يک عنوان به

. داراست را بزرگ مقياس با هاي برنامه نوشتنروي • هاي برنامه بيشتر که زبانهاست مشهورترين از يکي

. اند شده نوشته زبان اين به شما سيستمداراي • است شده تضمين استاندارد داراي آنجاييکه از

. باشد مي حمل قابليتزبان • براي کامپايلري مهم عامل سيستم .Cهر دارد++

C++ Programming: Ali Bazghandi 10

برنامه چيست؟

++ يک فايل متني است که شامل Cيک برنامه •++ مي باشد که بر طبق Cترتيبي از دستورات

++ کنار هم قرار گرفته اند. اين فايل Cگرامر متني به عنوان فايل منبع شناخته مي شود. اين

است .cppفايل منبع بنا به قارداد داراي پسوند يک کامپايلر برنامه نوشته شده به زبان سطح باال •

را به زبان ماشين ترجمه مي کند.

C++ Programming: Ali Bazghandi 11

ساختمان کامپيوتر

C++ Programming: Ali Bazghandi 12

C++ [1]پردازش يک برنامه

++Cبراي اجراي يک برنامه نوشته شده به زبان سطح باال مانند •از يک ويرايشگر براي ايجاد برنامه استفاده مي کنيم.−جمالتي از برنامه که عالمت # دارد بوسيله برنامه پيش −

پردازشگر پردازش مي شود.از يک کامپايلر براي تست گرامر و تبديل برنامه به زبان ماشين −

استفاده مي شود. لينک کننده:−

براي SDKترکيب برنامه شي با ديگر برنامه هاي توليد شده توليد کد اجرايي

لود کننده:−

لود برنامه قابل اجرا به حافظه اصلي•

آخرين مرحله اجراي برنامه است.−

C++ Programming: Ali Bazghandi 13

برنامه يک C++ [2]پردازش

C++ Programming: Ali Bazghandi 14

Problem Solving Process

• Step 1 - Analyze the problem− Outline the problem and its requirements− Design steps (algorithm) to solve the problem

• Step 2 - Implement the algorithm− Implement the algorithm in code− Verify that the algorithm works

• Step 3 - Maintenance− Use and modify the program if the problem

domain changes

C++ Programming: Ali Bazghandi 15

C++ Programming: Ali Bazghandi 16

++Cيک برنامه

// my first program in C++ #include <iostream> //#include <iostream.h>using namespace std; //omittedint main (){ cout <<"Hello World!"; return 0;}

Output: Hello World!

C++ Programming: Ali Bazghandi 17

++Cيک برنامه ديگر

#include <iostream>using namespace std;int main() { cout << "My first C++ program." << endl; cout << "The sum of 2 and 3 = " << 5 << endl; cout << "7 + 8 = " << 7 + 8 << endl; return 0;} Sample Run: My first C++ program.The sum of 2 and 3 = 57 + 8 = 15

C++ Programming: Ali Bazghandi 18

:[1]نکته ها

•C++ حساس به حرف است )تفاوت بين حرف کوچک و بزرگ(

48تعداد کلمات کليدي )رزرو شده براي هدف خاص( •کلمه است:

C++ Programming: Ali Bazghandi 19

:[2]نکته ها

•C++ حساس به حرف است )تفاوت بين حرف کوچک و بزرگ(

دستورات به ; ختم مي شوند.•هر دستور مي تواند در چند سطر ادامه يابد.•در هر سط مي توان چند دستور را تايپ کرد.•(comments )توضيحات•

− // comment− /* comment */

(header files )فايلهاي سرآيند•(namespaces )فضاهاي اسمي•

C++ Programming: Ali Bazghandi 20

:فايلهاي سرآيند

• <algorithm>   <bitset>   <deque>   <exception>   <fstream>   <functional>   <iomanip>   <ios>   <iosfwd>   <iostream>   <istream>   <iterator>   <limits>   <list>   <locale>   <map>   <memory>   <new>   <numeric>   <ostream>   <queue>   <set>   <sstream>   <stack>   <stdexcept>   <streambuf>   <string>   <typeinfo>   <utility>   <valarray>   <vector>

C++ Programming: Ali Bazghandi 21

:[1]فضاي اسمي

++ که برخي کامپايلرها آنرا Cامکان جديد در •پشتيباني نمي کنند.

يک ناحيه تعريفي را ايجاد مي کند که عناصر •مختلف برنامه مي توانند در آن قرار گيرند.

اجازه گروهبندي موجوديت هايي مانند: کالسها، •اشيا و توابع را تحت يک نام مي دهند. اين امر امکان تقسيم بندي فضاي کلي را به چند زير

فضاي داراي نام مي دهد.

C++ Programming: Ali Bazghandi 22

:[2]فضاي اسمي

نحوه تعريف فضاي اسمي:•namespace identifier

{entities}

مثال:•namespace myNamespace { int a, b; }

نحوه دسترسي از خارج فضاي اسمي:•myNamespace::a myNamespace::b

C++ Programming: Ali Bazghandi 23

:[3]فضاي اسمي

مثال:•#include <iostream>

using namespace std;

namespace first {

int var = 5;

}

namespace second {

double var = 3.1416;

}

int main ()

{

cout << first::var << endl;

cout << second::var << endl;

return 0;

}

C++ Programming: Ali Bazghandi 24

:[4]فضاي اسمي

مثال:•#include <iostream> using namespace std; namespace first { int x = 5; int y = 10; } namespace second { double x = 3.1416; double y = 2.7183; } int main () { using first::x; using second::y; cout << x << endl; cout << y << endl; cout << first::y << endl; cout << second::x << endl; return 0; }

C++ Programming: Ali Bazghandi 25

:[5]فضاي اسمي

مثال:•#include <iostream>

using namespace std;

namespace first {

int x = 5; int y = 10;

}

namespace second {

double x = 3.1416; double y = 2.7183; }

int main ()

{

using namespace first;

cout << x << endl;

cout << y << endl;

cout << second::x << endl;

cout << second::y << endl;

return 0;

}

C++ Programming: Ali Bazghandi 26

:[6]فضاي اسمي

مثال:•#include <iostream>

using namespace std;

namespace first { int x = 5; }

namespace second { double x = 3.1416; }

int main ()

{

{

using namespace first;

cout << x << endl;

}

{

using namespace second;

cout << x << endl;

}

return 0;

}

C++ Programming: Ali Bazghandi 27

(:variablesمتغيرها )

همان مفهوم متغير در رياضيات است که براي نگهداري مثال يک •عدد در يک محل فرضي استفاده مي شود.

X=1اما:•

(x + 2) = y / 2

x + 4 = y

solve for x and y

انواع متغير:•x = 1

x = 2.3

x = “this is a sentence”

x = Texas

C++ Programming: Ali Bazghandi 28

انواع متغيرها:

C++ Programming: Ali Bazghandi 29

:[1]محدوده متغيرها

#include <iostream.h>#include <limits.h>// Prints the constants stored in limits.h:Main(){ cout << "minimum char = ' cc CHAR-MIN << endl; cout << "maximum char = ' << CHAR-MAX << endl; cout << "minimum short = ' << SHRT-MIN << endl; cout << "maximum short = ' cc SHRTJAX << endl; cout << "minimum int = ' cc INT-MIN << endl; cout << "maximum int = ' << INTJLAX << endl; cout << "minimum long = ' << LONG-MIN << endl; cout << "maximum long = 'I << LONG-MAX << endl; cout << "minimum signed char = ' cc SCHAR MIN << endl; - cout << "maximum signed char = ' -cc SCHARJAX << endl; cout << "maximum unsigned char = ' cc UCHARJAX << endl; cout << "maximum unsigned short = ' c-c USHRTJAX << endl; cout << "maximum unsigned = << UINT-MAX << endl; cout << "maximum unsigned long = ' << ULONGJAX << endl; return 0;{

C++ Programming: Ali Bazghandi 30

:[2]محدوده متغيرها

minimum char = -128maximum char = 127minimum short = -32768maximum short = 32767minimum int = -2147483648maximum int = 2147483647minimum long = -2147483648maximum long = 2147483647minimum signed char = -128maximum signed char = 127maximum unsigned char = 255maximum unsigned short = 65535maximum unsigned = 4294967295maximum unsigned long = 4294967295

C++ Programming: Ali Bazghandi 31

• char• short• int• unsigned char• unsigned short• unsigned• range -128 to 127 (1 byte)• range -32,768 to 32,767 (2 bytes)• range -2,147,483,648 to 2,147,483,647 (4 bytes)• range 0 to 255 (1 byte)• range 0 to 65,535 (2 bytes)• range 0 to 4,294,967,295 (4 bytes)

C++ Programming: Ali Bazghandi 32

ANSI-C <assert.h> <ctype.h>

<errno.h> <float.h> <iso646.h> <limits.h>

ANSI-C++ <cassert> <cctype> <cerrno> <cfloat> <ciso646> <climits>

ANSI-C <locale.h> <math.h> <setjmp.h> <signal.h> <stdarg.h> <stddef.h>

ANSI-C++ <clocale> <cmath> <csetjmp> <csignal> <cstdarg> <cstddef>

ANSI-C <stdio.h> <stdlib.h> <string.h> <time.h> <wchar.h> <wtype.h>

ANSI-C++ <cstdio> <cstdlib> <cstring> <ctime> <cwchar> <cwtype>