Posts

References in C++

               If you want to create an alternate name for a variable, then this is option you have. You can access the variable as reference. How to create a Reference?               It uses ‘&’ symbol to create the reference. Syntax: ‘data_type &ref_name= variable;’ Eg: ‘int &ref = a;’ Let us create a C++ program to use the reference. This program starts by including built-in header file <iostream>.   ‘main()’ function creates two variable ‘a’ and ‘reference’. Get the ‘a’ value from the user. Assign it to reference. Get the value two more time and assign it to reference. Print the value. Program: #include <iostream> using namespace std; int main() { int a; cout<<"Enter the value:"<<endl; cin>>a; //create the reference value and assign the variable int &reference = a; //let us change the...

Pointers in c++

              A variable which holds the address of a memory location is called a pointer. It deals with memory. To create a pointer variable, follow the syntax. ‘datatype *variable_name;’ Eg: ‘int *ptr;’   C++ Program: This program includes the built-in header file <iostream>. Next main() is defined. It has following steps: A variable a is declared as integer. It reads the user input from command prompt. A pointer variable *ptr is declared. #include <iostream> using namespace std; int main() {     int a;//Declaration of integer variable     int *ptr; // Declarartion of pointer variable     // getting the input from user     cout<<"Enter the variable:"<<endl;     cin>>a;     // storing address of a in pointer myptr     ptr = &a;     cout << "Value o...

Functions in C++

                “A set of statements that performs a specific task” is called a function. Generally, the code is reusable in anywhere. Features: ·        Easy to read. ·        It has modularity. ·        The maintenance of code is easy. First, consider about the syntax of creating a function. Syntax: Function declaration: ‘return_value function_name(datatype parameter1,…datatype parameter); Eg: int factorial(int n); Function Definition: return_type function_name(parameter1,parameter2,…… parametern) { Code; return(); } Eg: int factorial(int n) {   Code… return(); } The functions can be classified into two types. ·        User defined functions: user can create this function. ·        Built-in functions: these are the system files. Example:...

String Operations in c++ part3

 This post shows the remaining string operations as follows. Substring extraction Searching an element Other String operations using built-in functions Substring extraction:               It extracts the part of string. This program reads a character from the user. Using the ‘substr()’ function,it extracts a substring and display it in the output screen. C++ Program: #include <iostream> #include <string> using namespace std; int main() {     string st1 = "C++ Programming";     string sub_str1 = st1.substr(4, 14);        cout << "Substring 1: " << sub_str1<< endl;     string sub_str2 = st1.substr(0, 3);       cout << "Substring 2: " << sub_str2 << endl;     return 0; } Output: Substring 1: Programming Substring 2: C+...

String Operations in C++ part2

               Part1 includes the basic functionality of strings. This post explains the string operations for the following functionalities. Length of a String Modification of a string Length of a String:               This function finds the number of characters in the String. It uses two method. One is ‘size()’ and second one is ‘length()’ . Program: #include <iostream> #include <string> using namespace std; int main() {     string st1 = "C++ Programming";     //first method (size())     cout << "Length of the string using size(): " << st1.size() << endl;     // Second method (length())     cout << "Length of the string using length(): " << st1.length() << endl;     return 0; } Output: ...

String Operations in C++

               Strings are a special kind of objects in std::string class. It is a collection of characters in a common name. Features: The manipulation is easy. You can access, concatenate strings, extract a substring and compare them quickly. It has many built-in functions. You can add or remove characters according to the user need. It adopt it effectively. How to create and manipulate Strings? First, include header file #include<string> Next, declare a string as follows.. Syntax: ‘string str;’ Where string is the keyword to represent string. ‘str’ is the variable. Eg: ‘string a;’ C++ Program to print string: #include <iostream> #include <string> using namespace std; int main() {     string a="Welcome to C++";     cout<<a;     return 0; } Output: Welcome to C++ The operations of strings are given below. ·    ...

Built-in functions of std::array

             The built-in header file <array> has variety of member functions. The way of using these functions in c++ programs as follows… ‘size()’:               This function finds of the size of an array. C++ Program:   #include <iostream> #include <array> using namespace std; int main() {     std::array<int, 8> x = {1, 2, 3, 4, 5,6,7,8};     size_t size = x.size();     cout<<"the size of array is:"<<size<<endl;     return 0; } Output: the size of array is:8 ‘at()’:               This function finds an element in a position. Sample c++ code: #include <iostream> #include <array> using namespace std; int main() {     int pos,i;    ...