Primitive Data Types in Data Structure
C++ Data Type: In C++, data types refer to the type of value that a variable can hold. A data type determines the amount of memory required to store a value, and it also determines the set of operations that can be performed on that value.
Bassic Data Type | Derived Data Type |
---|---|
Integer Data Type | Pointer |
Float Data Type | Array |
Character Data Type | Structure |
Double Data Type | Union |
Basic Data Types Hacker rank Solution in C++ |
int: This data type represents an integer value. It can hold values ranging from -32768 to 32767.
Integer Data Types in C++ Language
Data Types | Size of Byte | Range |
---|---|---|
short | 2 | -32768 To +32768 |
int | 2 | -32768 To +32768 |
Unsigned int | 2 | 0 To 65536 |
long | 4 | -214748 To +214748 |
Unsigned long int | 4 | 0 To 4294967295 |
#include <iostream> int main() { int x; // Declare an integer variable called x x = 10; // Assign the value 10 to x std::cout << x << std::endl; return 0; } **********OUTPUT********** 10 |
float: This data type represents a single-precision floating-point value. It can hold values with decimal points, and it is useful for representing numbers that are not necessarily integers.
Float Data Types in C++ Language
Data Types | Size of Bytes | Range |
---|---|---|
float | 4 | 3.4E-38 To 3.4E+38 |
double | 8 | 1.7E-308 To 1.7e+308 |
Long double | 10 | 3.4E-4932 To 1.1E+4932 |
#include <iostream> int main() { float x; // Declare a float variable called x x = 3.14; // Assign the value 3.14 to x std::cout << x << std::endl; return 0; } **********OUTPUT********** 3.14 |
double: This data type represents a double-precision floating-point value. It can hold values with decimal points, and it has a larger range and greater precision than the float data type.
#include <iostream> int main() { double x; // Declare a double variable called x x = 3.14159265358979323846; // Assign a long decimal value to x std::cout << x << std::endl; return 0; } **********OUTPUT********** 3.14159265358979323846 |
char: This data type represents a single character. It can hold any character in the ASCII table, including letters, digits, and special characters.
Data Types | Size of Bytes | Range |
---|---|---|
char | 1 | -128 To +127 |
Signed char | 1 | -128 To +127 |
Unsigned char | 1 | 0 To 255 |
#include <iostream> int main() { char ch = 'A'; std::cout << ch << std::endl; return 0; } **********OUTPUT********** A |
bool: This data type represents a boolean value, which can be either true or false.
There are also several other data types in C++, such as short, long, and unsigned, which have different sizes and ranges.
It's important to choose the appropriate data type for your variables, depending on the values that they will hold. Using the wrong data type can lead to errors or unexpected behavior in your code.
#include <iostream> int main() { bool flag = true; if (flag) { std::cout << "flag is true" << std::endl; } else { std::cout << "flag is false" << std::endl; } return 0; } |
In this example, the flag variable is assigned a boolean value of true, and the if statement uses it to control the flow of the program. The output of this program would be "flag is true".
The bool data type is often used in C++ to represent the result of a boolean expression or to control the flow of a program based on the value of a boolean variable. It is a useful and efficient way to represent true/false values in a program.
Non-primitive Data Type in C++
C++, a non-primitive data type is a data type that is not a built-in primitive data type, such as int, float, or char. Non-primitive data types are also known as composite data types or reference data types.
non primitive data types in data structure
1. Arrays: An array is a collection of elements of the same data type, stored in contiguous memory locations. In C++, arrays can be created using the [] operator, and they are indexed starting at 0.
#include <iostream> int main() { // Declare an array of integers with 5 elements int numbers[5] = {1, 2, 3, 4, 5}; // Access and print the third element of the array std::cout << numbers[2] << std::endl; // Outputs "3" // Modify the fourth element of the array numbers[3] = 10; // Print the entire array for (int i = 0; i < 5; i++) { std::cout << numbers[i] << " "; } return 0; } **********OUTPUT********** 1 2 3 10 5 |
2. Strings: A string is a sequence of characters that is used to represent text data. In C++, strings can be created using the string data type, which is part of the string library.
#include <iostream> #include <string> int main() { // Declare a string variable std::string message = "Hello, world!"; std::cout << message[2] << std::endl; message[3] = 'L'; std::cout << message << std::endl; // Concatenate two strings std::string greeting = "Hello, "; std::string name = "Ravi"; std::string full_greeting = greeting + name; std::cout << full_greeting << std::endl; return 0; } **********OUTPUT********** I HelLo, world! Hello, Ravi |
3. Pointers: A pointer is a variable that stores the memory address of another variable. In C++, pointers are created using the * operator and are used to manipulate memory directly.
#include <iostream> int main() { // Declare an integer variable and a pointer to it int value = 5; int* ptr = &value; // Access and print the value of the integer through the pointer std::cout << *ptr << std::endl; // Modify the value of the integer through the pointer *ptr = 10; // Print the modified value of the integer std::cout << value << std::endl; return 0; } **********OUTPUT********** 5 10 |
4. Struct C++: A structure is a user-defined data type that consists of a collection of variables of different data types, grouped together under a single name. In C++, structures are created using the struct keyword.
#include <iostream> // Define a structure to represent a student struct Student { std::string name; int age; double grade; }; int main() { // Declare a student structure and initialize its fields Student s = {"John", 20, 80.0}; // Access and print the fields of the student structure std::cout << "Name: " << s.name << std::endl; std::cout << "Age: " << s.age << std::endl; std::cout << "Grade: " << s.grade << std::endl; // Modify the fields of the student structure s.name = "Jane"; s.age = 21; s.grade = 90.0; // Print the modified fields of the student structure std::cout << "Name: " << s.name << std::endl; std::cout << "Age: " << s.age << std::endl; std::cout << "Grade: " << s.grade << std::endl; return 0; } |
5. Classes: A class is a user-defined data type that defines the data and behavior of objects in a program. In C++, classes are created using the class keyword and are used to implement object-oriented programming concepts such as inheritance and polymorphism.
Overall, non-primitive data types are an important aspect of C++ that allow programmers to represent complex data structures and implement advanced programming concepts.
#include <iostream> // Define a class to represent a student class Student { private: std::string name; int age; double grade; public: // Constructor to initialize the fields of the student Student(std::string n, int a, double g) { name = n; age = a; grade = g; } // Methods to access and modify the fields of the student std::string getName() { return name; } int getAge() { return age; } double getGrade() { return grade; } void setName(std::string n) { name = n; } void setAge(int a) { age = a; } void setGrade(double g) { grade = g; } }; int main() { // Create a student object using the constructor Student s("John", 20, 80.0); // Access and print the fields of the student object std::cout << "Name: " << s.getName() << std::endl; std::cout << "Age: " << s.getAge() << std::endl; std::cout << "Grade: " << s.getGrade() << std::endl; // Modify the fields of the student object s.setName("Jane"); s.setAge(21); s.setGrade(90.0); // Print the modified fields of the student object std::cout << "Name: " << s.getName() << std::endl; std::cout << "Age: " << s.getAge() << std::endl; std::cout << "Grade: " << s.getGrade() << std::endl; return 0; } |
What is Data Type in OOP
What are the 2 main types of data
There are many different types of data that can be used in various contexts, but in general, data can be broadly classified into two main categories: qualitative data and quantitative data.
Qualitative data: Qualitative data is non-numeric data that describes the characteristics or attributes of something. It is often used to describe the qualities or features of a phenomenon, such as a person's personality, the color of an object, or the texture of a material. Qualitative data can be collected through methods such as interviews, focus groups, and observations.
Quantitative data: Quantitative data is numeric data that represents a measurable quantity or amount. It is often used to describe the relationship between variables or to identify patterns and trends in data. Quantitative data can be collected through methods such as experiments, surveys, and statistical analysis.
Both qualitative and quantitative data are important and have their own unique characteristics and uses. Qualitative data can provide insights into the underlying reasons or motivations behind a phenomenon, while quantitative data can help to identify patterns and trends in data and make predictions based on those trends.
FAQ C++ Data Types
type array_name[size1][size2]...[sizeN]; |
int myArray[2][3]; |
0 Comments