You are on page 1of 52

ABSTRACT

C:
The C programming language is a computer programming language that was
developed to do system programming for the operating system UNIX and is
an imperative programming language. C was developed in the early 1970s by Ken
Thompson and Dennis Ritchie at Bell Labs. It is a procedural language, which
means that people can write their programs as a series of step-by-step instructions.
C is a compiled language. Because the ideas behind C are kept close to the design
of the computer, the compiler (program builder) can generate machine code/native
code for the computer. Programs built in machine code are very fast. This makes C
a good language for writing operating systems. Many operating systems,
including Linux and UNIX, are programmed using this language. The language
itself has very few keywords, and most things are done using libraries, which are
collections of code for them to be reused.

C++:
The C++ programming language was created by Bjarne Stroustrup and his team at
Bell Laboratories (AT&T, USA) to help implement simulation projects in an
object-oriented and efficient way. The earliest versions, which were originally
referred to as “C with classes,” date back to 1980. As the name C++ implies, C++
was derived from the C programming language: ++ is the increment operator in C.
In 1998 the ISO (International Organization for Standardization) approved a
standard for C++ (ISO/IEC 14882). C++ is not a purely object-oriented language
but a hybrid that contains the functionality of the C programming language. This
means that you have all the features that are available in C:
■ Universally usable modular programs.
■ Efficient, close to the machine programming.
■ Portable programs for various platforms.

1
C++:

INDEX
S.No Topic Page No.

01. Introduction to c++ 1

02. C++ fundamentals 2-3

03. Structure of C++ program 4-6

04. If Else Statement 7-10

05. Iteration Statements 11-17

06. Function 18-20

07. Pointer 21-22

08. Array 23-25

09. Linked List 26-29

2
C:

INDEX
S.No Topic Page No.

01. Introduction to c 30

02. Structure of C program 30-32

03. If Else Statement 33-34

04. Iteration Statements 35-37

05. Function 38-39

06. Array 39-40

07. Pointer 41-42

08. Hospital management system 43-48

09. Conclusion 49

3
Introduction to C++

C++ is not a purely object-oriented language but a hybrid that contains the
functionality of the C programming language. The features that are available in C:
■ universally usable modular programs
■ efficient close to the machine programming
■ portable programs for various platforms.

Case Sensitivity:
C++ is case sensitive. In other words, uppercase and lowercase letters
are considered to be different.

C++ supports the concepts of object-oriented programming which are:


■ data abstraction, that is, the creation of classes to describe objects
■ data encapsulation for controlled access to object data
■ inheritance by creating derived classes
■ polymorphism i.e. the implementation of instructions that can have varying
effects during program execution.

Objects
Object-oriented programming shifts the focus of attention to the objects, that is, to
the aspects on which the problem is centered .OOP objects combine data and
functions (capacities). A class defines a certain object type by defining both the
properties and the capacities of the objects of that type.

Advantages of OOP
Object-oriented programming offers several major advantages to software
development:
■ reduced susceptibility to errors: an object controls access to its own data. More
specifically, an object can reject erroneous access attempts.
■ easy re-use: objects maintain themselves and can therefore be used as building
blocks for other programs.
■ low maintenance requirement: an object type can modify its own internal data
representation without requiring changes to the application.

1
C++ Fundamentals
The following three steps are required to create and translate a C++ program:

1. First, a text editor is used to save the C++ program in a text file. In other words,
the source code is saved to a source file. In larger projects the programmer will
normally use modular programming. This means that the source code will be
stored in several source files that are edited and translated separately.

2. The source file is put through a compiler for translation. If everything works as
planned, an object file made up of machine code is created. The object file is also
referred to as a module.

3. Finally, the linker combines the object file with other modules to form an
executable file.

It is important to use the correct file extension for the source file’s name. Although
the file extension depends on the compiler you use, the most commonly found file
extensions are .cpp and .cc.

Fundamental Types, Constants, and Variables

2
Variable:

In C++ a variable is a place to store information. A variable is a location in your


computer’s memory in which you can store a value and from which you can later
retrieve that value.

Keywords
Some words are reserved by C++, and you may not use them as variable names.
These are keywords used by the compiler to control your program. Keywords
include if, while, for, and main.

Characters
Character variables (type char) are typically 1 byte, enough to hold 256 values (see
Appendix C). A char can be interpreted as a small number (0–255) or as a member
of the ASCII set. ASCII stands for the American Standard Code for Information
Interchange. The ASCII character set and its ISO (International Standards
Organization) equivalent are a way to encode all the letters, numerals, and
punctuation marks.

