Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ struct

//function having struct as a parameter
#include <iostream>
#include <string>

using namespace std;

struct car{
    string name;
    string color;
    int maxSpeed;
    int model;
};

void fun (car x){
    cout << "Name: " << x.name << endl;
    cout << "Model: " << x.model << endl;
    cout << "Color: " << x.color << endl;
    cout << "Maximum speed: " << x.maxSpeed << endl;
}

int main(){
    car c1 = {"BMW", "Red", 250, 2022};
    car c2 = {"Mercedes", "Black", 220, 2019};
    fun (c1);
    fun (c2);
}
 
PREVIOUS NEXT
Tagged: #struct
ADD COMMENT
Topic
Name
5+4 =