explain the difference between Struct Union Class and ArrayS

explain the difference between Struct, Union, Class and Array

Solution

Struct - Struct statement is used to define a new data type with more than 1 member. The members can be of different data types (int, float, char etc)

Syntax:

struct tag_name {

type1 member1;

type2 member2;

}

Example:

struct Books {

char title[50];

int book_id;

} book;

Union - Union allows to store different data types in the same location. One can define union with many members but only one member can contain a value at any given point of time.

Example:

union Data {

int i;

float f;

}

At any point of time, only one of i or f value will be stored.

Class: Class is a blueprint of a datatype. It defines objects of the class and the operations that can be performed on the objects. Class can have objects with restricted access to public (using private and protected as access-specifier).

Example:

class Box

{

private:

double length;

double breadth;

double height;

public:

void get_height (){

cout << Height << height;

}

}

Array: It can hold fixed number of elements of the same datatype. The length of the array is fixed at the time of creation.

Examples: int a[10]; char b[50]

Difference:

1. Array can hold data of only one type. Struct, Union and Class can hold data of different types.

2. Union is a subset of Struct where only one member (among the defined members) is stored in the memory.

3. Class can hold the operations on the data (which includes functions also). This is not possible with Struct, Union and Arrays.

explain the difference between Struct, Union, Class and ArraySolutionStruct - Struct statement is used to define a new data type with more than 1 member. The me
explain the difference between Struct, Union, Class and ArraySolutionStruct - Struct statement is used to define a new data type with more than 1 member. The me

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site