Sie sind auf Seite 1von 32

SUMMER TRAINING REPORT

On
C &C++ programming
Taken At
ENGINEERING COLLEGE, AJMER

In partial fulfilment for award of the Degree


Of
Bachelors of Technology
In Department of Computer Science & Information Technology

SUBMITTED BY:
ANKIT PARIHAR
18CS08

Department of Computer Science & Information Technology


Engineering College Ajmer 2019-20

1
ACKNOWLEDGEMENT

I have 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 computer science 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.

-ANKIT PARIHAR
(18EEACS008)

2
ABSTRACT

I am a student of Engineering College, Ajmer and studying in CSE 3 rd semester.


As a part of our degree we have undergone in a training process for 15 days.

Present is the age of computers and technologies. Practical knowledge has its
own importance. Without practical knowledge one cannot be specialized in
one’s field. We have automated the practical knowledge of institutes and
their working in the project.

3
CONTENTS

S. No Content Page No.


1 Brief history of C 5
2 Data types in C 6
3 Operations in C 9
4 Types of loop 11
5 C-Functions 12
6 Pointers
7 File Handling in C 14
8 OOP-Introduction 16
9 Constructor and 18
Destructor
10 Inheritance 23
11 Polymorphism 26
12 Exception Handling in 29
C++
13 Templates in C++ 30
14 File Handling in C++ 31

4
Brief History of C Programming Language

➔ C is a general-purpose programming language which features economy of


expression, modern control flow and data structures, and a rich set of
operators. C is not a "very high level" language, nor a "big" one, and is not
specialized to any particular area of application. But its absence of
restrictions and its generality make it more convenient and effective for
many tasks than supposedly more powerful languages.
➔ The history of C programming language is quite interesting. C was originally
designed for and implemented on the UNIX operating system on the DEC
PDP-ll, by Dennis Ritchie. C is the result of a development process that
started with an older language called BCPL. BCPL was developed by Martin
Richards, and it influenced a language called B, which was invented by Ken
Thompson. B led to the development of C in the 1970s.
➔ For many years, the de facto standard for C was the version supplied with
the UNIX operating system. In the summer of 1983 a committee was
established to create an ANSI (American National Standards Institute)
standard that would define the C language. The standardization process
took six years (much longer than anyone reasonably expected).
The ANSI C standard was finally adopted in December 1989

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

In programming, a variable is a container (storage area) to hold data.

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

Data types in C Language


➔ Data types specify how we enter data into our programs and what type of data we enter. C language
has some predefined set of data types to handle various kinds of data that we can use
in our program. These datatypes have different storage capacities.
C language supports 2 different type of data types:

1. Primary data types:

These are fundamental data types in C namely integer(int), floating point(float),


character(char) and void.

2. Derived data types:

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.

Size and range of Integer type on 16-bit machine:

Type Size(bytes) Range

int or signed int 2 -32,768 to 32767

unsigned int 2 0 to 65535

short int or signed short int 1 -128 to 127

unsigned short int 1 0 to 255

long int or signed long int 4 -2,147,483,648 to 2,147,483,647

unsigned long int 4 0 to 4,294,967,295

7
Floating point type:
Floating types are used to store real numbers.
Size and range of Integer type on 16-bit machine

Type Size(bytes) Range

Float 4 3.4E-38 to 3.4E+38

Double 8 1.7E-308 to 1.7E+308

long double 10 3.4E-4932 to 1.1E+4932

Character type:
Character types are used to store characters value.
Size and range of Integer type on 16-bit machine

Type Size(bytes) Range

char or signed char 1 -128 to 127

unsigned char 1 0 to 255

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 Increment and Decrement Operators


C programming has two operators increment ++ and decrement -- to change the value of an operand
(constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two
operators are unary operators, meaning they only operate on a single operand.

➔ 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.

Operator Meaning Example

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.

! Logical NOT. True only if the operand is 0

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 operators are used in C programming to perform bit-level operations.

Operators Meaning of operators

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

~ Bitwise complement

<< Shift left

>> Shift right

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.

Loop Name Syntax

for (exp1; exp2; expr3)


{ statements; }Where,
exp1 – variable initialization
( Example: i=0, j=2, k=3 )
exp2 – condition checking
( Example: i>5, j<3, k=3 )
exp3 – increment/decrement
For ( Example: ++i, j–, ++k )

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

A function is a group of statements that together perform a task. Every C


program has at least one function, which is main(), and all the most trivial
programs can define additional functions.
You can divide up your code into separate functions. How you divide up your
code among different functions is up to you, but logically the division is such that
each function performs a specific task.
A function declaration tells the compiler about a function's name, return type,
and parameters. A function definition provides the actual body of the function.
The C standard library provides numerous built-in functions that your program
can call. For example, strcat

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.

Basics of File Handling in C


So far the operations using C program are done on a prompt / terminal which is not stored
anywhere. But in the software industry, most of the programs are written to store the
information fetched from the program. One such way is to store the fetched information in a
file. Different operations that can be performed on a file are:
1. Creation of a new file (fopen with attributes as “a” or “a+” or “w” or “w++”)
2. Opening an existing file (fopen)
3. Reading from file (fscanf or fgetc)
4. Writing to a file (fprintf or fputs)
5. Moving to a specific location in a file (fseek, rewind)
6. Closing a file (fclose)
7. FILE *filePointer;
8. So, the file can be opened as
9. filePointer = fopen(“fileName.txt”, “w”)
opening a file

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.

Object Oriented Programming popularly known as OOP, is used in a modern programming


language like Java

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.

Types of Constructors in C++


Constructors are of three types:

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:

class_name(parameter1, parameter2, ...)


{
// constructor Definition
}

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.

Advantage of C++ Inheritance


Code reusability: Now you can reuse the members of your parent class. So, there is no need to define the
member again. So, less code is required in the 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

C++ Single Inheritance


Single inheritance is defined as the inheritance in which a derived class is inherited from the only one base
class.

Where 'A' is the base class, and 'B' is the derived class.

C++ Multilevel Inheritance


Multilevel inheritance is a process of deriving a class from another derived class.

C++ Multiple Inheritance


Multiple inheritance is the process of deriving a new class that inherits the attributes from two or more
classes.

24
C++ Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of inheritance.

C++ Hierarchical Inheritance


Hierarchical inheritance is defined as the process of deriving more than one class from a base class.

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
-----------------------------------------------------------------------

File Handling through C++ Classes

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”);

Open File by using open method


Calling of default constructor
ifstream fin;
31
fin.open(filename, openmode)
fin.open(“filename”);

END OF REPORT

32