Main PDF Lesson 4
Main PDF Lesson 4
STRUCTURES
LESSON #1
Structures
Upon completion of this subtopic, you will be able to:
• Understand what is a structure
• Learn how to declare and use structures in a program
• A structure is a group of elements which are of
different type. Each of the elements is identified by its
own identifier and they are called member.
Then inside the curly braces, you can declare one or more
members (declare variables inside curly braces) of that
structure
For example:
struct Person
{ char name[50];
int age;
float salary; };
bill.age = 50;
#include <iostream> int main()
using namespace std; {
struct Rectangle struct Rectangle r;
{ r.width=8;
int width, height; r.height=5;
cout<<"Area is: "<<(r.width * r.height)<<endl;
}; return 0;
}
#include <iostream> int main() {
using namespace std; Person p1;
struct Person cout << "Enter Full name: ";
{ cin.get(p1.name, 50);
char name[50]; cout << "Enter age: "; cin >> p1.age;
int age; cout << "Enter salary: "; cin >> p1.salary;
float salary; }; cout << "\nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;
return 0; }
If access specifier is not declared explicitly, then by default
access specifier will be public.
int main()
{
initializeFunction();
return 0;
}
#include <iostream>
using namespace std;
struct number {
int n;
};
int main()
{
initializeFunction();
return 0;
}
https://www.programiz.com/cpp-programming/structure-function
https://codescracker.com/cpp/cpp-pasing-structures-to-functions.htm
https://fresh2refresh.com/c-programming/c-passing-struct-to-function/
https://beginnersbook.com/2017/09/cpp-structure-and-function/