Download - Basic structure of C++ program
Course ObjectivesCourse Objectives
Objectives of this course are three foldObjectives of this course are three fold1.1. To appreciate the need for a To appreciate the need for a
programming languageprogramming language2.2. To introduce the concept and usability To introduce the concept and usability
of the structured programming of the structured programming methodologymethodology
3.3. To develop a useful program for the To develop a useful program for the processes that are used in Physicsprocesses that are used in Physics
22
Course ContentsCourse ContentsTo achieve our first two objectives weTo achieve our first two objectives wewill be discussingwill be discussing Basic Programming constructs andBasic Programming constructs and building blocksbuilding blocks Structured programmingStructured programming OOP conceptsOOP concepts
33
Course ContentsCourse Contents Introduction to C++Introduction to C++ Variables and expressionsVariables and expressions Control structures Control structures
– Decision MakingDecision Making– LoopingLooping
User Defined FunctionsUser Defined Functions44
Course ContentsCourse Contents Arrays and PointersArrays and Pointers Dynamic memory Allocation Dynamic memory Allocation File handling File handling Structures and UnionsStructures and Unions Object oriented programming Object oriented programming
ConceptsConcepts
55
ResourcesResources Books: Books:
– C++ How to Program by Deitel & DeitelC++ How to Program by Deitel & Deitel– Let Us C++ by Yashavant Kanetkar Let Us C++ by Yashavant Kanetkar
Slides, Materials from Internet etc.Slides, Materials from Internet etc.
66
Course PolicyCourse PolicyPolicy for the distribution of marks andPolicy for the distribution of marks andexamination is as followsexamination is as follows Assignments/Quiz 15%Assignments/Quiz 15% Midterm 15 %Midterm 15 % Final 70%Final 70%
77
Instructor Profi leInstructor Profi le
Kamran UllahKamran Ullah Lecturer in CS/IT, IBMSLecturer in CS/IT, IBMS
– Since September 2014Since September 2014 PhD Scholar in Computer SciencePhD Scholar in Computer Science
– From IIUI since Fall 2011From IIUI since Fall 2011 MS (CS)MS (CS)
– From IIUI in July 2011From IIUI in July 2011
4½ years teaching experience in4½ years teaching experience in– Islamic Centre, University of Peshawar Islamic Centre, University of Peshawar – Iqra National University, PeshawarIqra National University, Peshawar– IIUI as visiting LecturerIIUI as visiting Lecturer
Computer Programming Computer Programming and Programming and Programming LanguageLanguage Writing a code in computer language Writing a code in computer language
which run again and again to do a which run again and again to do a specific task.specific task.
The term computer language includes The term computer language includes a wide variety of languages used to a wide variety of languages used to communicate with computers (i.e. C+communicate with computers (i.e. C++, Java)+, Java)
99
What is a Program?What is a Program?
““ A precise sequence of steps A precise sequence of steps toto
solve a particular problem”solve a particular problem”
1010
Crit ical Skil lsCrit ical Skil ls
– AnalysisAnalysis– Critical ThinkingCritical Thinking– Attention to DetailAttention to Detail
1313
Design RecipeDesign RecipeTo design a program properly, we To design a program properly, we
must:must:– Analyze a problem statement, typicallyAnalyze a problem statement, typically expressed as a word problemexpressed as a word problem– Express its essence, abstractly and withExpress its essence, abstractly and with examplesexamples– Formulate statements and comments in aFormulate statements and comments in a precise languageprecise language– Evaluate and revise the activit ies in l ight Evaluate and revise the activit ies in l ight
ofof checks and testschecks and tests
1414
Where to write program?Where to write program?
C++ is a compiler and compile your C++ is a compiler and compile your written program forwritten program for– Checking ErrorsChecking Errors– Ready to execute on computerReady to execute on computer
User should be provided a Environment User should be provided a Environment (IDE) to write, compile and test the (IDE) to write, compile and test the programprogram– i.e: Borland C++, Dev C++, Visual C++i.e: Borland C++, Dev C++, Visual C++– We will use Visual C++We will use Visual C++ 1515
First Program in C++First Program in C++
1.1. #include <iostream>#include <iostream>
2.2. using namespace std;using namespace std;
3.3. void main()void main()4.4. {{5.5. cout<<"In the name of Allah"<<endl;cout<<"In the name of Allah"<<endl;6.6. cout<<"This is my first Program in C++...";cout<<"This is my first Program in C++...";7.7. cin.get();cin.get();8.8. }}
1616
Explain the ProgramExplain the Program(line by l ine)(l ine by l ine)#include <iostream>#include <iostream>
‘‘#’#’ uses for preprocessor directive uses for preprocessor directive Here Here #include #include is used include the is used include the
library function defined in library function defined in iostreamiostream file file
1919
Explain the ProgramExplain the Program(line by l ine)(l ine by l ine)using namespace stdusing namespace std
Basically, what using Basically, what using namespace namespace stdstd does is to inject all the names of does is to inject all the names of entities that exist in the std entities that exist in the std namespace into the global namespacenamespace into the global namespace– i.e i.e coutcout and and endlendl
2020
Explain the ProgramExplain the Program(line by l ine)(l ine by l ine)void main()void main()
main() main() is the basic function telling the is the basic function telling the computer to where the executed computer to where the executed should be started. should be started.
void void tells the computer that main() tells the computer that main() function does not return any value.function does not return any value.
2121
Explain the ProgramExplain the Program(line by l ine)(l ine by l ine) { { is used to start the main function and is used to start the main function and
}} is used to end the main function. is used to end the main function. Actually it define a block of code.Actually it define a block of code.
Its mean that execution started in just Its mean that execution started in just below the below the {{ and will remain alive when and will remain alive when it it get the it it get the }}– But: A program has many blocksBut: A program has many blocks
2222
Program with many blocksProgram with many blocks
#include <iostream>#include <iostream>using namespace std;using namespace std;void main()void main(){{
int F;int F;cout<<“Enter value for F”;cout<<“Enter value for F”; cin>>F;cin>>F;if(F<0)if(F<0)
{{cout<<“F must be positive”;cout<<“F must be positive”;exit(0);exit(0);}}
cin.get();cin.get();}}
2323
Explain the ProgramExplain the Program(line by l ine)(l ine by l ine)cout<<"In the name of Allah"<<endl;cout<<"In the name of Allah"<<endl;
cout<< cout<< is used for output a lineis used for output a line– It may be a STRING directly given or may It may be a STRING directly given or may
a variable.a variable. endlendl is used to end the line (as we is used to end the line (as we
used ENTER button in MSWord)used ENTER button in MSWord)
2424
Explain the ProgramExplain the Program(line by l ine)(l ine by l ine) cin.get() cin.get() stops the execution of the stops the execution of the
program until the user press the program until the user press the ENTER button from keyboard.ENTER button from keyboard.
getch() getch() may also be used which is may also be used which is used to press any key.used to press any key.
2525
What does Semi-What does Semi-Colon(;) do?Colon(;) do? If you have noticed that there is a If you have noticed that there is a
semi-colon(;) at the end some of lines.semi-colon(;) at the end some of lines.
Remember it is a rule that every Remember it is a rule that every statement must be end with a semi-statement must be end with a semi-colon.colon.
2626
You can write multiple statement in a You can write multiple statement in a single line:single line:
cout<<“IIUI”;cin.get();
==cout<<“IIUI”; cin.get();
2727
What does Semi-What does Semi-Colon(;) do?Colon(;) do?