Data Types
Lesson 7Author : Afrixi
Last Updated : November, 2022
In C++, data types are used to define the type of data that a variable can store. There are three categories of data types in C++: primitive data types, derived data types, and user-defined data types.
Primitive data types
are the basic data types in C++. They are used to store simple values such as integers, floating-point numbers, characters, and Boolean values.
#include <iostream>
using namespace std;
int main() {
int num = 10;
float pi = 3.14;
char letter = 'A';
bool isTrue = true;
cout << "num: " << num << endl;
cout << "pi: " << pi << endl;
cout << "letter: " << letter << endl;
cout << "isTrue: " << isTrue << endl;
return 0;
}
In the code above, we have defined variables of primitive data types, including an integer num
, a float pi
, a character letter
, and a Boolean value isTrue
. We have also printed the values of these variables using the cout
statement.
Derived data types
are derived from primitive data types. They include arrays, pointers, structures, and unions.
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
for(int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
return 0;
}
In the code above, we have defined an integer array numbers that can store five values. We have initialized this array with the values 1, 2, 3, 4, and 5, and then we have printed the values of the array using a for
loop.
#include <iostream>
using namespace std;
int main() {
int num = 10;
int* ptr = #
cout << "num: " << num << endl;
cout << "ptr: " << ptr << endl;
cout << "*ptr: " << *ptr << endl;
return 0;
}
In the code above, we have defined an integer variable num
, and a pointer variable ptr
that stores the memory address of num. We have then printed the value of num
, the memory address of ptr
, and the value of num
using the *
operator to dereference the pointer.
#include <iostream>
#include <string>
using namespace std;
struct Employee {
string name;
int id;
float salary;
};
int main() {
Employee emp1 = {"John Doe", 1001, 5000.0};
cout << "Name: " << emp1.name << endl;
cout << "ID: " << emp1.id << endl;
cout << "Salary: " << emp1.salary << endl;
return 0;
}
In the code above, we have defined a structure Employee
that contains three variables - a string name
, an integer id
, and a float salary. We have then defined a variable emp1
of type Employee and initialized it with values for the name
, id
, and salary
variables. We have then printed these values using the cout
statement.
User-defined data types
created by the programmer using classes and structures. They are used to create complex data structures and to implement object-oriented programming concepts such as inheritance and polymorphism.
#include <iostream>
#include <string>
using namespace std;
class Shape {
protected:
int width;
int height;
public:
Shape(int w, int h) {
width = w;
height = h;
}
virtual int area() = 0;
};
class Rectangle: public Shape {
public:
Rectangle(int w, int h): Shape(w, h) {}
int area() {
return width * height;
}
};
class Triangle: public Shape {
public:
Triangle(int w, int h): Shape(w, h) {}
int area() {
return (width * height) / 2;
}
};
int main() {
Rectangle rect(10, 5);
Triangle tri(10, 5);
cout << "Rectangle area: " << rect.area() << endl;
cout << "Triangle area: " << tri.area() << endl;
return 0;
}
In the code above, we have defined a class Shape
that contains two variables width and height. This class also has a virtual function area()
that returns the area of the shape. We have then created two classes Rectangle
and Triangle
that inherit from Shape
and override the area()
function to calculate the area of the specific shape. We have then created objects of these classes and called the area()
function to print their respective areas.
understanding data types in C++ is important because it helps you to choose the appropriate type of data for a given variable, to optimize memory usage, and to perform operations correctly.