Min menu

Pages

VB

Type struct in C++ _ C

word structinC++

In the previous lessons, we always dealt with simple data types, for example, we were defining a variable of its type int, and this variable we used to put only one value in it.
Simple types are very useful and we will always deal with them, but in certain cases we have to define new types.

As a simple example, if we intend to send information of a group of products and each product has the following information: product name, production date, price and components.
Here it would be an excellent option to create a new type that represents the product, that is, one that contains the basic information that any product must have.
And then any new product we want to define, we make a copy of it.

The word structis used to define a new type and this type can contain a set of values ​​of any type in an orderly fashion and easy to handle.
Most likely, you will find that the new type that is defined by the word structis used for this purpose only.
But you have to know that it can contain anything else, such as its own functions and even structother and you will see that later in the examples.


Terms about the struct type in C-Plus

Any new species identified by the word structis calledStructure.
Any instance you create of the new type you define is called an object(Object)from him.

new definition structinC++

If you are going to define the new type by keyword structyou should follow the following approach.

struct struct_name {
member_definition;
member_definition;
member_definition;
..
} object_names ;
  • struct_name :   In its place we put the name that we will give to the new type.

  • member_definition :   Here you can pass the name and type of whatever you intend to make the new type own.

  • object_names :   If you want to create an object (copy) of the new type directly when it is defined, whatever name you put here will be considered an object of it.

Now you have to know that you can define it structanywhere you want, for example you can define it in a special file, outside the function main()and even inside it if you want.


In the following example, we have defined a new type whose name Bookrepresents the information that can be contained in any book such as its title, author's name, price and number of pages.

Example

      struct Book { 
		  string title; 
		  string author; 
		  double price; 
		  int numberOfPages; 
		  };
    

Create an object from structinC++

There are several ways to create objects from structand you can go any way you like.


In the following example, we created structits name Bookand then created an object from it.

The first example is to create an object from a struct in C++

      // It contains 4 variables called Book struct. Here we have defined 
		  struct Book { 
		  string title; 
		  string author; 
		  double price; 
		  int numberOfPages; 
		  }; 
	  
		  // book called Book Here we create an object from 
		  struct Book book;
    

In the following example, we created structits name Bookand created an object directly from it.
So, this example is exactly the same as the first example, but the code is written in it only briefly.

second example

      // book contains 4 variables. And then we created an object of it called Book, its name is struct. Here we define 
		  struct Book { 
		  string title; 
		  string author; 
		  double price; 
		  int numberOfPages; 
		  } book;
    

In the following example, we created structits name Bookand then created three objects from it.

third example

      // It contains 4 variables called Book struct. Here we have defined 
		  struct Book { 
		  string title; 
		  string author; 
		  double price; 
		  int numberOfPages; 
		  }; 
	  
		  // book3 and the third is called book2, the second is his name, the first book is his name, the book is here by creating three objects from the 
		  struct book book1; 
		  struct book book2; 
		  struct book book3;
    

In the following example, we created structits name Bookand then created three objects from it.
So, this example is exactly the same as the third example, but the code is written in it only briefly.

Fourth example

      // It contains 4 variables called Book struct. Here we have defined 
		  struct Book { 
		  string title; 
		  string author; 
		  double price; 
		  int numberOfPages; 
		  }; 
	  
		  // book3 and the third his name is book2, the second his name is book1, the first is his name, Book here by creating three objects from 
		  struct Book book1, book2, book3;
    

Access to things inside an object from structinC++

To access the values ​​of the variables contained in it, we use the operator .ie the regular point.


technical terms

The worker .is calledMember OperatorBecause it allows us to access anything(Member)present in the object.


In the following example, we have defined structits name Bookrepresenting the information that can be included in any book such as its title, author's name, price and number of pages.
Then we create two objects from it and then give each one its own values.

Example

