Struct Example in C++


The program defines a structure test with two data members x and y. It also defines a member function display() to display the values of x and y. In the main() function, an object o of type test is created. The values of x and y are assigned using the dot operator .. Finally, the display() function is called on the object o, which displays the values of x and y.

Source Code

#include<iostream>
using namespace std;
struct test
{
    int x;  //Data Member
    int y;

    void display()//Data Function
    {
        cout<<"X value is : "<<x<<endl;
        cout<<"Y value is : "<<y<<endl;
    }
};

int main()
{
    test o;
    o.x=10;
    o.y=20;
    o.display();
    return 0;
}
To download raw file Click Here

Output

X value is : 10
Y value is : 20

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples