INTRODUCTION TO C++ AND ITS LANGUAGE BUILDING BLOCKS Engineer Jokhio Sultan Salahuddin Kohistani Lecturer, Computer Systems Engineering, MUET Jamshoro E-mail: [email protected]Facebook: http://www.facebook.com/salahuddinjokhio http://www.twitter.com/engrsalahuddinj
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
INTRODUCTION TO C++ AND ITS
LANGUAGE BUILDING BLOCKS
Engineer Jokhio Sultan Salahuddin Kohistani
Lecturer, Computer Systems Engineering, MUET Jamshoro E-mail: [email protected]
Programming is all about making the computer to do your own desired work.
“Computer is like a husband”, will do any thing that is said in well format and language according to appropriate language rules.
Programming is the art and science of instructing the computer (in an specific language) to do and carry out the said work according to the programmer‟s desires and intents.
Requires lot of interest and hard work.
Also requires analytical ability (as we are about to solve problems).
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
3
Mastering the programming
You must have Brain.
You must know tactics, such as how to use your Brain?
Draw rough sketch (or flow or solution) in your mind.
Program on a rough page (graphical using flow chart).
Don‟t directly use interactive, intelligent compilers to
write programs. Better to do with editors like notepad.
Make Mistakes and try to correct them.
Think out of Box.
Practice, Practice and Practice.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
4
Practice
Common English proverb, “Practice Makes the Man
perfect”.
Chinese Proverb,
“I hear, I forget;
I see, I remember;
I do, I understand.”
So, Practice as much as you can to master the
programming.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
5
Learning Approaches
Top Down.
Start from advance and cover the basics as you move further.
Used in west.
First build the roof then basic blocks.
Bottom Up.
Vice versa of Top Down Approach.
Our Approach.
Both approaches parallel.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
6
Computer Languages
High level Computer Languages
Syntax and Semantics(Rules of Language) very much similar to
daily life English sentences and phrases.
Easier to learn and easier to implement (to write code) very
difficult and complex logics.
C++, Java, PHP, ASP.NET, C#.NET all are high level languages.
Require either Compiler or Interpreter (or both) to translate
into respective Machine code (so that computer can
understand).
Mostly used to write Consumer and Enterprise Software
Applications.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
7
Computer Languages
Medium level Computer Languages
Syntax and Semantics are short hand or stenographic representations of commands and phrases.
For MOVE, we have mov keyword, for INSERT, we have ins keyword and so on.
Assembly Languages for programming Microprocessors, Microcontrollers all are Medium level languages.
Bit harder to difficult.
Requires knowledge of hardware.
Requires Assembler to translate into respective machine code.
Harder to learn.
Implementing complex logic may be difficult, but still applicable.
Used to write control programs for industry control systems, air control systems and other automation projects.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
8
Computer Languages
Low Level Language
Ones and Zeroes.
Extremely hard to understand for humans.
Understood by Computers only.
1 0 1 1 0 1 1
What
the hell
this
string
mean?
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
9
Towards C++ Language
Machine Language
“A” Language,
an Assembly Language
BCPL Language
B Language
C Language
C++ Language
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
10
C & C++ Language.
C is structured or Functional programming language.
Working on procedural methodology.
Also offers Assembly Language to be embedded inside C code.
C++, in addition to classic C Features, also offers
programming capability in Object Oriented Paradigm.
Object Oriented Paradigm offers depiction of real world in
programming and is including following key features.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
11
Object Oriented Programming
OOP
Polymorphism
Abstraction
Encapsulation
Inheritance
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
12
Object Oriented Programming
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
13
Abstraction
Ideas about problem solving. Rough Sketches.
Polymorphism
One thing having poly (many) morphs (shapes).
Inheritance
Allow child programs to inherit different properties from the
parent programs.
Encapsulation
Provides means of security, by encapsulating (closing and
hiding) the data inside its object with certain attributes. Like
capsules encapsulates medication.
Compilation of a C++ Program
Editing Preprocessing Compiling Linking Executing
A .cpp
File
This
requires
an editor
in which
programs
are
written
mostly in
notepad
Headers files and
namespaces
Since header
files are
imported
into the
source code,
to assure the
compatibility
this is done.
Its automatic
A single .obj File
or multiple
Here
translation
is done
from c++
to
respective
code
An exe
Executable File
Here
translated
obj files
and library
files are
combined
and one
exe file is
created
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
14
Steps to execute a C++ Program
1. Editing: Requires an editor program to write the source
code and must be saved as one of the extension
formats as .cpp, .C.
2. Compilation
1. Preprocessing: A C Preprocessor actually looks for the
imported files and classes and collect them for compiling, it
works on seeing the preprocessor directives.
2. Compilation: Program is compiled and Machine language
code for the respective platform is generated.
3. Linking: Compiled Libraries and others are linked.
4. Executing: .exe file is executed then to run the program.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
15
Basic C++ Program Structure
Preprocessor Directives
Using Directive
Start of Main Function
Program Statements
End of Main Function
Mostly some constants are
declared.
Namespaces are used to
identify different names.
This is where the execution
starts.
These all Program statements
are executed.
Execution Stops here.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
16
Basic Program outline
#include <some header file>
using namespace namespace-name
int main( ){
Statement(s) come here.
return 0;
}
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
17
Basic C++ Program
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
18
An Alternative Version
#include <iostream>
int main()
{
std::cout << “Every age has a
language of its own\n”;
return 0;
}
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
19
#include <iostream>
The # sign, indicates the use of any preprocessor directive. In this case include is the preprocessor directive.
Preprocessor directives are not part of the program, but they are instructions to the compiler, and preprocessor, a part of compiler program, deals with them.
It tells the compiler to include the header files in the program.
In older versions of C++, extension for header file (.H) was required.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
20
#include <iostream>
This iostream header file actually contains the definition
of cout, << operator and other basic input/output
operations.
Other header files could be found in the include folder of
your compiler.
Some common header files are math, ctype, dos, io etc.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
21
using namepace std;
The using is another directive, which is used to import
namespaces inside the program.
Each using command must end with a semicolon (;).
Namespace is the part of program, which identifies
different names and items inside c++ program statements.
The using directive tells the program that statements
inside main will be recognized using the std (standard)
namespace.
cout is also identified inside std namespace.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
22
int main( )
main is the perhaps the most essential part of any C++
program, which acts as the entry point for statement
execution.
The Parenthesis followed by main keyword tell the
compiler that it‟s the the function, and main shows that it
is the main function.
Statement inside main are actually executed.
Braces limit the function‟s boundary. { tells the starting of
the function, while } tells the ending of the function.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
23
cout << “Hello”;
An statement must end with semicolon ( ; ).
cout (console-out) is an standard output object which is
sending out to the standard connected output device.
Mostly Monitor is the standard output object.
The thing enclosed in “ “ will appear as it would be inside
it.
The “\n” will insert a new line after the output, and will
force the cursor to blink on the next line.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
24
return 0;
0 is going to be returned to operating system by main
function, which indicates the ending of program and
operating systems frees the memory (RAM) space, which
was occupied by the program
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
25
White Spaces and Blanks
White Spaces and Blanks are ignored by the compiler.
They are mostly for human readability. #include<iostream>
using namespace std;
int main(){
cout<<“Hello World!”;
return 0;
}
Each Header File must be written on
separate line, and space between #include
and <iostream> is not necessary. While the
space between using and namespace and
namespace‟s name is important. Space
between int and main is also necessary. So
there should be space between C++
reserved keywords. They also must not be
used for some other purpose. The Ending
bracket can also be put anywhere after the
return 0‟s semicolon.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
26
C++ Keywords
These are part of the C++ Language.
They can not be used for any other purpose than their
defined operation.
In previous program, include, using, namespace, int, main,
cout, return, they were all c++ reserved keywords.
They even can not be used to define **variables.
**Variables will be studied soon in future classes.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
27
cout
console out displays different data on the screen.
For string (a sequence of characters), enclose it into the
double quotes. E.g. cout<<“This is a Program”;
For a single character, single quotes E.g. cout<<„A‟;
For any number, without quotes or with quotes works
same. e.g. cout<<123; e.g. cout<<“123”;
Without quotes, it also performs the arithmetic
operation.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
28
cout
cout << “12a34”;
cout << “af4”;
cout<<“ ”;
cout<<„ ‟;
cout<<1+3;
cout<<1243;
cout<<“123+654”;
cout>>asd;
cout>>as23asc;
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
29
Cascading cout with <<
We can display multiple outputs in single cout statement
by cascading the << several times. So,
cout<<“Hello\n”<<123<<234+56; is equal to three
cout statements.
cout<<“Hello\n”;
cout<<123;
cout<<234+56;
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
30
Comments
They are not executed but are ignored by the compiler.
They are just for writing some message for future
reference purpose.
Two Types:
Single Line Comments & Multi- Line Comments
Single Line Comments
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
31
Multi-Line comments
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
32
Escape Sequences
They are some special characters
Do not appear on the screen, rather their effect can be seen,
sensed and felt.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
33
Manipulators
They are some keywords, which are used to manipulate
(modify/change) the output from its original appearance.
endl Manipulator
Inserts a new line, and is used with cout statement.
cout<< “Hello”<<endl<<“Bye”;
setw Manipulator
Inserts a some spaces in the output, and is used with cout
We already have used two functions, main is the function, which is most mandatory. we have also used other function named as setw(value), which was a manipulator.
Similarly, C++ offers a large variety of library function, which when called, perform some really useful operation. Like sqrt(double number), inside cmath header file calculates the square root of the argument** double type number and it is depicted in the next slide with program.
sqrt accepts type double and also returns double type result.
**argument is the input of the function.
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
59
Program // sqrt.cpp
// demonstrates sqrt() library function
#include <iostream> //for cout, etc.
#include <cmath> //for sqrt()
using namespace std;
int main()
{
double number, answer; //sqrt() requires type double
cout << “Enter a number: “;
cin >> number; //get the number
answer = sqrt(number); //find square root
cout << “Square root is “
<< answer << endl; //display it
return 0;
}
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
60
Casts
Casting Processing is done in order to convert from one
data type to the other.
Two types of casting.
Implicit casting (Also done Automatically by the compiler).
Explicit casting (By the Programmer with brute force).
Conversion can be implicit when converting from lower
to higher data types, because lower data types can be
accommodated in the larger ones, but vice versa may not
be true, some of the digits/precision numbers will be lost
in the alternative case (Converting from higher to lower)
Higher Data Type?? Lower Data Type?? Next Slide
61 Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
Automatic Conversions or Implicit Casting
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
62
Types are roughly designated “Higher” or “Lower” in the
below order.
Program on the next slide for demonstration.
Program
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
63
#include <iostream>
using namespace std;
int main()
{
int count = 7;
float avgWeight = 155.5F;
double totalWeight = count * avgWeight;
cout << “totalWeight=” << totalWeight << endl;
return 0;
} The Result inside totalWeight variable can still be accommodated in a
float variable, but double is used just to depict the automatic conversion.
Explicit Casting
Engr. J.S. Salahuddin Kohistani, Lecturer, CSE
MUET Jamshoro
64
Using static_cast keyword.
Will be covered in the future lectures.
Assignment # 02
Solve Exercise Questions from 1 to 10 of Chapter 2,
Robert Lafore‟s Object oriented Programming in C++.