Chapter-1: Introduction to c++
C++:
It is an extension to C programming.
It is a Middle level programming language.
It is a case sensitive and Intermediate language.
It supports both object oriented, procedural programming.
Basic Syntax:
Output:
i. <iostream>: It is a header file which provides input&output streams.
ii. Using namespace std: It tells the compiler to use standard namespaces.
It collects identifiers used for class, object and variables.
iii. Main(): It is the function which holds the executing part of a program.Its return type is int.
iv. Cout:
It is used to display output to the standard output devices i.e monitor.
It is associated with the standard c output stream.
Declaration:
cout<<variable_name;
To display combination of strings and values
cout<<"String"<<variable_name1<<variable_name1;
v. Cin:
It is used to accept the input from the user from the standard input devices.
It is used with extraction operator(>>) in order to receive input.
Declaration:
cin>>variable_name;
To accept multiple inputs
cin>>variable_name1>>variable_name2>>....>>variableN;
Chapter-2: Variables and Data types in c++
Variables:
A variable is nothing but a field which is used to store values.
Syntax: data_type variable_name=value;
ex: int number=10;
variables are two types.
i. Global variable
ii. Local variable
1. Global variable:
A variable which is declared outside of any function(including main) is called global variable.
It's scope is within the entire program.
They can be accessed anywhere in the program i.e in the main, in the user defined function.
2. Local variable
Local variables are declared inside the braces of any user defined, main function, loops or any control statements.
It's scope is limited inside the braces.
Ex: For Local and Global Variables
Output:
Datatypes:
Datatypes define the type of data a variable can hold.
Datatypes in c++ are categorized in three types.
i.Primitive Datatypes
ii.User defined Datatypes
iii.Derived Datatypes
1.Primitive Datatypes:
These datatypes are predefined datatypes and can be used directly by the user to declare variables.
i.Char: It is used for storing character values.It requires 1 byte of memory..
ii.Int: it is used to store numerical data. it requires 2 bytes of memory.
iii.Boolean: It can store logical value i.e either true or false.
iv.Float: It is used to store single decimal values.It requires 4 byte of memory.
v.Double: It is used to store double decimal values.It requires 8 byte of memory.
vi.void: It means without any value.it is used to represent a valueless entity.
Datatype Modifiers:
These are used for primitive datatypes to modify the length of data that a particular data type can hold.
These are classified into 4 types
Signed.
UnSigned.
Short.
Long.
Ex: Displaying the size of datatypes
Output:
2.User defined Datatypes:
We have three types of user defined datatypes
i. Struct
ii. enum
iii. Union
i. Structure:
It is a user defined data type.
Structure is a compound data type that contains different variables of different types.
It defines a new data type with more than one member.
Syntax: Struct [structure tag]
{
member definition;
member definition;
member definition;
member definition;
}
Ex:
struct Student
{
int id;
string name;
double phonenumber;
};
Ex:
Output:
ii. Enumeration:
Ex: enum season{summer,winter,spring}select;
Ex:
Output:
iii. Union
Syntax: Union union_name
{
datatype variable1;
datatype variable2;
};
Ex-1:
Output:
This is because the union shares a common memory for all its variables.
We need to access a single variable at a time.
Ex-2:
Output:
3.Derived Datatypes
Chapter-3: Operators in c++
Operators:
1.Arithmetic operators.
+,-,*,/,%
2. Assignment operators.
=,+=,-=,*=,/=,%=
3. Auto_increment/decrement operators.
++,--
ex: a++, ++a, a--, --a
4. Logical operator:
&&,||,!
5. Relational operator.
==,!=,>,<.<=,>=
6. Bitwise operator:
&,|,^,~,<<,>>
7. Ternary operator:
Syntax: variable num_1=(experssion)? value if true: value if false.
Ex:
Output:
Task-1:
Output:
Task-2:
Output:
Task-3:
Output:
Chapter-4: Conditional statements in c++
Conditional Statements:
If we want to execute a block of code only when the specified condition is true then we can use conditional statements.
1.If Statement
Syntax: if(condition)
{
statements;
}
2.If else Statement
Syntax: if(condition)
{
statements;
}
else
{
statements;
}
3.If else if Statement
Syntax: if(condition)
{
statements;
}
else if
{
statements;
}
else
{
statements;
}
4.Switch:
Syntax: Switch(variable or integer expression)
{
case constant:
statements;
case constant:
statements;
default:
statements;
}
Task-1:
Output-1:
Output-2:
Output-3:
Chapter-5:Looping statements in c++
Loops:
A loop is used for executing a block of statements repeatedly until a particular condition is satisfied.
1. For loop:
Syntax: for(initialization; condition; increment/decrement)
{
Statements;
}
2. While loop:
Syntax: while( condition)
{
Statements;
increment/decrement;
}
3. do-while loop:
Syntax: do
{
Statements;
increment/decrement;
}
while( condition)
Task-1: Multiplication table using for loop
Output:
Task-2: Multiplication table using while loop
Syntax: return-type function_name(parameter list)
Declaration
Syntax: return-type function_name(parameter list)
{
statements
}
Function Calling: function_name(parameters);
Types of Functions:
Functions are two types
i. Built-in functions
ii. User-defined functions
1. Built-in functions:
These are also known as library functions.
We no need to declare these functions they are already written in the c++ libraries.
Ex: iostream, cmath etc
2. User-defined functions
It is nothing but a group of code which is used to perform a particular task.
It allows the programmer to define their own functions.
Task-1:
Output:
Task-2:
Output:
Math Functions:
i. Ceil
ii. Floor
iii.Log
iv. Sqrt
v. Pow
vi. Abs
Ex:
Output:
Chapter-7: Introduction to arrays
Array:
Array is a collection of similar elements.
It's index start with zero.
These are used to store similar type of data.
Array are two types.
i. Single array
ii. Multidimensional array.
1. Single array:
Declaration:
1. return_type array_name[size] = {Value_1,Value_2.....Value_N};
2. return_type array_name[] = {Value_1,Value_2.....Value_N};
Ex: int Numbers[5]={10,20,30,40,50};
2. Multidimensional array:
i.Two Dimensional array:
This is also known as array of arrays.
Declaration:
1. return_type array_name[size][size];
Ex: int Numbers[2][3]={10,20,30,40,50,60};
ii.Three Dimensional array:
Declaration:
1. return_type array_name[size][size];
Ex: int Numbers[2][3]={10,20,30,40,50,60};
Task-1:
Output:
Chapter-8: Introduction to files
Files:
Files are used to store the data.
To create files we have to use another library called fstream.
fstream: is also like iostream which is used to read and write on files.
ofstream: It is used to create files and write on files.
ifstream: It is used to read from files.
fstream: It acts like both ofstream and ifstream which means it can createm,and write on files and reaed from files.
There are different file modes which are used while working with files.
ios::in: Opens a file for reading.
ios::out: Opens a file for writing.
ios::ate: Opens a file for output and move the read/write control to the end of the file.
ios::turnc: Truncates the content before opening a file, if file exists.
ios::in: Opens a text file for appending.
Task-1:
Output:
Task-2:
Output:
Chapter-9: Tasks
Task-1: Write a program to Reverse a string
Output:
Task-2: Find the highest number among four numbers
Output:
Task-3:
Output:
Task-4:Finding Maximum occurring character in a given string.
Output:
Chapter-10: Introduction OOPs concepts in c++
Object Oriented Programming Concepts
C++ is a object oriented programming language.
object oriented programming is a programming style which is associated with concepts of class,objects,Abstraction,Encapsulation,Inheritance,polymorphism.
Class:
Class is a template or a blue print in which objects are created.
Class contains fields, methods, constructors.
Object:
Object is an instance of a class. All the members of the class can be accessed through the object.
Object is an entity that has states and behaviors.
state means data and behavior means functionality.
Ex: Creating a class and accessing through object.
Output:
C++:
It is an extension to C programming.
It is a Middle level programming language.
It is a case sensitive and Intermediate language.
It supports both object oriented, procedural programming.
Basic Syntax:
Output:
i. <iostream>: It is a header file which provides input&output streams.
ii. Using namespace std: It tells the compiler to use standard namespaces.
It collects identifiers used for class, object and variables.
iii. Main(): It is the function which holds the executing part of a program.Its return type is int.
iv. Cout:
It is used to display output to the standard output devices i.e monitor.
It is associated with the standard c output stream.
Declaration:
cout<<variable_name;
To display combination of strings and values
cout<<"String"<<variable_name1<<variable_name1;
v. Cin:
It is used to accept the input from the user from the standard input devices.
It is used with extraction operator(>>) in order to receive input.
Declaration:
cin>>variable_name;
To accept multiple inputs
cin>>variable_name1>>variable_name2>>....>>variableN;
Chapter-2: Variables and Data types in c++
Variables:
A variable is nothing but a field which is used to store values.
Syntax: data_type variable_name=value;
ex: int number=10;
variables are two types.
i. Global variable
ii. Local variable
1. Global variable:
A variable which is declared outside of any function(including main) is called global variable.
It's scope is within the entire program.
They can be accessed anywhere in the program i.e in the main, in the user defined function.
2. Local variable
Local variables are declared inside the braces of any user defined, main function, loops or any control statements.
It's scope is limited inside the braces.
Ex: For Local and Global Variables
Output:
Datatypes:
Datatypes define the type of data a variable can hold.
Datatypes in c++ are categorized in three types.
i.Primitive Datatypes
ii.User defined Datatypes
iii.Derived Datatypes
1.Primitive Datatypes:
These datatypes are predefined datatypes and can be used directly by the user to declare variables.
i.Char: It is used for storing character values.It requires 1 byte of memory..
ii.Int: it is used to store numerical data. it requires 2 bytes of memory.
iii.Boolean: It can store logical value i.e either true or false.
iv.Float: It is used to store single decimal values.It requires 4 byte of memory.
v.Double: It is used to store double decimal values.It requires 8 byte of memory.
vi.void: It means without any value.it is used to represent a valueless entity.
Datatype Modifiers:
These are used for primitive datatypes to modify the length of data that a particular data type can hold.
These are classified into 4 types
Signed.
UnSigned.
Short.
Long.
Ex: Displaying the size of datatypes
Output:
2.User defined Datatypes:
We have three types of user defined datatypes
i. Struct
ii. enum
iii. Union
i. Structure:
It is a user defined data type.
Structure is a compound data type that contains different variables of different types.
It defines a new data type with more than one member.
Syntax: Struct [structure tag]
{
member definition;
member definition;
member definition;
member definition;
}
Ex:
struct Student
{
int id;
string name;
double phonenumber;
};
Ex:
Output:
ii. Enumeration:
- Enum is a user defined data type.
- It contains a set of values for a variable and the variable can only take one out of set of possible values.
- enum keyword is used to define enumeration.
- By default enum elements are starts with zero. we can change the values at the time of declaration.
Ex: enum season{summer,winter,spring}select;
Ex:
Output:
iii. Union
- Union is a collection different datatypes.
- It is used to group number of variables of different data types in a single unit.
- It allocates common memory for all its members.
- The memory allocated by union is large enough to hold the largest member of the union.
- The main purpose of having union is to use one variable at a time.
Syntax: Union union_name
{
datatype variable1;
datatype variable2;
};
Ex-1:
Output:
This is because the union shares a common memory for all its variables.
We need to access a single variable at a time.
Ex-2:
Output:
3.Derived Datatypes
i. Array
ii. Function
iii. Pointer
Task-1: Declare string variable and display your Name
Output:
Task-2: Declare two variables and assign your first name and last name and display full name.
Output:
Chapter-3: Operators in c++
Operators:
operators represent an action.
An operator works on two or more operands and produce an output.
Operators are classified into different types.
1.Arithmetic operators.
+,-,*,/,%
2. Assignment operators.
=,+=,-=,*=,/=,%=
3. Auto_increment/decrement operators.
++,--
ex: a++, ++a, a--, --a
4. Logical operator:
&&,||,!
5. Relational operator.
==,!=,>,<.<=,>=
6. Bitwise operator:
&,|,^,~,<<,>>
7. Ternary operator:
Syntax: variable num_1=(experssion)? value if true: value if false.
Ex:
Output:
Task-1:
Output:
Task-2:
Output:
Task-3:
Output:
Chapter-4: Conditional statements in c++
Conditional Statements:
If we want to execute a block of code only when the specified condition is true then we can use conditional statements.
1.If Statement
Syntax: if(condition)
{
statements;
}
2.If else Statement
Syntax: if(condition)
{
statements;
}
else
{
statements;
}
3.If else if Statement
Syntax: if(condition)
{
statements;
}
else if
{
statements;
}
else
{
statements;
}
4.Switch:
Syntax: Switch(variable or integer expression)
{
case constant:
statements;
case constant:
statements;
default:
statements;
}
Task-1:
Output-1:
Output-2:
Output-3:
Chapter-5:Looping statements in c++
Loops:
A loop is used for executing a block of statements repeatedly until a particular condition is satisfied.
1. For loop:
Syntax: for(initialization; condition; increment/decrement)
{
Statements;
}
2. While loop:
Syntax: while( condition)
{
Statements;
increment/decrement;
}
3. do-while loop:
Syntax: do
{
Statements;
increment/decrement;
}
while( condition)
Task-1: Multiplication table using for loop
Output:
Task-2: Multiplication table using while loop
Output:
Chapter-6: Introduction to functions
Functions:
A function is a block of code which is used to perform a particular task.Functions:
Syntax: return-type function_name(parameter list)
Declaration
Syntax: return-type function_name(parameter list)
{
statements
}
Function Calling: function_name(parameters);
Types of Functions:
Functions are two types
i. Built-in functions
ii. User-defined functions
1. Built-in functions:
These are also known as library functions.
We no need to declare these functions they are already written in the c++ libraries.
Ex: iostream, cmath etc
2. User-defined functions
It is nothing but a group of code which is used to perform a particular task.
It allows the programmer to define their own functions.
Task-1:
Output:
Task-2:
Output:
String Expressions:
i. Strlen(): strlen is used to returns the length of the string.
ii. Strcat(): strcat is used to combine the two strings.
iii. Strcpy(): strcpy is used to copy the last string into the first string.
iv. Strcmp(): strcmp is used to compare two strings.
Ex:
Output:
Math Functions:
i. Ceil
ii. Floor
iii.Log
iv. Sqrt
v. Pow
vi. Abs
Ex:
Output:
Chapter-7: Introduction to arrays
Array:
Array is a collection of similar elements.
It's index start with zero.
These are used to store similar type of data.
Array are two types.
i. Single array
ii. Multidimensional array.
1. Single array:
Declaration:
1. return_type array_name[size] = {Value_1,Value_2.....Value_N};
2. return_type array_name[] = {Value_1,Value_2.....Value_N};
Ex: int Numbers[5]={10,20,30,40,50};
2. Multidimensional array:
i.Two Dimensional array:
This is also known as array of arrays.
Declaration:
1. return_type array_name[size][size];
Ex: int Numbers[2][3]={10,20,30,40,50,60};
ii.Three Dimensional array:
Declaration:
1. return_type array_name[size][size];
Ex: int Numbers[2][3]={10,20,30,40,50,60};
Task-1:
Output:
Chapter-8: Introduction to files
Files:
Files are used to store the data.
To create files we have to use another library called fstream.
fstream: is also like iostream which is used to read and write on files.
ofstream: It is used to create files and write on files.
ifstream: It is used to read from files.
fstream: It acts like both ofstream and ifstream which means it can createm,and write on files and reaed from files.
There are different file modes which are used while working with files.
ios::in: Opens a file for reading.
ios::out: Opens a file for writing.
ios::ate: Opens a file for output and move the read/write control to the end of the file.
ios::turnc: Truncates the content before opening a file, if file exists.
ios::in: Opens a text file for appending.
Task-1:
Output:
Task-2:
Output:
Chapter-9: Tasks
Task-1: Write a program to Reverse a string
Output:
Task-2: Find the highest number among four numbers
Output:
Task-3:
Output:
Task-4:Finding Maximum occurring character in a given string.
Output:
Chapter-10: Introduction OOPs concepts in c++
Object Oriented Programming Concepts
C++ is a object oriented programming language.
object oriented programming is a programming style which is associated with concepts of class,objects,Abstraction,Encapsulation,Inheritance,polymorphism.
Class:
Class is a template or a blue print in which objects are created.
Class contains fields, methods, constructors.
Object:
Object is an instance of a class. All the members of the class can be accessed through the object.
Object is an entity that has states and behaviors.
state means data and behavior means functionality.
Ex: Creating a class and accessing through object.
Output:
Ex: Creating a class and accessing through object. by using methods
Output:
Member Functions:
Output:
Member Functions:
- A Member function of a class is a function that has its definition or its prototype with in the class definition.
- It operates on any object of the class in which it is a member.
- It can be defined with in the class definition or separately using scope resolution operator(:).
Ex:
Output:
Access Specifiers:
Data hiding is one of the important features of object oriented programming.
The access restrictions to the class members is specified bu the public, private, protected sections with in the class.
The default access for members and classes is private.
1. Public:
A public member is accessible from anywhere outside the class but with in a program.
We can set and get the public variable values with out using data members.
2.Private:
A Private member variable or function can not be accessed, or even viewed from outside of the class.
Only class and friend functions can access private members.
By default all the members of the class is private.
3.Protected:
It is similar to a private member. but we can access protected members in derived classes.
Ex:
Output:
Constructors:
- It is a special method which is invoked automatically at the time of object creation.
- It does not have any return type.
- It has the same name as class name.
There are two types of constructors
1. Default constructor:
A default constructor doesn't have any arguments(or parameters).
When we don't specify any constructor in the class then compiler will insert a default constructor in the code.
2. Parameterized constructor:
A constructor with parameters is known as parameterized constructor.
We can pass arguments in parameterized constructors at the time of object creation.
Ex:
Output:
Destructors:
- It is just opposite to constructors.
- constructors are used to initialize objects.
- Where as destructors are used to delete or destroy the objects.
- It has the same name as class name but with a prefix of tidle(~).
- A destrructor automatically called when the program is finished execution or when the local variable scope ends or when we call the delete operator.
Inheritance:
A class which is derived from another class is known as inheritance.
The child class can acquire all the properties(Data members) and functionalities(member functions) from base class.
Syntax:
class parent_class
{
// body of parent class
};
class child_class : access_modifier parent_class
{
// body of child class
}
Types of Inheritance
Inheritance is classified into 5 types.
1. Single Inheritance:
In single inheritance only class is derived from base class.
2. Multilevel Inheritance:
In this one class is derived from one base class and another class is derived from that derived class.
Ex: C is inherited from B and B is inherited from A
3. Multiple Inheritance:
In this one class can be derived from more than one base class.
Ex: C inherits from both A and B.
4. Hierarchical Inheritance:
In this one parent class can have more than one child class.
Ex: Class B and C inherits form class A.
5.Hybrid Inheritance:
It is the combination of more than one type of inheritance.
Polymorphism:
It means many forms.
It allows objects to behave differently in different conditions.
Polymorphism is of two types
1. Compile time Polymorphism:
The overloaded functions are invoked by matching the type and number of arguments.
The compiler selects the appropriate functions at the compile time.
This is achieved by function overloading and operator overloading which is also known as early binding or static binding.
2. Run time Polymorphism:
Run time polymorphism is achieved when the objects methods is invoked at run time.
It is achieved method overriding which is also known as dynamic binding.
Overloading:
If we create two or more members having the same name but different in number or type of parameter.
It is possible with methods, constructors, indexed properties.
It is of two types
1. Function overloading:
It means two or more functions have same name but with different in parameters or different type of parameters is known as function overloading.
It increase the readability of the program.
2. Operator overloading:
It is used to overload or redefines most of the operators available in c++.
It is used to perform the operations on user defined operations.
Existing operators can only be overloaded, but the new operators can not overloaded.
Ex:
Output:
Function overriding:
If derived class contains the same functions as in the base class is known as function overriding.
It is used to provide run time polymorphism.
In this the function in parent class is called overridden function and function in child class is called overriding class.
Ex:
Output:
Abstraction:
It is the process of hiding unwanted data to the user.
Encapsulation:
A class which is derived from another class is known as inheritance.
The child class can acquire all the properties(Data members) and functionalities(member functions) from base class.
Syntax:
class parent_class
{
// body of parent class
};
class child_class : access_modifier parent_class
{
// body of child class
}
Types of Inheritance
Inheritance is classified into 5 types.
1. Single Inheritance:
In single inheritance only class is derived from base class.
2. Multilevel Inheritance:
In this one class is derived from one base class and another class is derived from that derived class.
Ex: C is inherited from B and B is inherited from A
3. Multiple Inheritance:
In this one class can be derived from more than one base class.
Ex: C inherits from both A and B.
4. Hierarchical Inheritance:
In this one parent class can have more than one child class.
Ex: Class B and C inherits form class A.
5.Hybrid Inheritance:
It is the combination of more than one type of inheritance.
Polymorphism:
It means many forms.
It allows objects to behave differently in different conditions.
Polymorphism is of two types
1. Compile time Polymorphism:
The overloaded functions are invoked by matching the type and number of arguments.
The compiler selects the appropriate functions at the compile time.
This is achieved by function overloading and operator overloading which is also known as early binding or static binding.
2. Run time Polymorphism:
Run time polymorphism is achieved when the objects methods is invoked at run time.
It is achieved method overriding which is also known as dynamic binding.
Overloading:
If we create two or more members having the same name but different in number or type of parameter.
It is possible with methods, constructors, indexed properties.
It is of two types
1. Function overloading:
It means two or more functions have same name but with different in parameters or different type of parameters is known as function overloading.
It increase the readability of the program.
2. Operator overloading:
It is used to overload or redefines most of the operators available in c++.
It is used to perform the operations on user defined operations.
Existing operators can only be overloaded, but the new operators can not overloaded.
Ex:
Output:
Function overriding:
If derived class contains the same functions as in the base class is known as function overriding.
It is used to provide run time polymorphism.
In this the function in parent class is called overridden function and function in child class is called overriding class.
Ex:
Output:
Abstraction:
It is the process of hiding unwanted data to the user.
Encapsulation:
- It binds data and functions together to manipulate the data.
- This is to prevent the access directly, the access to them is provided through the functions of the class.
- It is a mechanism of bundling the data, and the fictions that use them.
- Data abstraction is a mechanism of exposing only the interface and hiding the implementation details from user.
Ex:
Output: