Академический Документы
Профессиональный Документы
Культура Документы
Usage of C++
By the help of C++ programming language, we can develop different types
of secured and robust applications:
o Window application
o Client-Server application
o Device drivers
o Embedded firmware
Features of C++
1. Simple
C++ is a simple language in the sense that it provides structured
approach (to break the problem into parts), rich set of library
functions, data types etc.
2. Machine Independent or Portable
Unlike assembly language, c programs can be executed in many
machines with little bit or no change. But it is not platform-
independent.
3. Mid-level programming language
C++ is also used to do low level programming. It is used to develop
system applications such as kernel, driver etc. It also supports the
Smt. Chandaben Mohanbhai Patel Institute of Computer
Applications - Changa
B.Sc.(IT) SEMESTER – III (Academic year 2019-20)
CA 503 Object Oriented Programming.
Note that C++ provides the flexibility of writing a program with or without
a class and its member functions definitions. A simple C++ program
(without a class) includes comments, headers, namespace, main() and
input/output statements.
Comments are a vital element of a program that is used to increase the
readability of a program and to describe its functioning. Comments are not
executable statements and hence, do not increase the size of a file.
C++ supports two comment styles: single line comment and multiline
comment. Single line comments are used to define line-by-line descriptions.
Double slash (//) is used to represent single line comments. To understand the
concept of single line comment, consider this statement.
/ An example to demonstrate single line comment It can also be written
as
/ / An example to demonstrate
/ / single line comment
Multiline comments are used to define multiple lines descriptions and are
represented as / * * /. For example, consider this statement.
Smt. Chandaben Mohanbhai Patel Institute of Computer
Applications - Changa
B.Sc.(IT) SEMESTER – III (Academic year 2019-20)
CA 503 Object Oriented Programming.
/* An example to demonstrate
multiline comment */
Generally, multiline comments are not used in C++ as they require more space
on the line. However, they are useful within the program statements where
single line comments cannot be used. For example, consider this statement.
Compiler ignores everything written after the single line comment and hence,
an error occurs. Therefore, in this case multiline comments are used. For
example, consider this statement.
These standard streams process data as a stream of characters, that is, data is
read and displayed in a continuous flow. The standard streams defined in
<iostream> are listed here.
cin (pronounced "see in") : It is the standard input stream that is associated
with the standard input device (keyboard) and is used to take the input from
users.
• cout (pronounced "see out") : It is the standard output stream that is
associated with the standard output device(monitor) and is used to display the
output to users.
• cerr (pronounced "see err") : It is the standard error stream that is
associated with the standard error device (monitor) and is used to report errors
to the users. The cerr object does not have a buffer (temporary storage area)
and hence, immediately reports errors to users. '
• clog (pronounced "see log"): It is the buffered error stream that is associated
with the standard error device (computer screen) and is used to report errors
to users. Unlike cerr, clog reports errors to users only when the buffer is full
Namespace:
Since its creation, C++ has gone through many changes by the C++ Standards
Committee. One of the new features added to this language is namespace. A
namespace permits grouping of various entities like classes, objects, functions
and various C++ tokens, etc., under a single name.
Different users can create separate namespaces and thus can use similar names
of the entities. This avoids compile-time error that may exist due to identical-
name conflicts.
The C++ Standards Committee has rearranged the entities of the standard
library under a namespace called std. In Figure, the statement using namespace
std informs the compiler to include all the entities present in the namespace
std. The entities of a namespace can be accessed in different ways which are
listed here.
• By specifying the using directive
using namespace std;
Smt. Chandaben Mohanbhai Patel Institute of Computer
Applications - Changa
B.Sc.(IT) SEMESTER – III (Academic year 2019-20)
CA 503 Object Oriented Programming.
cout<<"Hello World";
• By specifying the full member name
std: :cout<<"Hello World";
• By specifying the using declaration
using std:: cout;
cout<<"Hello World";
As soon as the new-style header is included, its contents are included in the
std namespace. Thus, all the modern C++ compilers support these statements.
#include<iostream>
using namespace std;
However, some old compilers may not support these statements. In that case,
the statements are replaced by this single statement.
#include<iostream.h>
Extension of C++ file is .cpp .
C++ is designed to be a compiled language. It is generally translated into
machine language that can be understood directly by the system.
Most frequently used and free available compiler is GNU C/C++ compiler.
After compilation, compiler will generate a.out executable file that will show
the output of program.
Smt. Chandaben Mohanbhai Patel Institute of Computer
Applications - Changa
B.Sc.(IT) SEMESTER – III (Academic year 2019-20)
CA 503 Object Oriented Programming.
C vs C++
2) Data is less secured in C. In C++, you can use modifiers for class members to
make it inaccessible for outside users.
5) In C, you can't use functions in In C++, you can use functions in structure.
structure.
7) In C, scanf() and printf() are C++ mainly uses stream cin and cout to perform
mainly used for input/output. input and output operations.
9) C programs are divided C++ programs are divided into functions and classes.
into procedures and modules
10) C does not provide the feature C++ supports the feature of namespace.
of namespace.
11) Exception handling is not easy C++ provides exception handling using Try and
in C. It has to perform using Catch block.
other functions.
Smt. Chandaben Mohanbhai Patel Institute of Computer
Applications - Changa
B.Sc.(IT) SEMESTER – III (Academic year 2019-20)
CA 503 Object Oriented Programming.
C++ I/O operation is using the stream concept. Stream is the sequence of
bytes or flow of data. It makes the performance fast.
If bytes flow from main memory to device like printer, display screen, or a
network connection, etc., this is called as output operation.
If bytes flow from device like printer, display screen, or a network
connection, etc to main memory, this is called as input operation.
I/O Library Header Files
Standard input stream (cin) The cin is a predefined object of istream class.
It is connected with the standard input device, which is usually a
keyboard.
The cin is used in conjunction with stream extraction operator (>>) to
read the input from a console.
The endl is a predefined object of ostream class.
It is used to insert a new line characters and flushes the stream.
Standard end line (endl) The endl is a predefined object of ostream class.
Smt. Chandaben Mohanbhai Patel Institute of Computer
Applications - Changa
B.Sc.(IT) SEMESTER – III (Academic year 2019-20)
CA 503 Object Oriented Programming.
C++ Variable
A variable is a name of memory location. It is used to store data.
Its value can be changed and it can be reused many times.
It is a way to represent memory location through symbol so that it can
be easily identified.
Example:
• int x;
• float y;
• char z;
• int x=5,b=10; //declaring 2 variable of integer type
Rules for defining variables
Smt. Chandaben Mohanbhai Patel Institute of Computer
Applications - Changa
B.Sc.(IT) SEMESTER – III (Academic year 2019-20)
CA 503 Object Oriented Programming.
Array in C++
o
Some Points to be Remembered.
Smt. Chandaben Mohanbhai Patel Institute of Computer
Applications - Changa
B.Sc.(IT) SEMESTER – III (Academic year 2019-20)
CA 503 Object Oriented Programming.
o Arrays have 0 as the first index not 1. In this example, mark[0] is the first
element.
o If the size of an array is n, to access the last element, (n-1) index is used.
In this example, mark[4] is the last element.
o Suppose the starting address of mark[0] is 2120d. Then, the next address,
a[1], will be 2124d, address of a[2] will be 2128d and so on. It's because
the size of float is 4 bytes.
o #include <iostream>
o using namespace std;
o
o int main(){
o int arr[] = {11, 22, 33, 44, 55};
o cout<<arr[0]<<endl;
o cout<<arr[1]<<endl;
o cout<<arr[2]<<endl;
o cout<<arr[3]<<endl;
o cout<<arr[4]<<endl;
o return 0;
o }
1-D array
int arr[10];
#include<iostream.h>
#include<conio.h>
Smt. Chandaben Mohanbhai Patel Institute of Computer
Applications - Changa
B.Sc.(IT) SEMESTER – III (Academic year 2019-20)
CA 503 Object Oriented Programming.
void main()
{
clrscr();
int arr[5] = {1, 2, 3, 4, 5};
int i;
for(i=0; i<5; i++)
{
cout<<"arr["<<i<<"] = "<<arr[i]<<"\n";
}
getch();
}
Inline Function
{
// this function is automatically inline
// function body
}
};
Good Programming style
class S
{
public:
int square(int s); // declare the function
};
Function Overloading
Function overloading is a feature in C++ where two or more functions can have
the same name but different parameters.
These functions having different number or type (or both) of parameters are
known as overloaded functions. For example:
int test() { }
int test(int a) { }
float test(double a) { }
Notice that, the return type of all these 4 functions are not same. Overloaded
functions may or may not have different return type but it should have different
argument(s).
// Error code
int test(int a) { }
The number and type of arguments passed to these two functions are same even
though the return type is different. Hence, the compiler will throw error.
****************************************************************