Structures in C++: February 2021
Structures in C++: February 2021
net/publication/348960526
Structures in C++
CITATIONS READS
0 4,982
1 author:
Tarfa Hamed
University of Mosul
38 PUBLICATIONS 202 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Tarfa Hamed on 02 February 2021.
C++ Structures
Defining a Structure
1
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
struct Library {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
#include <iostream>
#include <cstring>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
2
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
3
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
#include <iostream>
#include <cstring>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
• When the above code is compiled and executed, it produces the
following result:
4
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Pointers to Structures
struct_pointer = &Book1;
struct_pointer->title;
struct Books {
char title[50];
5
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// Book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// Book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
6
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
int main()
{
Complex z1, z2, z3;
cout<<endl;
z3 = add_complex( z1, z2 );
//show the sum
cout<< z3.real<< "\t"<<z3.img ;
return 0;
}
7
Department of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq
Array of structures
int main(){
int i;