Constants
Like variables, constants are data storage locations. Unlike variables, and as the
name implies, constants don’t change. You must initialize a constant when you
create it, and you cannot assign a new value later.
C++ has two types of constants: literal and symbolic.

3
Structure of a c++ program

Simple output

4
1. Header File:

#include <iostream>

Lines beginning with a hash sign (#) are directives for the preprocessor. They are
not regular code lines with expressions but indications for the compiler's
preprocessor. In this case the directive #include <iostream> tells the preprocessor
to include the iostream standard file. This specific file (iostream) includes the
declarations of the basic standard input-output library in C++, and it is included
because its functionality is going to be used later in the program.

2. using namespace std;


All the elements of the standard C++ library are declared within what is called a
namespace, the namespace with the name std. So in order to access its functionality
we declare with this expression that we will be using these entities.

3. Main function:

int main ()

This line corresponds to the beginning of the definition of the main function. The
main function is the point by where all C++ programs start their execution,
independently of its location within the source code.

The word main is followed in the code by a pair of parentheses (()). That is
because it is a function declaration: In C++, what differentiates a function
declaration from other types of expressions are these parentheses that follow its
name.

4. Body:

Right after these parentheses we can find the body of the main function enclosed in
braces ({}). What is contained within these braces is what the function does when
it is executed.

5
Program to add two numbers

Output:

6
if-else Statement

Sometimes we need to execute a block of statements only when a particular


condition is met or not met. This is called decision making, as we are executing a
certain code after making a decision in the program logic. For decision making in
C++, we have four types of control statements (or control structures), which are as
follows:

a) if statement
b) nested if statement
c) if-else statement
d) if-else-if statement

If statement in C++:

7
If else statement in C++:
Sometimes you have a condition and you want to execute a block of code if
condition is true and execute another piece of code if the same condition is false.
This can be achieved in C++ using if-else statement.

Check Whether Number is Even or Odd using if else

8
Output:

Program 4. Check Whether Number is Prime or not using if else

9
Output:

10
Iteration Statements

A loop statement allows us to execute a statement or group of statements multiple


times and following is the general from of a loop statement in most of the
programming languages –

C++ programming language provides the following type of loops to handle looping
requirements. There are three type of loops in C++ programming:

1. for loop
2. while loop
3. do...while loop

11
For Loop:

How for loop works?


1. The initialization statement is executed only once at the beginning.
2. Then, the test expression is evaluated.
3. If the test expression is false, for loop is terminated. But if the test
expression is true, codes inside body of for loop is executed and update
expression is updated.
4. Again, the test expression is evaluated and this process repeats until the test
expression is false.

12
Program 5. Table of an integer:

Display Multiplication table up to 10

Output:

13
While Loop:

How while loop works?


 The while loop evaluates the test expression.
 If the test expression is true, codes inside the body of while loop is
evaluated.
 Then, the test expression is evaluated again. This process goes on until the
test expression is false.
 When the test expression is false, while loop is terminated.

14
Program 6. Fibonacci series:

Output:

15
Do While Loop:

How do...while loop works?


 The codes inside the body of loop is executed at least once. Then, only the
test expression is checked.
 If the test expression is true, the body of loop is executed. This process
continues until the test expression becomes false.
 When the test expression is false, do...while loop is terminated.

16
Program 7.Armstrong Number :

Output:

17
Function:

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.

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, function strcat() to concatenate two strings,
function memcpy() to copy one memory location to another location and many
more functions.

A C++ function definition consists of a function header and a function body. Here
are all the parts of a function −
 Return Type − A function may return a value. The return_type is the data
type of the value the function returns. Some functions perform the desired
operations without returning a value. In this case, the return_type is the
keyword void.
 Function Name − This is the actual name of the function. The function
name and the parameter list together constitute the function signature.
 Parameters − A parameter is like a placeholder. When a function is
invoked, you pass a value to the parameter. This value is referred to as actual
parameter or argument. The parameter list refers to the type, order, and
number of the parameters of a function. Parameters are optional; that is, a
function may contain no parameters.
 Function Body − The function body contains a collection of statements that
define what the function does.

Types of function
We have two types of function in C++:

18
1) Build-in functions

Built-in functions are also known as library functions. We need not to declare and
define these functions as they are already written in the C++ libraries such as
iostream, cmath etc. We can directly call them when we need.

2) User-defined functions

The functions that we declare and write in our programs are user-defined
functions.

19
Program 8.Check if the given no is palindrome or not using function:

Output:

20
Pointers
Pointers are powerful features of C++ that differentiates it from other programming
languages like Java and Python.
Pointers are used in C++ program to access the memory and manipulate the
address.

Address in C++:

To understand pointers, you should first know how data is stored on the computer.
Each variable you create in your program is assigned a location in the computer's
memory. The value the variable stores is actually stored in the location assigned.
To know where the data is stored, C++ has an & operator. The & (reference)
operator gives you the address occupied by a variable. If var is a variable
then, &var gives the address of that variable.

Following are the valid pointer declaration –

int *ip; // pointer to an integer


double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character

Using Pointers in C++:

There are few important operations, which we will do with the pointers very
frequently. (a) We define a pointer variable. (b) Assign the address of a variable to
a pointer. (c) Finally access the value at the address available in the pointer
variable. This is done by using unary operator * that returns the value of the
variable located at the address specified by its operand.

21
Program 9. Program to Swap Elements Using Call by Reference:

Output:

22
Array

C++ provides a data structure, the array, which stores 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.
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.

Declaring Arrays:
To declare an array in C++, the programmer specifies the type of the elements and
the number of elements required by an array as follows –
type arrayName [ arraySize ];

Arrays in C++:

Arrays are important to C++ and should need lots of more detail. There are
following few important concepts, which should be clear to a C++ programmer –

Sr.N Concept & Description


o

1 Multi-dimensional arrays
C++ supports multidimensional arrays. The simplest form of the
multidimensional array is the two-dimensional array.

2 Pointer to an array
You can generate a pointer to the first element of an array by simply
specifying the array name, without any index.

3 Passing arrays to functions


You can pass to the function a pointer to an array by specifying the
array's name without an index.

4 Return array from functions


C++ allows a function to return an array.

23
Program 10. Find Transpose of a Matrix

24
Output:

25
Linked Lists
A linked list is a series of connected "node
odes" contains
th the "address" of the next
node. Each node can store a data point which may be a number, a string or any
other type of data.

Utility of Linked List:

Lists are one of the most popular and efficient data structures, with implementation
in every programming language like C, C++, Python, Java and C#.
Apart from that, linked lists are a great way to learn how pointers work. By
practicing how to manipulate linked lists, you can prepare yourself to learn more
advanced data structures like graphs and trees.

Types of Linked List -


There are three common types of Linked List.
1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List

Singly Linked List


It is the most common. Each node has data and a pointer to the next node.
Node is represented as:

1. struct node {
2. int data;
3. struct node *next;
4. }

26
Doubly Linked List

We add a pointer to the previous node in a doubly linked list. Thus, we can go in
either direction: forward or backward. A node is represented as

1. struct node {
2. int data;
3. struct node *next;
4. struct node *prev;
5. }

Circular Linked List

A circular linked list is a variation of linked list in which the last element is linked
to the first element. This forms a circular loop. A circular linked list can be either
singly linked or doubly linked.

 for singly linked list, next pointer of last item points to the first item

 In doubly linked list, prev pointer of first item points to last item as well.

27
Program 11. C++ Program To Implement Singly Linked List:

Inserting a node in linked list :

28
Deleting a node from Linked list:

Searching a node in Linked list:

Calling the function from Main function

29
Introduction to C

C is a high-level and general-purpose programming language that is ideal for


developing firmware or portable applications. Originally intended for writing
system software, C was developed at Bell Labs by Dennis Ritchie for the Unix
Operating System in the early 1970s. Ranked among the most widely used
languages, C has a compiler for most computer systems and has influenced many
popular languages – notably C++.

Structure of a C program:

The components of the above structure are:


1. Header Files Inclusion: A header file is a file with extension .h which
contains C function declarations and macro definitions to be shared between
several source files.

Syntax to include a header file in C:


#include <(header_file_name).h>

30
2. Main Method Declaration: The next part of a C program is to declare the
main() function. The syntax to declare the main function is:

Syntax to Declare main method:


int main()
{}
3. Variable Declaration: The next part of any C program is the variable
declaration. It refers to the variables that are to be used in the function. Please
note that in C program, no variable can be used without being declared. Also
in a C program, the variables are to be declared before any operation in the
function.

