1. Integers: Integers are used to represent whole numbers. They can be either signed (positive, negative, or zero) or unsigned (positive or zero).
cppint a = 42; // Signed integer unsigned int b = 100; // Unsigned integer2. Floating-Point Numbers: Floating-point types are used to represent numbers with a decimal point. They include
float
,double
, andlong double
.cppfloat pi = 3.14159; double gravity = 9.81;3. Characters: Characters represent single characters like letters, digits, or symbols.
cppchar letter = 'A';
4. Boolean: Boolean type represents true or false values.
cppbool isTrue = true;
5. Strings: Strings are sequences of characters.
cppstd::string name = "John Doe";
6. Arrays: Arrays hold a fixed number of elements of the same data type.
cppint scores[5] = {95, 87, 72, 64, 90};
7. Pointers: Pointers hold memory addresses of other variables.
cppint x = 10; int *ptr = &x; // Pointer to an integer8. Enums: Enums define a set of named integer constants.
cppenum Color { RED, GREEN, BLUE }; Color myColor = GREEN;9. Structures: Structures allow grouping different data types under a single name.
cppstruct Person { std::string name; int age; }; Person person1 = {"Alice", 25};10. Classes: Classes are user-defined data types that can include both data members and member functions.
cppclass Circle { public: double radius; double getArea() { return 3.14159 * radius * radius; } }; Circle myCircle; myCircle.radius = 5.0; double area = myCircle.getArea();
These are just some of the basic data types in C++. Each data type serves a specific purpose and has its own set of operations that can be performed on it. Understanding these data types is essential for effective programming in C++.
No comments:
Post a Comment