main.cpp
      #include <iostream> 
	  
		  using namespace std; 
	  
		  // It contains 4 variables called Book struct. Here we have defined 
		  struct Book { 
		  string title; 
		  string author; 
		  double price; 
		  int numberOfPages; 
		  }; 
	  
		  int main() 
		  { 
		  // book2 and the second one is called book1 the first is called Book, here we have defined two objects from 
		  struct Book book1; 
		  struct book book2; 
	  
		  // book1 Here we have given values ​​for the object variables 
		  book1.title = "C++ for beginners"; 
		  book1.author = "Mhamad Harmush"; 
		  book1.price = 9.99; 
		  book1.numberOfPages = 420;
	  
		  // book2 Here we have given values ​​to the object variables 
		  book2.title = "Network 1"; 
		  book2.author = "Nadine Masri"; 
		  book2.price = 22.49; 
		  book2.numberOfPages = 310; 
	  
		  // book1 Here we have shown the values ​​of the 
		  cout object << "Book 1 ----------- \n"; 
		  cout << "Title: " << book1.title << "\n"; 
		  cout << "Author: " << book1.author << "\n"; 
		  cout << "Price: " << book1.price << "$\n"; 
		  cout << "Number of pages: " << book1.numberOfPages << "\n\n"; 
	  
		  // book2 Here we have shown the values ​​of the 
		  cout object << "Book 2 ----------- \n"; 
		  cout << "Title: " << book2.title << "\n"; 
		  cout << "Author: " << book2.author << "\n";
		  cout << "Price: " << book2.price << "$\n"; 
		  cout << "Number of pages: " << book2.numberOfPages; 
	  
		  return 0; 
		  }
    

We will get the following result when running.

      Book 1 ----------- 
		  Title: C++ for beginners 
		  Author: Mhamad Harmush 
		  Price: $9.99 
		  Number of pages: 420 
	  
		  Book 2 ----------- 
		  Title: Network 1 
		  Author: Nadine Masri 
		  Price: $22.49 
		  Number of pages: 310
    

Comprehensive examples of the struct type in C++

C++ Defining a function that takes a parameter of its type struct

In the following example, we have defined structits name Bookrepresenting the information that can be included in any book such as its title, author's name, price and number of pages.
Then we defined a function whose name printInfowhen called, we pass an object of its Booktype to it, and it prints its values ​​in an orderly manner.

Finally, we create an object from Bookand give it values, and then pass it to the function printInfo()to print its values.

Example

main.cpp
      #include <iostream> 
	  
		  using namespace std; 
	  
		  // It contains 4 variables called Book struct. Here we have defined 
		  struct Book { 
		  string title; 
		  string author; 
		  double price; 
		  int numberOfPages; 
		  }; 
	  
		  // it prints all the values ​​of the variables in it when Book is called. We pass it an object from printInfo. Here we have defined a function called 
		  void printInfo(struct Book book) 
		  { 
		  cout << "Title: " << book.title << "\n"; 
		  cout << "Author: " << book.author << "\n"; 
		  cout << "Price: " << book.price << "$\n"; 
		  cout < < "Number of pages: " << book.numberOfPages << "\n"; 
		  }
	  
		  // main() here we have defined the function 
		  int main() 
		  { 
		  // book called Book here we have defined an object from 
		  struct Book book; 
	  
		  // book Here we have given values ​​for the object variables 
		  book.title = "C++ for beginners"; 
		  book.author = "Mhamad Harmush"; 
		  book.price = 9.99; 
		  book.numberOfPages = 420; 
	  
		  // its until you print the values ​​in it book and pass the printInfo() object here we called the 
		  printInfo(book) function; 
	  
		  return 0; 
		  }
    

We will get the following result when running.

      Title: C++ for beginners 
		  Author: Mahamad Harmush 
		  Price: $9.99 
		  Number of pages: 420
    

C++ defines a function inside struct

In the following example, we have defined structits name Bookrepresenting the information that can be included in any book such as its title, author's name, price and number of pages.
We also put a function named printInfoinsidethestructWhen called from any object we create it from, it prints its values ​​in order.

Finally, we create an object from Bookand give it values, and then call the function printInfo()from it to print its values.

Example