Example:
int main()
{
int a;

}
4. Body: Body of a function in C program, refers to the operations that are
performed in the functions. It can be anything like manipulations, searching,
sorting, printing, etc.
Example:
int main()
{
int a;

printf("%d", a);

5. Return Statement: The last part in any C program is the return statement.
The return statement refers to the returning of the values from a function. This
return statement and return value depend upon the return-type of the function.
For example, if the return type is void, then there will be no return statement.

31
In any other case, there will be a return statement and the return value will be
of the type of the specified return-type.
Example:
int main()
{
int a;

printf("%d", a);

return 0;
}

32
If-else Statements
If Statement

The if statement evaluates the test expre ssion insi de the parenthesis ().

 If the test expression is evaluated to true, statements inside the body of if are
executed.
 If the test expression is evaluated to false, statements inside the bodyof if
are not executed.

The syntax of the if statement in C programming is:


1. if (test expression)
2. {
3. // statements to be executed if the test expression is true
4. }

33
if...else Statement
If the test expression is evaluated to true:
 statements inside the body of if are executed.
 statements inside the body of else are skipped from execution.

If the test expression is evaluated to false,


 statements inside the body of else are executed
 statements inside the body of if are skipped from execution.

The if statement may have an optional else block. The syntax of


the if..else statement is:

1. if (test expression) {
2. // statements to be executed if the test expression is true
3. }
4. else {
5. // statements to be executed if the test expression is false
6. }

34
ITERATION STATEMENTS:
C for Loop:
Loops are used in programming to execute a block of code repeatedly until a
specified condition is met.
C programming has three types of loops:
1. for loop
2. while loop
3. do...while loop

For Loop
The syntax of the for loop is:

35
program 1: for loop

while loop :
The syntax of the while loop is:

program 3: while loop

36
do...while loop :
The do..while loop is similar to the while loop with one important difference. The
body of do...while loop is executed at least once. Only then, the test expression is
evaluated.
The syntax of the do...while loop is:
1. do
2. {
3. // statements inside the body of the loop
4. }
5. while (testExpression);

51`

Program 4: do...while loop

37
Functions
A function is a block of code that performs a specific task dividing
complex problem into small components making program easy to understand
Types of function

Depending on whether a function is defined by the user or already included in C


compilers, there are two types of functions in C programming

There are two types of function in C programming:

 Standard library functions


 User defined functions



38
Program in c for swaping two numbers:

output :

Arrays

An array is a collection of a fixed number of values of a single type. For example:


if you want to store 100 integers in sequence, you can create an array for it.

int data[100];

The size and type of arrays cannot be changed after its declaration.

Arrays are of two types:

1. One-dimensional arrays
39
2. Multidimensional arrays (will be discussed in next chapter)
How to declare arrays?

data_type array_name[array_size];

Program :Arrays:

Output:

40
Pointers

Pointers are powerful features of C and (C++) programming that differentiates it


from other popular programming languages like Java and Python. Pointers are used
in C program to access the memory and manipulate the address.

Pointer variables:

In C, you can create a special variable that stores the address (rather than the
value). This variable is called pointer variable or simply a pointer.

How to create a pointer variable?


data_type* pointer_variable_name;

int* p;

Program : To swap two number using call by reference(address):

41
Output

Enter value of num1: 10


Enter value of num2: 20
Before Swapping: num1=10, num2=20
After Swapping: num1=20, num2=10

42
Hospital management
system in C++
Hospital management system is based on the concept of recording the patient
details. This whole system is in the C++ language. There is no login system for this
project. In this project we have various specialists doctors, we can add both normal
patient as well as emergency case patient. We can also view the details off all
patients.
Some Key Features are :

1. Add normal patient

2. Add a critically ill patient

3. Take patient to the doctor

4. Display the list of all the patient

5. Various Specialists Doctors

43
Some Code snippets of the project are :

Input of patient detail :

Admitting the normal patient:

44
Admitting the Urgent patient :

Taking patient first in waiting list to the doctor :

45
Getting detail of all patients :

Intial landing layout code :

46
Layout code of each department :

Layout of first layout :

47
Layout of each Department :

48
CONCLUSION
I can honestly say that my time spent training with Hexnbit resulted in one of the
best summers of my life. I gain practical skills and learn the most in-demanded
technology which is used to create the products and software.
Overall, my internship at Hexnbit has been a success. I was able to gain
practical skills, work in a fantastic environment, and make connections that willlast
a lifetime. I could not be more thankful

49

You might also like