Introduction C++
Introduction C++
Introduction C++
Chapter 2
INTRODUCTION to C++
C++ was introduced by Bjarne Stroustrup at Bell labs in 1980, developed from the C
language introduced by Dennis Ritchie in 1972. The name C is used as the successor to the B
language introduced by Ken Thompson who is the successor of the BPCL (Basic Combined
Programming Language) language introduced by Martin Richard in 1967. The name C++ denotes
an addition, i.e., class, but was originally named "C with class" which could facilitate object-
oriented programming.
Programming
Inventors
language
Bcpl Martin Richard
B Ken Thompson
C Dennis Ritchie
C++ Bjarne Strouptrup
The major difference between C and C++ is that C is a procedural programming language,
which means that it is derived from sequential step by step structured programming. On the
other hand, C++ is a combination of procedural programming as well as object-oriented
programming. Objects consists of Data in form of its characteristics and are coded in the form of
methods. In object-oriented programming computer programs are designed using the concept
of objects that interact with the real world. Some of the differences between c and c++ can be
seen in table below:
C c++
Since C is a procedural programming, it
C++ is an object driven language.
is a function driven language.
In C, functions cannot be defined inside In C++, functions can be used inside a
structures structure.
C uses functions for input/output. For C++ uses objects for input output. For
example, scanf and printf. example, cin and cout.
C does not provide direct support for C++ provides support for exception
error handling (also called exception handling. Used for errors that make
handling) the code incorrect.
C does not support reference variables. C++ supports reference variables.
Header
In C++, all the header files may or may not end with the “.h” extension but in C, all the
header files must necessarily end with the “.h” extension. There are of 2 types of header file:
1. Pre-existing header files: Files which are already available in C/C++ compiler we just need to
import them.
2. User-defined header files: These files are defined by the user and can be imported using
“#include”.
Syntax:
2.2. Identifier
An identifier is a name commonly used in programming to express variables, constants,
data type, function, label, object, as well as other things declared or defined by programmers.
Rules for constructing identifier names:
1) Can consist of letters, digits, and underscores
2) Must begin with a letter or an underscore
3) These are case sensitive
4) Whitespaces or special characters like !, #, %, etc. Are not allowed
5) Reserved words cannot be used as names
Note: The maximum length of the identifying name on C++ depends on the compiler used. For
example, Borland C++ allows identifying names up to 32 characters (which is significant, the
advantages will be ignored), while Turbo C++ guarantees a significant name of up to 31
characters.
Reserved Words:
auto const double float int short struct unsigned
break continue else for long signed switch void
case default enum goto register sizeof typedef volatile
char do extern if return static union while
asm dynamic_cast namespace reinterpret_cast try bool explicit new
static_cast typeid catch false operatot template typename class
friend private this using const_cast inline public throw
virtual delete mutable protected true Wchar_t
2.5. Variables
Variables are containers for storing data values or data that can change during the
process. To create a variable, you must specify the type and assign it a value, it’s called a variable
declaration.
Syntax: data_type variable_name;
Example: int a;
Int a, b, c;
char grade;
float value1;
Manipulator
Manipulators are commonly used to organize the appearance of data.
Manipulator Description
Inserts a newline and sends the output buffer contents to
Endl the output device.
cout<<information<<endl;
Insert null characters.
Ends
cout<<information<<ends;
Sends the output buffer contents to the output device.
Flush
cout<<information<<flush;
Convert to base 10
Dec
cout<<dec<<information<<endl;
Convert to base 16
Hex
cout<<hex<<information<<endl;
Convert to base 8
Oct
cout<<oct<<information<<endl;
Convert to base n (n=8, 10 or 16)
Setbase(int n)
cout<<setbase(n)<<information<<endl;
Set the field width for a value as big as n characters.
Setw(int n)
cout<<setw(n)<<information<<endl;
Set the brewing character in the form of c.
Setfill(int c)
cout<<setfill(c);
Sets the precision of a fractional number of n digits.
Setprecision(int n) cout<<setiosflags (ios::fixed);
cout<<setprecision(n)<<information<endl;
Setting the format specified by f. f is the format sign
Setiosflags(long f)
cout<<setiosflags(f);
Removing the format specified by f. f is a format sign.
Resetiosflags(long f)
cout<<resetiosflags(f);
Note: If using a manipulator other than dec, hex, oct, endl, flush, header file named iomanip.h
needs to be included.
Examples:
setprecision()
Result:
resetiosflag()
Result:
Result:
Assigment:
1. Create a program with input and output with this template:
Your name : ___________
Your Place of Birth : ___________ input
“Hello, my name is (your name) and I born at (your place of birth) → output
2. Create a program to find the area of the triangle if it is known base=10 and height=20.
3. Create a program for calculating the total price, with the following conditions:
Input : Item Code, Item Name, Item price and number of items
Output: total price
4. A car travels at a fixed speed v km/h. If the car runs for t hours, write an algorithm to calculate
the distance that has been travelled by the car (in km). The algorithm reads inputs in the
form of v and t, calculates the distance with the formula s = vt, and then print the distance.
Draw a flowchart to solve it.
5. Translate the algorithm on problem number 4 into the program in c++, then test the program
with various values v and t.