main.cpp
      #include <iostream> 
	  
		  using namespace std; 
	  
		  // It contains 4 variables called Book struct. Here we have defined 
		  struct Book { 
		  string title; 
		  string author; 
		  double price; 
		  int numberOfPages; 
	  
		  // prints all the values ​​of the variables in Book when called from any object we create from printInfo Here we have defined a function called 
		  void printInfo() 
		  { 
		  cout << "Title: " << title << "\n"; 
		  cout << "Author: " << author << "\n"; 
		  cout << "Price: " << price << "$\n"; 
		  cout << "Number of pages: " << numberOfPages << "\n"; 
		  } 
		  }; 
	  
		  // main() Here we have defined the function
		  int main() 
		  { 
		  // book named Book Here we have defined an object from 
		  struct Book book; 
	  
		  // book Here we have given values ​​for the object variables 
		  book.title = "C++ for beginners"; 
		  book.author = "Mhamad Harmush"; 
		  book.price = 9.99; 
		  book.numberOfPages = 420; 
	  
		  // In order to print the values ​​in book from the printInfo() object here we called the function 
		  book.printInfo(); 
	  
		  return 0; 
		  }
    

We will get the following result when running.

      Title: C++ for beginners 
		  Author: Mahamad Harmush 
		  Price: $9.99 
		  Number of pages: 420
    

C++ Defining a function that takes a parameter of its type is a pointerfor struct

To access things in a pointerforstructWe use the factorial ->, not the .usual factor.


In the following example, we have defined structits name Bookrepresenting the information that can be included in any book such as its title, author's name, price and number of pages.
Then we defined a function whose name printInfowhen called, we pass to it the address of an object that Bookis in memory, and it prints its values ​​in an orderly manner.

Finally, we create an object from Bookand give it values, and then pass its address in memory to the function printInfo()so that it prints its values.

Example

main.cpp
      #include <iostream> 
	  
		  using namespace std; 
	  
		  // It contains 4 variables called Book struct. Here we have defined 
		  struct Book { 
		  string title; 
		  string author; 
		  double price; 
		  int numberOfPages; 
		  }; 
	  
		  // it prints all the values ​​of the variables in it when Book is called. We pass it the address of an object from printInfo. Here we have defined a function called 
		  void printInfo(struct Book* book) 
		  { 
		  cout << "Title: " << book->title << "\n "; 
		  cout << "Author: " << book->author << "\n"; 
		  cout << "Price: " << book->price << "$\n"; 
		  cout << "Number of pages: " << book->numberOfPages << "\n"; 
		  }
	  
		  // main() here we have defined the function 
		  int main() 
		  { 
		  // book called Book here we have defined an object from 
		  struct Book book; 
	  
		  // book Here we have given values ​​for the object variables 
		  book.title = "C++ for beginners"; 
		  book.author = "Mhamad Harmush"; 
		  book.price = 9.99; 
		  book.numberOfPages = 420; 
	  
		  // its until you print the values ​​in book and pass the address of the printInfo() object here we called the 
		  printInfo(&book); 
	  
		  return 0; 
		  }
    

We will get the following result when running.

      Title: C++ for beginners 
		  Author: Mahamad Harmush 
		  Price: $9.99 
		  Number of pages: 420
    

C++ short name modefor struct by wordtypedef

In the previous examples, we noticed that whenever we wanted to create an object Bookwe had to writestruct Book.
If you want to abbreviate these two words to just one word, you can put a short name for the type at the Bookmoment of its definition and then you can use it whenever you want to create an object from it.


In the following example, we have created structand Bookgiven it as an abbreviated name based on the wordtypedef.
Then we created three objects out of it using the acronym we gave it.

Example

      // as a short name and we put 4 variables in the Book we gave it the word struct here we defined a 
		  typedef struct { 
		  string title; 
		  string author; 
		  double price; 
		  int numberOfPages; 
		  } Book; 
	  
		  // book3 and the third his name is book2 the second his name, the first book1 his name, the book here by creating three objects from 
		  Book book1, book2, book3;
    

C++ short name modeforstructby word using

In the previous examples, we noticed that whenever we wanted to create an object Bookwe had to writestruct Book.
If you want to abbreviate these two words to just one word, you can put a short name for the type at the Bookmoment of its definition and then you can use it whenever you want to create an object from it.


In the following example, we have created structand Bookgiven it as an abbreviated name based on the wordusing.
Then we created three objects out of it using the acronym we gave it.

Example

      // as a short name and we put 4 variables in Book we gave it the word struct here we defined 
		  using Book = struct { 
		  string title; 
		  string author; 
		  double price; 
		  int numberOfPages; 
		  }; 
	  
		  // book3 and the third his name is book2 the second his name, the first book1 his name, the book here by creating three objects from 
		  Book book1, book2, book3;