Followers

Friday, November 17, 2023

Basic data types used in c Language.


C language provides several basic data types that are used to define variables. Here are some of the basic data types in C:
  1. 1. int: Integer data type is used to store whole numbers. It can be both positive and negative.

    int x = 10;
  2. 2. float: Float data type is used to store floating-point numbers (real numbers).

    float pi = 3.14;
  3. 3. double: Double data type is used to store double-precision floating-point numbers. It provides more precision than the float data type.

    double bigNumber = 123456789.987654321;
  4. 4. char: Char data type is used to store a single character.

    char grade = 'A';
  5. 5. short: Short data type is used to store small integers.

    short smallNumber = 5;
  6. 6. long: Long data type is used to store large integers.

    long bigInteger = 1234567890;
  7. 7. long long: Long long data type is used to store very large integers.

    long long veryBigInteger = 1234567890123456789LL;
  8. 8. _Bool: Bool data type is used to represent boolean values. It can have values either true (1) or false (0).

    _Bool isTrue = 1;
  9. 9. void: Void is a special data type that indicates the absence of a specific type.

    void myFunction() {
    // some code
    }
  10. These data types can be used to declare variables, and the choice of a data type depends on the kind of data we want to store and the range of values it needs to cover. The size of these data types may vary depending on the compiler and the platform.

Friday, November 10, 2023

Template in C++

 

In C++, a template is a feature that allows us to write generic programs. It enables us to create functions and classes that work with any data type. Templates provide a way to write code once and use it with different types without having to duplicate the code for each type.

There are two main types of templates in C++:

1. Function Templates:

Function templates allow us to define a generic function that can operate on different data types. The syntax for a function template is:

cpp
template <typename T> T myFunction(T param) { // Function body // ... }

2. Class Templates:

Class templates allow us to create a generic class that can work with different data types. The syntax for a class template is:

cpp
template <typename T> class MyClass { public: T member; // Constructor MyClass(T value) : member(value) {} // Member function T getValue() const { return member; } };


Here's a simple example of a function template in C++:

cpp
#include <iostream> using namespace std; // Function template for finding the maximum of two values template <typename T> T max(T a, T b) { return (a > b) ? a : b; } int main() { // Example with integers int intMax = max(3, 7); cout << "Max of 3 and 7 is: " << intMax << endl; // Example with floating-point numbers double doubleMax = max(4.5, 2.3); cout << "Max of 4.5 and 2.3 is: " << doubleMax << endl; // Example with strings string strMax = max("apple", "orange"); cout << "Max of 'apple' and 'orange' is: " << strMax << endl; return 0; }

In this example, the max function is a template that can work with different data types (int, double, std::string, etc.). The <typename T> declares a template parameter T, which is a placeholder for the actual type. When we use the function, the compiler automatically generates the appropriate code for the given data types.

Similarly, you can create class templates. Here's an example of a simple template class:

cpp
#include <iostream> using namespace std; // Template class for a generic pair template <typename T, typename U> class Pair { public: T first; U second; Pair(T a, U b) : first(a), second(b) {} }; int main() { // Example with integers and strings Pair<int, string> myPair(1, "apple"); cout << "Pair: " << myPair.first << ", " << myPair.second << endl; return 0; }

In this case, the Pair class is a template that can hold a pair of values of different types. When we create an instance of Pair, we specify the types for the template parameters (int and std::string in this example).

Templates are a powerful feature in C++ that enable us to write flexible and reusable code. They are widely used in the standard library and are essential for creating generic algorithms and data structures.