Hi there, we’re Harisystems

"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".

For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.

1
1

C++ Data Types

In C++, data types define the kind of data that variables can store. C++ provides a wide range of data types, including primitive types, user-defined types, and standard library types. Understanding different data types is crucial for proper memory allocation, type safety, and efficient programming. In this article, we will explore some commonly used C++ data types along with examples.

1. Primitive Data Types

C++ provides several primitive data types that represent basic values. Here are a few commonly used primitive data types:

  • int: Used for storing whole numbers (integers).
  • float: Used for storing floating-point numbers (decimal values).
  • char: Used for storing single characters.
  • bool: Used for storing boolean values (true or false).

Here's an example that demonstrates the usage of primitive data types:

#include <iostream>

int main() {
    int age = 25;
    float salary = 2500.50;
    char grade = 'A';
    bool isEmployed = true;

    std::cout << "Age: " << age << std::endl;
    std::cout << "Salary: " << salary << std::endl;
    std::cout << "Grade: " << grade << std::endl;
    std::cout << "Employed: " << isEmployed << std::endl;

    return 0;
}

In the above example, we declare and initialize variables of different primitive data types and then print their values.

2. User-Defined Data Types

C++ allows you to define your own data types using classes and structures. These user-defined types can encapsulate multiple data members and functions. Here's an example of a user-defined class:

#include <iostream>

class Point {
public:
    int x;
    int y;
};

int main() {
    Point p;
    p.x = 5;
    p.y = 10;

    std::cout << "Point coordinates: (" << p.x << ", " << p.y << ")" << std::endl;

    return 0;
}

In the above code, we define a class called Point with two integer data members x and y. We create an object of the Point class and assign values to its data members.

3. Standard Library Data Types

C++ standard library provides additional data types through various classes and templates. One commonly used standard library data type is std::string for handling strings. Here's an example:

#include <iostream>
#include <string>

int main() {
    std::string name = "John Doe";

    std::cout << "Name: " << name << std::endl;

    return 0;
}

In the above code, we use the std::string class from the standard library to store and manipulate a string value.

Understanding different data types in C++ is essential for effective programming. Each data type has its own characteristics and usage scenarios. Choose the appropriate data type based on the requirements of your program to ensure proper data representation and memory utilization.

Experiment with different data types, explore their limitations and capabilities, and use them appropriately in your C++ programs to enhance clarity and efficiency.

4.5L

Learners

20+

Instructors

50+

Courses

6.0L

Course enrollments

4.5/5.0 5(Based on 4265 ratings)

Future Trending Courses

When selecting, a course, Here are a few areas that are expected to be in demand in the future:.

Beginner

The Python Course: Absolute Beginners for strong Fundamentals

By: Sekhar Metla
4.5 (13,245)
Intermediate

JavaScript Masterclass for Beginner to Expert: Bootcamp

By: Sekhar Metla
4.5 (9,300)
Intermediate

Python Coding Intermediate: OOPs, Classes, and Methods

By: Sekhar Metla
(11,145)
Intermediate

Microsoft: SQL Server Bootcamp 2023: Go from Zero to Hero

By: Sekhar Metla
4.5 (7,700)
Excel course

Future Learning for all

If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.

View Courses

Most Popular Course topics

These are the most popular course topics among Software Courses for learners