Node is saved as draft in My Content >> Draft
-
Composite DataType in C++
C++ allows you to define different types of variables. You can define a single variable that can hold data of multiple variables.
Defining a Structure:
struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
}book;
We define variables inside structure and we have created object at last for that structure to access its elements.
Accessing Structure Members:
#include <iostream>
#include <cstring>
using namespace std;
struct Student
{
char name[50];
char address[50];
};
int main( )
{
struct Student Stu1;
struct Student Stu2;
// book 1 specification
strcpy( Stu1.name, "Himanshu Joshi");
strcpy( Stu1.address, "Dehradun");
// book 2 specification
strcpy( Stu1.name, "Manoj Singh");
strcpy( Stu1.address, "Nainital");
// Print Stu1 info
cout << "Stu1 name : " << Stu1.name <<endl;
cout << "Stu1 address : " << Stu1.address <<endl;
// Print Stu2 info
cout << "Stu2 name : " << Stu2.name <<endl;
cout << "Stu2 address : " << Stu2.address <<endl;
return 0;
}
0 Comment(s)