C++ Program to Illustrate Usage of Structs

This C++ program illustrates the usage of structs. The struct is defined using keyword ‘struct’ followed by its name and then the member declarations, either data-members or functions enclosed in curly braces. The members are set to public by default which is opposite to that of class.

Here is the source code of the C++ program illustrates the usage of structs. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

/*

* C++ Program to illustrate structs

*/

#include <iostream>

struct Car {

std::string brand;

int age;

void setAge(void){

std::cout<<"Enter the age of car : ";

std::cin>> age;

}

void setBrand(void){

std::cout<<"\nEnter the name of car : ";

std::cin>> brand;

}

};

void howOld(Car cc)

{

if(cc.age>5)

std::cout<< std::endl<< cc.brand<<" is very old.";

else

std::cout<< std::endl<< cc.brand<<" is very new.";

}

int main()

{

Car cc;

cc.setAge();

cc.setBrand();

howOld(cc);

}

advertisement

$ a.out
Enter the age of car : 10
Enter the name of car : Chevy
Chevy is very old.

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement

If you wish to look at all C++ Programming examples, go to C++ Programs.