C++ Report File
C++ Report File
C++ Report File
On
C &C++ programming
Taken At
SUBMITTED BY:
SAHIL KUMAR
99200390001
Ihave taken efforts in this industrial training. However, it would not have
been possible without the kind support and help of many individuals and
organizations. I would like to extend my sincere thanks to all of them.
A Special thanks to electronics and communication department for giving me
the opportunity to pursue my industrial training in the institute.
I would like to express my special gratitude and thanks to industry persons
for giving us such attention and time. .
-SAHIL KUMAR
99200390001
2
ABSTRACT
3
CONTENTS
4
Brief History of C Programming Language
5
Constants and Variables in C language
:Constant
➔ As the name suggests the name constants is given to such variables or values in C
programming language which cannot be modified once they are defined. They are
fixed values in a program. There can be any types of constants like integer, float, octal,
hexadecimal, character constants etc. Every constant has some range. The integers
that are too big to fit into an int will be taken as a long. Now there are various ranges
that differ from unsigned to signed bits. Under the signed bit, the range of an int varies
from -128 to +127 and under the unsigned bit, int varies from 0 to 255.
Variables in c
To indicate the storage area, each variable should be given a unique name (identifier).
Variable names are just the symbolic representation of a memory location
6
Derived data types are nothing but primary datatypes but a little twisted or grouped
together like array, stucture, union and pointer. These are discussed in details later.
Data type determines the type of data a variable will hold. If a variable x is declared as int.
it means x can hold only integer values. Every variable which is used in the program must be
declared as what data-type it is.
Integer type:
➔ Integers are used to store whole numbers.
7
Floating point type:
Floating types are used to store real numbers.
Size and range of Integer type on 16-bit machine
Character type:
Character types are used to store characters value.
Size and range of Integer type on 16-bit machine
void type:
void type means no value. This is usually used to specify the type of functions which returns nothing. We
will get acquainted to this datatype as we start learning more advanced topics in C language, like functions,
pointers etc.
8
C Programming Operators
➔ C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication,
division etc on numerical values (constants and variables).
➔ C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common assignment operator
is =
➔ C Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the
relation is false, it returns value 0.
➔ C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression results
true or false. Logical operators are commonly used in decision making in C programming.
Logical AND. True only if all operands are If c = 5 and d = 2 then, expression ((c==5) &&
&& true (d>5)) equals to 0.
Logical OR. True only if either one operand If c = 5 and d = 2 then, expression ((c==5) ||
|| is true (d>5)) equals to 1.
9
C Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are
converted to bit-level which makes processing faster and saves power.
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
10
TYPES OF LOOP CONTROL STATEMENTS IN C:
There are 3 types of loop control statements in C language. They are,
1. for
2. while
3. do-while
Syntax for each C loop control statements are given in below table with description.
while (condition)
{ statements; }where,
While condition might be a>5, i<10
do { statements; }
while (condition);where,
do while condition might be a>5, i<10
ARRAY-
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same
type. An array is used to store a collection of data, but it is often more useful to think of an array as a
collection of variables of the same type.
11
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one
array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent
individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and
the highest address to the last element.
C - Functions
12
What are Pointers?
A pointer is a variable whose value is the address of another variable,
i.e., direct address of the memory location. Like any variable or
constant, you must declare a pointer before using it to store any
variable address. The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type
and var-name is the name of the pointer variable. The asterisk * used
to declare a pointer is the same asterisk used for multiplication.
However, in this statement the asterisk is being used to designate a
variable as a pointer. Take a look at some of the valid pointer
declarations −
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
The actual data type of the value of all pointers, whether integer, float,
character, or otherwise, is the same, a long hexadecimal number that
represents a memory address. The only difference between pointers of
different data types is the data type of the variable or constant that the
pointer points to.
13
() to concatenate two strings, memcpy() to copy one memory location to another location, and many more
functions.
A function can also be referred as a method or a sub-routine or a procedure, etc.
FILE * filePointer;
filePointer = fopen(“fileName.txt”, “r”);
fscanf(filePointer, "%s %s %s %d", str1, str2, str3, &year);
writing a file
FILE *filePointer ;
filePointer = fopen(“fileName.txt”, “w”);
fprintf(filePointer, "%s %s %s %d", "We", "are", "in", 2012);
14
Basic concepts of object oriented programming
15
What is OOPS?
Object Oriented Programming is a programming concept that works on the principle that objects
are the most important part of your program. It allows users create the objects that they want
and then create methods to handle those objects. Manipulating these objects to get results is
the goal of Object Oriented Programming.
1) Class
2) Object
3) Inheritance
4) Polymorphism
5) Encapsulation
16
1) Class
The class is a group of similar entities. It is only an logical component and not the physical
entity. For example, if you had a class called “Expensive Cars” it could have objects like
Mercedes, BMW, Toyota, etc. Its properties(data) can be price or speed of these cars.
While the methods may be performed with these cars are driving, reverse, braking etc.
2) Object
An object can be defined as an instance of a class, and there can be multiple instances of a
class in a program. An Object contains both the data and the function, which operates on the
data. For example - chair, bike, marker, pen, table, car, etc.
3) Inheritance
Inheritance is an OOPS concept in which one object acquires the properties and behaviors
of the parent object. It’s creating a parent-child relationship between two classes. It offers
robust and natural mechanism for organizing and structure of any software.
4) Polymorphism
Polymorphism refers to the ability of a variable, object or function to take on multiple forms.
For example, in English, the verb run has a different meaning if you use it with a laptop, a foot
race, and business. Here, we understand the meaning of run based on the other words used
along with it.The same also applied to Polymorphism.
5) Encapsulation
Encapsulation is an OOP technique of wrapping the data and code. In this OOPS concept, the
variables of a class are always hidden from other classes. It can only be accessed using the
methods of their current class. For example - in school, a student cannot exist without a class.
17
Constructors and Destructors in C++
Constructors are special class functions which performs initialization of every object. The Compiler calls the
Constructor whenever an object is created. Constructors initialize values to object members after storage is
allocated to the object.
Whereas, Destructor on the other hand is used to destroy the class object.
While defining a contructor you must remeber that the name of constructor will be same as the name of
the class, and contructors will never have a return type.
Constructors can be defined either inside the class definition or outside class definition using class name
and scope resolution :: operator.
1. Default Constructor
2. Parametrized Constructor
3. Copy COnstructor
18
Default Constructors
Default constructor is the constructor which doesn't take any argument. It has no parameter.
Syntax:
Parameterized Constructors
These are the constructors with parameter. Using this Constructor you can provide different
values to data members of different objects, by passing the appropriate values as argument.
Copy Constructors
These are special type of Constructors which takes an object as argument, and is used to
copy values of data members of one object into other object. We will study copy
constructors in detail later.
Destructors in C++
Destructor is a special class function which destroys the object as soon as the scope of
object ends. The destructor is called automatically by the compiler when the object goes out
of scope. Destructor will not have any argument.
The syntax for destructor is same as that for the constructor, the class name is used for the
name of destructor, with a tilde ~ sign as prefix to it.
class A
{
public:
// defining destructor for class
~A()
{
// statement
}
}
19
20
21
22
C++ Inheritance
In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent
object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are
defined in other class.
In C++, the class which inherits the members of another class is called derived class and the class whose
members are inherited is called base class. The derived class is the specialized class for the base class.
23
Types of Inheritance
C++ supports five types of inheritance:
o Single inheritance
o Multiple inheritance
o Hierarchical inheritance
o Multilevel inheritance
o Hybrid inheritance
Where 'A' is the base class, and 'B' is the derived class.
24
C++ Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of inheritance.
25
Polymorphism in C++
The word polymorphism means having many forms. Typically, polymorphism occurs when there is a
hierarchy of classes and they are related by inheritance.
C++ polymorphism means that a call to a member function will cause a different function to be executed
depending on the type of object that invokes the function.
26
Virtual Function
A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a
base class a virtual function, with another version in a derived class, signals to the compiler that we don't
want static linkage for this function.
What we do want is the selection of the function to be called at any given point in the program to be based
on the kind of object for which it is called. This sort of operation is referred to as dynamic linkage, or late
binding.
27
28
Pure Virtual Functions
It is possible that you want to include a virtual function in a base class so that it may be redefined in a
derived class to suit the objects of that class, but that there is no meaningful definition you could give for
the function in the base class.
-------------------------------------------------------------------------------
One of the advantages of C++ over C is Exception Handling. Exceptions are run-time
anomalies or abnormal conditions that a program encounters during its execution. There are
two types of exceptions: a) Synchronous, b) Asynchronous(Ex:which are beyond the
program’s control, Disc failure etc.). C++ provides following specialized keywords for this
purpose.
try: represents a block of code that can throw an exception.
catch: represents a block of code that is executed when a particular exception is thrown.
throw: Used to throw an exception. Also used to list the exceptions that a function throws,
but doesn’t handle itself.
29
----------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------
Templates in C++
A template is a simple and yet very powerful tool in C++. The simple idea is to pass data type as a
parameter so that we don’t need to write the same code for different data types. For example, a software
company may need sort() for different data types. Rather than writing and maintaining the multiple codes,
we can write one sort() and pass data type as a parameter.
C++ adds two new keywords to support templates: ‘template’ and ‘typename’. The second keyword can
always be replaced by keyword ‘class’.
How templates work?
Templates are expanded at compiler time. This is like macros. The difference is, compiler does type
checking before template expansion. The idea is simple, source code contains only function/class, but
compiled code may contain multiple copies of same function/class.
30
-----------------------------------------------------------------------
In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream available in
fstream header file.
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
Now the first step to open the particular file for read or write operation. We can open file by
1. passing file name in constructor at the time of object creation
2. using the open method
For e.g.
Open File by using constructor
ifstream (const char* filename, ios_base::openmode mode = ios_base::in);
ifstream fin(filename, openmode) by default openmode = ios::in
ifstream fin(“filename”);
END OF REPORT
32