Examples of structures in c++ Programming

0

The struct keyword defines a structure type followed by an identifier (name of the structure).

Example :

struct Person

{

    char name[50];

    int age;

    float salary;

};

All Contributions

Using enums for flags :

#include <iostream>
using namespace std;
enum designFlags 
{
    BOLD = 1,
    ITALICS = 2,
    UNDERLINE = 4
};
int main() 
{
    int myDesign = BOLD | UNDERLINE; 

        //    00000001
        //  | 00000100
        //  ___________
        //    00000101

    cout << myDesign;

    return 0;
}

Using enum variable in Structures :

#include <iostream>
using namespace std;

enum suit {
    club = 0,
    diamonds = 10,
    hearts = 20,
    spades = 3
} card;

int main() 
{
    card = club;
    cout << "Size of enum variable " << sizeof(card) << " bytes.";   
    return 0;
}

Program that Changing Default Value of Enums :

#include <iostream>
using namespace std;

enum seasons { spring = 34, summer = 4, autumn = 9, winter = 32};

int main() {

    seasons s;

    s = summer;
    cout << "Summer = " << s << endl;

    return 0;
}

Using Enumeration Type in Structures :

#include <iostream>
using namespace std;
enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
int main()
{
    week today;
    today = Wednesday;
    cout << "Day " << today+1;
    return 0;
}

Example with Pointers to Structure :

#include <iostream>
using namespace std;
struct Distance 
{
    int feet;
    float inch;
};
int main() 
{
    Distance *ptr, d;
    ptr = &d;  
    cout << "Enter feet: ";
    cin >> (*ptr).feet;
    cout << "Enter inch: ";
    cin >> (*ptr).inch;
    cout << "Displaying information." << endl;
    cout << "Distance = " << (*ptr).feet << " feet " << (*ptr).inch << " inches";
    return 0;
}

total contributions (10)

New to examplegeek.com?

Join us