Back Button
Back to Chapter
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.
Collapse_Icon
Table of Contents

Non-primitive data types are those data types that are derived from primitive data types. They are further divided into derived and user-defined data types. They are also called “reference variables” or “object references” since they reference a memory location that stores the data.

Derived Data Types

The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types.

These include arrays, pointers, functions, and reference.

Let us look into each one in detail:-

Functions

A function is generally defined to save the user from writing the same lines of code again and again for the different input.

Syntax:

return data-type functionName(formal arguments) { ..... }

Let us write a program to understand syntax and usability of functions better.

#include using namespace std; int add(int a, int b) { return a+b;//assigns value a+b to the function and returns control to main function } main() { int a =10; int b=120; int sum=add(a,b);//function call, sum gets assigned the value returned by the add function int sum2 = add(30,50);//reusability of code. cout<<"sum 1:"<

Arrays

These are lists of variables of the same data type. To understand arrays better consider a list of cars. All elements in the list are cars although their names can differ. Arrays are useful when we want to store similar elements together. Consider the following code to understand implementation and syntax better.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/5951cafb-c849-464d-b1b1-2460bf03acdb/Untitled_Diagram_(6).png
#include using namesapce std; main() { int a[5]={10,20,30,40,50,60}; cout<

Reference

When we declared a variable as a reference, it becomes an alternative name for an existing variable. A variable can be declared as a reference by putting & in the declaration.

include using namespace std; int main() { int a = 3; // ref is a reference to a. int &ref = a; // Value of a is now changed to 20 ref = 20; cout << "x = " << x << endl ; // Value of a is now changed to 30 a = 30; cout << "ref = " << ref << endl ; return 0; }

Pointers

These are basically variables that store addresses of other variables. The value stored in a pointer irrespective of their data type is the same, long hexadecimal number. Pointers are very essential to perform tasks such as dynamic memory allocation. They are used to implement arrays and linked lists. Let us see how we can use pointers.

When we declare an array we are basically declaring a pointer to the first element of the array so when we say:

int a [10]

we mean to say create a pointer a which will point to the location of the base or first element of the array.

A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.

int b=20; int *a=&b; int c=*a

Don't get confused between the *, a pointer is declared using an asterisk and once it stores the reference, it is dereferenced using asterisk itself.

Syntax:

data-type *pointerName =&variableName;
#include using namesapce std; main() { int a=20; int *addr= &a;//& is used to access the location of variable cout<<"number "<<*addr<<"is stored at"<
Output: number 20is stored at0x6ffe04

User Defined Data Types

These are abstract data types that are defined by users themselves. The user can make use of one or more of the available data types to make his customized data type and use it in the program when required. Classes, structures, unions, and typedef are user-defined data types that allow us to create a customized data type.

Classes

Suppose you own an automobile company and you wish to build a car. The first thing we do is decide on the design and specify the features as well as the safety measures. Once your design is finalized all the manufactured cars will have the same design. This design is also known as a class.

Similarly in C+,+ a class is a design based on which objects belonging to that class are created. A class is essentially a blueprint, which means it has no actual space in memory, in fact only when an object which belongs to a particular class is created, space is allotted in the memory. An object is like a variable and a class is a data type.

Syntax:

class className { access specifier(public/private/protected) data member1 ; (attribute 1) data member2 ; (attribute 2) public function1 ; (behavior1) function 2; (behavior 2) } to create a object : className obj; to access members and function of the object : obj.member1; obj.member2;

Lets us see an example to understand better:

class firstClass { // The class public: // Access specifier int myNum; // Attribute (int variable) int myAge; int add(int a) { return myNum+a; } // Attribute (string variable) }; int main() { firstClass myObj; // Create an object of MyClass // Access attributes and set values myObj.myNum = 15; myObj.myAge = 19; int sum=obj.add(25); // Print attribute values cout << myObj.myNum <
Output: 15 19:40

Structures

In many situations, we need to store a group of data whether of similar data types or non-similar data types. Arrays can be used to store data of the same data type but in order to group data of different data types, we use structures. The struct keyword is used to create a structure.

Syntax

struct myName{ member1; member2; member3; . . . membern; };

Example code:

#include using namespace std; struct Point { int a, b; }; int main() { struct Point p01 = { 0, 1 }; // Accessing members of point p1 p01.a = 20; cout << "a = " << p1.a << ", b = " << p01.b; return 0; }
Output: a = 20, b = 1

Union

A Union is a user-defined datatype. The members of the union share the same memory location. The size of a union is decided by the size of the largest member of the union. If you want to use the same memory location for two or more members, a union is your most efficient option. They allow us to use the same memory location for multiple purposes. It is declared by using the keyword “union“.

Syntax:

union type{ int type1; float type2;// double type3; };

Example program:

#include using namespace std; union memory{ int type1; float type2; double type3; } main() { union memory iNt,fLoat,dOuble; iNt.type1 = 34; fLoat.type2 = 34.8; dOuble.type3 = 34.84; // Printing values cout << "The first value at " << "the allocated memory: " << iNt.type1 << endl; cout << "The next value stored " << "after removing the " << "previous value: " << fLoat.type2 << endl; cout << "The Final value value " << "at the same allocated " << "memory space: " << dOuble.type3 << endl; return 0; }
The first value at the allocated memory: 34 The next value stored after removing the previous value: 34.8 The Final value at the same allocated memory space: 34.84

Typedef

It literally means "type-definition", it is used for creating alternative names for data types such as int, float, char, and so on. It can also be used to have an alternative name for user-defined data types. It provides clarity to your code and makes it easier to update your code.

Syntax:

typedef int integer;

Now, you can use integer instead of int in the program.

Consider this example:

#include using namespace std; main() { typedef int marks; marks phy = 80; marks chem = 70; cout<<"phy marks:"<
Output: phy marks:80 chem marks:70