Rabu, 22 April 2020

CPP: Structure

Hi, CPP Programmer, welcome.

Structure vs array

Structure are different types value. Array are the same type value.

Structure consist of:
1. Structure variable
2. Structure tag
3. Structure member

Defining structure tag is optional.
Defining member is just like the defining variable.

Basic structure use are below:
#include <iostream>
#include <cstring>

using namespace std;

//1.Create the structure
struct Books{
char title[50];
char author[50];
char subject[50];
int book_id;
};



int main()
{
//2. Declare Book1 of type Books
struct Books Book1;
struct Books Book2;


//3. Define book Book1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;

//4. Define book Book2 specification
strcpy( Book1.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;

//5. Print out Book1 info
cout << "Book 1 title: " << Book1.title << endl;
cout << "Book 1 author: " << Book1.author << endl;
cout << "Book 1 subject: " << Book1.subject << endl;
cout << "Book 1 book id: " << Book1.book_id << endl;

//6. Print
cout << "Book 1 title: " << Book1.title << endl;
cout << "Book 1 author: " << Book1.author << endl;
cout << "Book 1 subject: " << Book1.subject << endl;
cout << "Book 1 book id: " << Book1.book_id << endl;

}


Sending structure variable pointer to a function is almost the same as sending the struct variable to the function, the difference is: if you use structure variable pointer you use asterix and you need to declare a structure variable pointer.

Tidak ada komentar:

Posting Komentar