Mooc File
Mooc File
Mooc File
On
INTRODUCTION TO MODERN DATABASE
SYSTEM (CS403)
BY : MAYANK JOSHI
ROLL NO: 1961101
COURSE: B.TECH
BRANCH : CSE
I would like to give a special thanks to several people who made this all possible.
It is because of their tremendous support that I was able to finish this project with immense success .
I would like to thank each and everyone who helped me in the completion of this project, but firstly
and fore mostly I would like to pour my heart out in gratitude and humility towards our MOOC
coordinator Dr. Mehul Manu who ostensibly supported me in this project whole heartedly.
I consider myself very fortunate and gratified that I was given an opportunity and once again would
like to show my appreciation whole heartedly to everyone to make this possible.
MOOC BASED SEMINAR. THE COURSE OF THE MOOC REGISTRATION CS107 : C++
Signature : Signature :
INTRODUCTION TO C++
In the 1990s , C++ was one of the most popular programming languages, and now it is the third
most popular programming language, according to the Tiobe Rankings (November 2017).
C++ language was initially designed with a focus mainly on systems programming, it is an
attractive language if someone is creating end user applications because of its features like
resource constraints. C++ is extensively used in developing games, web clients / server side,
robotic and back office of financial applications.
Bjarne Stroustrup was the designer of C++ language and original implementer. C++ language
was originally named as C with Classes as it was derived or inherited from C language having a
new concept of classes.
This language was developed during 1979 in Bell Labs for the purpose of implementing the
Unix Operating System , by Dennis Ritchie . It gave great control to users over hardware at a
higher conceptual level than assembly language.
It is a standard recognized by American National Standard Institute (ANSI), British Standards
Institute (BSI), The German National Standards Bodies and other National Standard Bodies and
was also ratified by The International Standard Organization.
#include<iostream>
int main()
{
std::cout<<“Hello World”<<std::endl;
return 0;
}
HOW TO INSTALL ECLIPSE:
We need an IDLE/IDE software to run the program and to implement the code written in C++
language. For this we install the compiler in which interpret the code and convert it into binary
code and also make an executable file which can be easily understood by the computer . There
are different types of compiler which can be used to run the program , some of them are
TurboC++ , DevC++ ,Eclipse, GNU C++ Compiler(in case of MAC or Linux) , etc.
The common step to install the compiler is as follows:
Go to official websites of compiler.
Click on Download c++ compiler
An Idle software along with the compiler will be downloaded
Open the compiler and start programming
MY FIRST PROGRAM:
Steps to create our first C++ file:
1. Open Codeblocks .
2. Go to file > New > Empty File.
3. Write the following snippet and save the file as “MyFirstProgram.cpp”
4. For saving a file go to File>Save File as.
#include<iostream>
using namespace std;
int main()
{
cout<<”Hello World!”;
return 0;
}
Output of the above code
C++ Comments :
Comment are used to explain C++ code to make it more readable. These can also be used to
prevent execution when testing alternative codes. Comments are of two types in C++ :
1. Single line Comments
Single line comments are used to comment a single line.
It starts with double forward slash (//).
Any text between // and EOL(end of line) is ignored by the compiler and will not be
executed.
Example –
cout<<“Mayank Joshi”;// Single line comment
C++ Variables:
Variables are used to store data , like number , text etc.
According to the type of data , in C ++ , variables are of mainly 5 types :
int : it stores integers [whole numbers] , without the decimals , like 0, 100 , 18
double : it stores floating point numbers, with decimals , like 1.56 , 11.11
char : it stores single character , like ‘m’, ‘2’. Character values are surrounded by single
quotes
string : it stores text , like “Mayank Joshi”. String values are surrounded by double quotes.
bool – it stores the values with two states – true or false.
To insert a new line or to break lines we can use \n or endl
Example :
#include<iostream>
using namespace std;
int main()
{
cout<<“MAYANK JOSHI”<<endl;
cout<<“MOOC REPORT\n”;
cout<<“C++”;
return 0;
}
DECLARING/CREATING A VARIABLE:
To create a variable , we must specify the data type of variable and then assign a value to it.
Syntax : datatype variable_name = value
where datatype is the type of value to be assigned (like int) , variable_name is the name of
the variable which is also called identifier , = denotes assignment operator which assigns
value to variable_name and the value will be stored.
Example :
#include <iostream>
using namespace std;
int main()
{
int Num = 18; // Integer (whole number without decimals)
double Float = 18.5; // Floating point number (with decimals)
char ch = 'M'; // Character
string str = "Mayank"; // String (text)
bool Boolean = true; // Boolean (true or false)
return 0;
}
To display a variable we use cout object with << this operator. We can also combine a
variable and a text by separating them with << operator
For example : cout << “The value =“ << value;
We can declare more than one variable of the same type using comma separated list as
follows:
int a = 10 , b = 20 , c = 30;
cout<< a + b + c;
Reserved words (like C++ keywords, such as char) cannot be used as names
Name must begin with a letter or an underscore (_)
example : answer , _answer
Names are case sensitive (ans and aNs are different variables)
Name cannot contain whitespaces or special characters like !, #, %, etc.
Name can contain letters, digits and underscores
example : sec_D_student70
CONSTANTS:
When we do not want to alter the value of a variable, then we use const keyword , then the
variable will be treated as a constant and we can not change the value stored in the variable ,
and it will be read only variable.
Syntax :
const datatype variable_name = value;
Example :
Example :
#include<iostream>
using namespace std;
int main()
{
int I;
cout << “Enter a number : ” ;
cin >> I ;
cout<<“The entered value is ”b<< I ;
return 0;
}
In the above example we have asked user to input an integer number then we have printed the
number that was entered by the user.
When the user have entered the number the number is stored into the variable I and then we
have printed the value stored in the variable I .
SIMPLE CALCULATOR :
//The code to add two numbers and print the output as the sum of the two numbers.
#include<iostream>
using namespace std;
int main()
{
int x,y;
cout<<“Enter two numbers\n”;
cin>>x;
cin>>y;
cout<<“SUM = ”<<x+y;
return 0;
}
In the above code we have taken two numbers from the user and stored them in x and y
variables.
Then we have added those numbers using + operator .
At last we have given the sum as the output of the code.
BASIC DATA TYPES :
Data type specifies the size of the data to be stored and the type of the data to be stored.
There are mainly 5 datatypes in c++ :
C++ OPERATORS :
Operators are used to perform operations in C++ variables.
In C++ there are 5 types of operators:
• Arithmetic Operators
• Assignment Operators
• Logical Operators
• Comparison Operators and
• Bitwise Operators
Assignment Operators :
These are used to perform common mathematical operations .
ASSIGNMENT OPERATORS :
Assignment operators are used to assign values to the variables.
+= a += 10 a = a + 10
-= a -= 10 a = a – 10
*= a *= 10 a = a * 10
/= a /= 10 a = a / 10
%= a %= 10 a = a % 10
|= a |= 10 a = a | 10
^= a ^= 10 a = a ^ 10
Example:
#include<iostream>
using namespace std;
int main()
{
int a, b;
a=10;
b=20;
cout<<(a==b)<<endl;
cout<<(a!=b)<<endl;
cout<<(a>b)<<endl;
cout<<(a<b)<<endl;
return 0;
}
The Output of the code will be :
LOGICAL OPERATORS :
The logical operators are used to determine the logic between variables and values and also
to solve logical expressions.
Example:
string Name = “Mayank Joshi” ;
For using string we have to include string header file.
Snippet Example :
#include<iostream>
#include<string>
using namespace std;
int main()
{
string FirstName = “Mayank”, LastName = “Joshi” ;
cout<<FirstName<<“ ”<<LastName;
return 0;
}
In the above code we have stored the list of characters in the variable and then printed the
values stored in the variables.
STRING CONCATENATION :
We can append one string at the end of another string using + operator .
This is known as concatenation.
Snippet Example :
#include<iostream>
#include<string>
using namespace std;
int main()
{
string FirstName = “Mayank”, LastName = “Joshi” ;
cout << FirstName + “ ” + LastName;
return 0;
}
In the above example we have stored the string values in the string variables FirstName and
LastName and then add then using + operator . + operator combined or concatenated the
string and we printed concatenated string which is Mayank Joshi. Please note that we can
also add space or concatenate space at the end of the string.
So we get desired concatenated output by using + operator.
Append:
We can concatenate two strings using append. As string is an object in C++ so it also contains
function to append that is append() function.
Snippet Example :
#include<iostream>
#include<string>
using namespace std;
int main()
{
string FirstName = “Mayank ”, LastName = “Joshi” ;
cout << FirstName.append(LastName);
return 0;
}
we can use both + operator and append function , separately or simultaneously to perorm
concatenation or to add two strings together.
The difference between the two is that the append() function is much faster than the +
operator .
However for testing and in small programs + operator is generally preferred to append()
function.
We cannot add a number with string . This will produce an error in the program.
int a = 10;
string b = “20”;
int c = a + b;
The above code will give an error as 20 is treated as string and not as a number.
int a = 10;
string b = 20;
string c = a + b;
The above code will also produce an error as we cannot add or concatenate a string with a
number.
int a = 10;
int b = 20;
int c = a+b;
In this code the value 30 will be stored in c.
string a = “10”;
string b = “20”;
string c = a + b;
The value stored in string will be of type string and will be “1020” not “30” as the + operator
works to concatenate but not to add algebraically the two values stored in a and b.
Length of string:
To find out length of string we can use either length(), strlen(), or size() function.
size() function is an alias of length() function. We may use any of the 3 aforesaid method.
Example :
string name = “Mayank Joshi’;
name.length();
ASSESSING A STRING :
We can access any character of the string using their index values. The indices starts from 0
and goes to length -1.
So to access the first value we will write :
string name ;
cout << “Enter your name ”;
cin>>name;
cout <<name;
// Enter your name Mayank Joshi
// Mayank
This happened because cin stored only the characters before space and hence
Mayank is stored in the name variable.
So we will use getline function while dealing with the strings .
Example :
string name ;
cout << “Enter your name ”;
getline (cin, name);
cout <<name;
// Enter your name Mayank Joshi
// Mayank Joshi
We can omit namespace from our code but for this we have to print the variables as follows :
std::cout<<name;
MATHEMATICS IN C++ :
C++ offers many functions to perform mathematical operations on variables and numbers.
Example 2 :
int a=10,b=20;
cout<<min(a,b);
//it will print a
expm1(x) Returns ex -1
For the same purpose C++ has bool data type , that can take the values false (0) or true (1).
BOOLEAN VALUES :
A boolean variable can take only two values that are True or False and are declared using
bool keyword.
Example snippet :
The output of the above code fragment will be 10 as the bool variables returns 1 for true and
0 for false.
A Boolean Expression in C++ is an expression that returns boolean value : 1 for True and 0
for False.
C++ Conditions
Equal to a == b
Not Equal to : a != b
Less than : a < b
Less than or equal to : a <= b
Greater than : a > b
Greater than or equal to : a >= b
we can use the above conditions to perform different actions for different tasks.
SYNTAX :
if (test condition)
{
//block of code to execute when the test condition is true.
}
Example :
if(3>2)
{
cout<< “three is greater than two”;
}
ELSE STATEMENT :
The else statement is used to execute the code fragment if the given test condition is false.
Example :
int a = 10
if(x==10)
{
cout<< “X = 10”;
}
else
{
cout<< “X != 10”;
}
Example :
#include<iostream>
using namespace std;
int main()
{
int x=-1;
if (x>0)
{
cout<<“POSITIVE”;
}
else
{
cout<<“NEGATIVE”;
}
return 0;
}
The above code will give :
The above code can be written as :
#include<iostream>
using namespace std;
int main ()
{
int i=-1;
i > 0 ? cout << “Positive” : cout << “Negative” ;
return 0;
}
SWITCH STATEMENT :
The switch statement is to select one of the many code blocks .
Syntax :
switch(expression)
{
case a : //code fragment
break;
case b : //code fragment
break;
default : //code fragment
}
It works as follows :
#include <iostream>
using namespace std;
int main()
{
int day = 7;
switch (day)
{
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
The output of the above code is
While Loop :
Syntax :
While(condition)
{
//code block to be executed
}
Example :
int i=0;
while(i<5)
{
cout<<“i”;
i++;
}
// The output will be 01234
DO WHILE LOOP:
Syntax :
do
{
//code to be executed
}while(test condition);
Example :
do
{
cout<<i;
i++;
}while(i<5);
01234
Please note that the body is executed first and then the test condition
FOR LOOP:
When we know how many times we have to loop , then we prefer for loop to while loop
Syntax :
Example :
CONTINUE STATEMENT:
The continue statement skips the code fragment following the continue statement for only
one iteration of the loop and then continues with the next iteration in the loop.
Example :
ARRAYS :
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
To declare an array, define the variable type, specify the name of the array followed
by square brackets [] and specify the number of elements it should store:
Example :
int arr[10];
string names[4] = {“Mayank”, ”Joshi”, “Manya” , “Dhyani”};
REFERENCES :
A reference variable is a "reference" to an existing variable, and it is created with
the & operator:
Example :
string name=“Mayank Joshi”;
string &me = name;
cout<<me; // it will produce Mayank Joshi as output.
POINTERS :
The pointers are the variables that stores the memory address of other variables.
syntax
datatype * nameofpointer = &nameofvarable;
Example :
int i = 10
int *I = & i;
FUNCTIONS :
Functions are blocks of code which only run when they are called.
We can pass data into functions which are called as parameters.
Functions are used to simplify the program by writing long part once and then using it
multiple times by calling the function containing long code fragments.
C++ have predefined functions and allows users to make their own functions as per the use
called as user defined functions:
Syntax :
datatype function_name( arguments )
{
// code fragment
}
The functions can be called by passing reference values called as call by reference method.
C++ also have concept of Function Overloading.
In functions the parameters can be assigned default values as per the requirements.
The parameter are the variables that are present in the variable definition of the program,
where as the arguments are the values passed to the function
OOPs CONCEPT :
OOP stands for Object Oriented Programming.
In procedural programming we write only procedures or functions that perform operations on
data.
Advantages of OOP over PP :
OOP provides a clear structure for programs.
OOP is faster and easier to execute.
Reusability of code.
OOP help to keep the c++ code DRY , that is Don’t Repeat Yourself.
It is easier to modify or debug the program.
We can define methods or functions inside the class as well as outside the class .
class Numbers
{
int n1,n2,n3,n4, add , avg;
void sum()
{
sum= n1+ n2 + n3 + n4;
}
void average();
};
void Numbers :: average()
{
avg = sum/4;
}
We can also add parameters to the class functions.
Constructor is special method that is automatically called when a new object is formed.
To create constructor we have to use the same name as the class name followed by
parenthesis.
It is always public and does not have any return value.
Constructors may have parameters and default parameters too as in case of functions.\
The Constructor can also be defined outside the class..
In C++ , there are three access specifiers to control what we want to show to the outer world
and what we don’t :
1. public : The members can be accessed from outside the class.
2. private : The members can not be accessed from outside the class.
3. protected : The members cannot be accessed from outside the class but they can be
accessed through inherited class .
Encapsulation :
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To
achieve this, you must declare class variables/attributes as private (cannot be accessed from
outside the class). If you want others to read or modify the value of a private member, you
can provide public get and set methods.
Encapsulation ensures better control of data and also increases security.
Example :
class Student
{
private:
int rno;
public:
void enter(int crn)
{ rno = crn;
}
int ret()
{ return rno;
}
};
Now the data can be retrieved or modify by indirect access and our data is safe and
encapsulated from the outside world.
INHERITANCE :
In C++ we can inherit one or more class from another or parent class. Attributes and Methods
both can be inherited from parent class to sub class.
Derived class is the class that is derived from the another class
Base class is the class from which derived class is inherited.
Example :
class base : public inherited{
}
There are different types of inheritance
MULTILEVEL INHERITANCE
A class can be derived from a class that is also derived from another class . This type of
inheritance is called Multilevel inheritance.
MULTIPLE INHERITANCE
A class can be derived from more than one class is called Multiple inheritance.
Example:
class derived : public baseclass1 , public baseclass2{
}
Polymorphism
Polymorphism means "many forms", it occurs whenawe have many classes that are related to
each other by inheritance.
Inheritance lets us inheritdattributes and methodsifrom another class. Polymorphism uses
those methods to performjdifferent tasks. This allows us to perform absingle action in
different ways.
For example, think of a basedclass called Animal that has a method called animalSound().
Derived classes ofkAnimals could be Pigs, Cats,fDogs, Birds - And they alsophave their own
implementation of an animal sound (the pigdoinks, and the cat meows, etc.):