Breaking News

Interesting Facts about C++ 👨‍💻

C++ के बारे में रोचक तथ्य...


C++


C++ is a general-purpose, object-oriented programming language. It supports generic programming and low-level memory manipulation. Bjarne Stroustrup (Bell Labs) in 1979, introduced the C-With-Classes, and in 1983 with the C++.

Here are some awesome facts about C++ that may interest you:

1. The name of C++ signifies the evolutionary nature of the changes from C. “++” is the C increment operator.


2. C++ is one of the predominant languages for the development of all kind of technical and commercial software.


3. C++ introduces Object-Oriented Programming, not present in C. Like other things, C++ supports the four primary features of OOP: encapsulation, polymorphism, abstraction, and inheritance.


4. C++ got the OOP features from Simula67 Programming language.


5. A function is the minimum requirement for a C++ program to run.


6. C and C++ invented at same place i.e. at T bell laboratories.


7. Not purely object oriented: We can write C++ code without using classes and it will compile without showing any error message. The language have some extensions over C, that make OOP and generic programming more convenient.


8. Many programming languages are influenced by c++, some of which include C#, Java and even newer versions of C.


9. Concept of reference variables: operator overloading borrowed from Algol 68 programming language.


10. A major reason behind success of C++ is that it supports various programming styles. It is a multiparadigm programming language that not only supports OOP paradigm but also many other paradigms.


                                  C++ bitset interesting facts


Bitset is a container in C++ Standard Template Library for dealing with data at the bit level.

1. A bitset stores bits (elements with only two possible values: 0 or 1). We can however get the part of a string by providing positions to bitset constructor (Positions are with respect to string position from left to right)

Example:

// C++ program to demonstrate that we can get part of a  

// bit string in bitset. 

#include <bitset> 

#include <string> 

#include <iostream> 

  

int main() 

  std::string bit_string = "110010"; 

  std::bitset<8> b1(bit_string);             // [0, 0, 1, 1, 0, 0, 1, 0] 

  

  // string from position 2 till end 

  std::bitset<8> b2(bit_string, 2);      // [0, 0, 0, 0, 0, 0, 1, 0] 

  

  // string from position 2 till next 3 positions 

  std::bitset<8> b3(bit_string, 2, 3);   // [0, 0, 0, 0, 0, 0, 0, 1] 

    

  std::cout << b1 << '\n' << b2 << '\n' << b3 << '\n'; 

  

  return 0; 

}  

Output:

2. 00110010

3. 00000010

4. 00000001

5.

6. We can construct a bitset using the characters in the std::basic_string _str. An optional starting position _pos and length _n can be provided, as well as characters denoting alternate values for set (_one) and unset (_zero) bits.

Syntax:

7. std::bitset b1(str, pos, n, zero, one);

8. str    : string used to initialize the bitset

9. pos    : a starting offset into str

10. n    : number of characters to use from str

11. zero    : alternate character for unset bits in str

12. one    : alternate chracters for set bits in str 

o If _pos > str.size(), this constructor throws std::out_of_range.

o If any characters examined in _str is not zero or one, it throws std::invalid_argument.

// C++ program to demonstrate that we can construct bitset using 

// alternate characters for set and unset bits. 

#include <bitset> 

#include <string> 

#include <iostream> 

  

int main()  

    // string constructor using custom zero/one digits 

    std::string alpha_bit_string = "aBaaBBaB"; 

    std::bitset<8> b1(alpha_bit_string, 0, alpha_bit_string.size(), 

                      'a', 'B');         // [0,1,0,0,1,1,0,1] 

  

    std::cout << b1 << '\n'; 

13. Output:

14. 01001101

15. Constructs an object of class bitset, initializing the N bits to values that correspond to the characters provided in a c-style character string of zeros and ones. You call the constructor without casting the string into a string type. It also has two optional parameters, _Zero and _One, which indicate what character in _Str is to be interpreted to mean a 0 bit and a 1 bit, respectively.

#include <bitset> 

#include <iostream> 

  

int main()  

    // char* constructor using custom digits 

    std::bitset<8> b1("XXXXYYYY", 8, 'X', 'Y'); // [0, 0, 0, 0, 1, 1, 1, 1] 

    std::cout << b1 << '\n'; 

16. Output:

17. 00001111

18.  


Bitset Operations

1. std::bitset::to_string()

Converts the contents of the bitset to a string. Uses zero to represent bits with value of false and one to represent bits with value of true. The resulting string contains N characters with the first character corresponds to the last (N-1th) bit and the last character corresponding to the first bit. Also, we can pass the characters used to print true and false value through the parameters.

Example:

// C++ program to demonstrate that we can convert contents 

// of bitset to a string. 

#include <iostream> 

#include <bitset> 

  

int main() 

    std::bitset<8> b(42); 

    std::cout << b.to_string() << '\n' 

              << b.to_string('*') << '\n' 

              << b.to_string('O', 'X') << '\n'; 

Output:

00101010

**1*1*1*

OOXOXOXO

2. std::bitset::to_ulong():

Converts the contents of the bitset to an unsigned long integer. The first bit of the bitset corresponds to the least significant digit of the number and the last bit corresponds to the most significant digit. Function throws std::overflow_error if the value cannot be represented in unsigned long.

Example:

// C++ program to demonstrate that we can get value of bitset 

// as  unsigned long integer. 

#include <iostream> 

#include <bitset> 

   

int main() 

    std::bitset<5> b(5); 

    std::cout << b.to_ulong() << '\n';  

Output:

5


No comments