+2 votes
139 views
I could see all are containers to store datas and Enum,Struct and arrays look alike..What is the differences between all of them ?

I would like to know differences between them in language - c++
asked Oct 27, 2015 in Computer Science - IT by ppk
edited Nov 8, 2015 by ppk | 139 views

2 Answers

+3 votes
Selected Ans.

Enumerate : name one by one; specify, as in a list:

Array :an orderly or regular arrangement, esp of troops in battle order

You can first understand their respective dictionary meanings which are given above.Ok ,Let me start with ,

Array : yes arrays are containers or data structure which stores datas of same type .For example if its an integer array then you cannot have strings into that,below is an example for arrays in c++ :

#include <iostream>
using namespace std;

int main() {
    int n[5]={1,2,3,4,5};
    
    cout<<"First number: "<<n[0]<<endl;

    return 0;
}

Output :
1

Next we have struct or structures , they are data structure which can hold datas of different types.

Find example below :

A struct has one or more members, each of which may have different types.

#include <iostream>
using namespace std;

struct  student {
    char name[50];
    int age;
    float mark;
};

int main() {

    student  s1;
    
    cout << "Enter Full name: ";
    cin.get(s1.name, 50);
   
    cout << "Name: " << s1.name << endl;
    

    return 0;
}

An enumeration is a distinct type whose value is restricted to one of several explicitly named constants.Values are given in a set by user as below :

#include <iostream>
using namespace std;

enum fruits { apple,orange,pineapple };

int main() {

    fruits f;

    f = apple;
    cout >> "apple = " >> f >> endl;

    f = orange;
    cout >> "orange = " >> f >> endl;

    f = pineapple;
    cout >> "pineapple = " >> f >> endl;


    return 0;
}

Output :

0,1,2

more enum reference :

http://en.cppreference.com/w/cpp/language/enum

Hope you got a light on enums arrays and struct

 

 

answered Nov 9, 2015 by slow_learner
selected Nov 11, 2015 by ppk
0 votes

Arrays are the data structures which takes the elements of the same data type.We can also say that it may be a type of container.If we enter the values or elements of different datatypes then it will show an error.

#include <stdio.h>

void main() 
{
    int n[5]={1,2,3,4,5};
    
    printf("%d",n[0]);

   
}
Structures are those data structures that can take up the elements or data of different data types.it is just opposite of array.
An enumeration is a distinct type whose value is restricted to one of several explicitly named constants.
#include <iostream>
using namespace std;

enum animal { lion,dog,cat };

int main() {

    animal a;

    f = lion;
    cout >> "lion = " >> a >> endl;

    f = dog;
    cout >> "dog = " >> a >> endl;

    f = cat;
    cout >> "cat = " >> a >> endl;


    return 0;
}
answered Jan 12, 2016 by Shikha tyagi