Anmol Sip-1
Anmol Sip-1
ON
C++
Submitted in partial fulfilment of the requirements for the Degree Of
Bachelor of Technology
Uttaranchal Institute of Technology
Primarily I would like to thank Dr. S.D Pandey, Dean UIT for providing a healthy and encouraging
environment to study.
I profusely thankful to the department of computing Science, and Dr. Sumit Chaudhary, HOD. I am
also thankful to my mentor Mr.Ashok Kumar sir for guiding me throughout the internship period.
He has been generous enough to provide me an opportunity and accepting my candidature for the most
valuable guidance and affordable treatment given to us at every stage to boost my morale.
Date:
(Signature)
Lalit Kumar
This is to certify that the internship entitled “C++” by Lalit kumar has been submitted in partial fulfilment
of the requirements for the award of the degree of B.TECH CSE from Uttaranchal University, Dehradun.
The results embodied in this project have not been submitted to any other University or Institution for the
record of any degree.
List of Tables
2.1 Rational Operators
2.2 Operators
1. Relational Operator
2. Arithmetic Operator
3.Logical Operator
Flow Of Control
2.1 C++ Loop Types
There may be a situation, when you need to execute a block of code several number
of times. In general, statements are executed sequentially: The first statement in a
function is executed first, followed by the second, and so on. 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 han- dle
looping requirements.
The keyword public determines the access attributes of the members of the class
that follows it. A public member can be accessed from outside the class anywhere
within the scope of the class object. You can also specify the members of a class as
private or protected which we will discuss in a sub-section.
Define C++ Objects
A class provides the blueprints for objects, so basically an object is created from a
class. We declare objects of a class with exactly the same sort of declaration that we
declare variables of basic types. Following statements declare two objects of class
Box
Both of the objects Box1 and Box2 will have their own copy of data
members.
6 .Array
an array is a data structure that is used to store multiple values of similar data types in a
contiguous memory location.
For example, if we have to store the marks of 4 or 5 students then we can easily store
them by creating 5 different variables but what if we want to store marks of 100 students
or say 500 students then it becomes very challenging to create that numbers of variable
and manage them. Now, arrays come into the picture that can do it easily by just creating
an array of the required size.
• An Array is a collection of data of the same data type, stored at a contiguous memory location.
• Indexing of an array starts from 0. It means the first element is stored at the 0th index, the second at 1st,
and so on.
• Elements of an array can be accessed using their indices.
• Once an array is declared its size remains constant throughout the program.
• An array can have multiple dimensions.
• The number of elements in an array can be determined using the sizeof operator.
• We can find the size of the type of elements stored in an array by subtracting adjacent addresses.
Array Declaration in C++
In C++, we can declare an array by simply specifying the data type first and then the name of an array with
its size.
data_type array_name[Size_of_array];
Example
int arr[5];
In C++, we can initialize an array in many ways but we will discuss some most common ways to initialize
an array. We can initialize an array at the time of declaration or after declaration.
1. Initialize Array with Values in C++
We have initialized the array with values. The values enclosed in curly braces ‘{}’ are assigned to the array.
Here, 1 is stored in arr[0], 2 in arr[1], and so on. Here the size of the array is 5.
int arr[5] = {1, 2, 3, 4, 5};
2. Initialize Array with Values and without Size in C++
We have initialized the array with values but we have not declared the length of the array, therefore, the
length of an array is equal to the number of elements inside curly braces.
int arr[] = {1, 2, 3, 4, 5};
3. Initialize Array after Declaration (Using Loops)
We have initialized the array using a loop after declaring the array. This method is generally used when we
want to take input from the user or we cant to assign elements one by one to each index of the array. We
can modify the loop conditions or change the initialization values according to requirements.
for (int i = 0; i < N; i++) {
arr[i] = value;
}
4. Initialize an array partially in C++
Here, we have declared an array ‘partialArray’ with size ‘5’ and with values ‘1’ and ‘2’ only. So, these
values are stored at the first two indices, and at the rest of the indices ‘0’ is stored.
int partialArray[5] = {1, 2};
5. Initialize the array with zero in C++
We can initialize the array with all elements as ‘0’ by specifying ‘0’ inside the curly braces. This will
happen in case of zero only if we try to initialize the array with a different value say ‘2’ using this method
then ‘2’ is stored at the 0th index only.
int zero_array[5] = {0};
Accessing an Element of an Array in C++
Elements of an array can be accessed by specifying the name of the array, then the index of the element
enclosed in the array subscript operator []. For example, arr[i].
Example 1: The C++ Program to Illustrate How to Access Array Elements
int main()
{
int arr[3];
return 0;
}
Output
arr[0]: 10
arr[1]: 20
arr[2]: 30
7. Link list
The Linked Lists are linear data structures where the data is not stored at contiguous memory locations so we
can only access the elements of the linked list in a sequential manner. Linked Lists are used to overcome the
shortcoming of arrays in operations such as deletion, insertion, etc.
Types of Linked List
1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List
4. Circular Doubly Linked List
5. Header Linked List
Project Description
The C++ Book Management System project allows users to manage a library of books. Users can add books to the
library, display the list of books, and exit the system. The project incorporates classes, functions, and arrays to create a
simple yet functional book management system.
#include <iostream>
#include <string>
// Define the Book class
class Book {
public:
std::string title;
std::string author;
int publicationYear;
// Constructor
Book(std::string t, std::string a, int year) : title(t), author(a), publicationYear(year) {}
// Function to display book details
void displayDetails() {
std::cout << "Title: " << title << "\nAuthor: " << author << "\nYear of Publication: " << publicationYear <<
"\n\n";
}
};
int main() {
const int maxBooks = 10;
Book library[maxBooks]; // Array to store books
int bookCount = 0;
while (true) {
displayMenu();
int choice;
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1: // Add a Book
if (bookCount < maxBooks) {
std::string title, author;
int year;
case 3: // Exit
std::cout << "Exiting the Book Management System. Goodbye!\n";
return 0;
default:
std::cout << "Invalid choice. Please enter a valid option.\n\n";
}
}
return 0;
}
Sample Output
References
[1] https://www.tutorialspoint.com/cplusplus/index.htm
[2] www.w3schools.com
[3] The C++ Programming Language by Bjarne Structure