C++ Data Types: Comprehensive Overview of Basic and Custom Types
C++ is a strongly typed language with various data types, which can be categorized into basic data types and custom data types. In this article, we will provide a detailed introduction to the various data types in C++, including their characteristics, usage, and example code, to help basic users gain a comprehensive understanding of these concepts.
1. Basic Data Types
The basic data types are built-in data structures in C++, mainly including the following:
1. Integer (int)
Integer is used to store integer values. It typically consists of 4 bytes (32 bits), and its value range varies depending on the platform.
#include <iostream>
int main() { int a = 10; // Positive integer int b = -5; // Negative integer std::cout << "a: " << a << ", b: " << b << std::endl; return 0;}
2. Character (char)
Character is used to store a single character and occupies 1 byte. Character constants can be enclosed in single quotes, for example, ‘A’.
#include <iostream>
int main() { char letter = 'A'; std::cout << "letter: " << letter << std::endl; return 0;}
3. Floating Point (float and double)
Floating point is used to store numbers with decimal points. float
occupies 4 bytes, while double
occupies 8 bytes, providing higher precision.
#include <iostream>
int main() { float f = 3.14f; // Using f suffix indicates floating-point literal as float double d = 2.718; // Default is double std::cout << "float: " << f << ", double: " << d << std::endl; return 0;}
4. Boolean (bool)
Boolean is used to represent true or false, with values limited to true
(true) or false
(false), occupying 1 byte.
#include <iostream>
int main() { bool flag = true; if(flag) { std::cout << "The flag is true."<<std::endl; } else { std::cout<<"The flag is false."<<std::endl; } return 0; }
2. Custom Data Types
In addition to basic data types, C++ also allows users to create custom data structures. Some common custom data classes include structures, unions, and enumerations.
1. Enumeration (enum)
Enumeration is a user-defined data type used to name a set of related constants, enhancing code readability.
#include <iostream>
enum Color { RED, GREEN, BLUE };
int main() { Color myColor = GREEN;
if(myColor == RED) std::cout<< "Color is Red."<<std::endl;
return 0; }
2. Structure (struct)
Structures allow the aggregation of related information into a single data structure, providing a simple and flexible way to manage complex information.
#include <iostream>#include <string>
struct Person { std::string name; int age; };
int main() { Person p1 {"Alice", 30}; // Output person information std::cout<<"Name: "<<p1.name<<", Age: "<<p1.age<<std::endl;
return 0; }
3. Class (class)
Class is one of the core features of C++, allowing the creation of objects using encapsulation techniques, thus implementing the OOP programming paradigm. Classes can contain both attributes and methods.
#include <iostream>
class Car { private: float speed ; public: void setSpeed(float s) { speed=s;}
void showSpeed () { std::cout << "Current speed : "<<speed<<" km/h"<<std::endl;}
};
int main(){ Car myCar ; myCar.setSpeed(80); myCar.showSpeed();
return 0 ; }
3. Conclusion
This article introduced the basic and custom data types in C++, including integers, characters, booleans, floating points, and advanced concepts such as enumerations, structures, and classes. These concepts are crucial for writing any program, as the choice of appropriate data structures directly affects program performance and maintainability. Therefore, please use various data types reasonably based on actual needs. This will help improve your code quality and readability!