Table of Contents
Back Button
Back to Chapter
C++ Setting Up
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.

C++ structures

Consider we want to make a program to store the database of the students of a school. For each student we have data like name class, section age and roll number. If we were to use the procedural approach this program would be very big and confusing hence we make use of structures.

A structure is a user-defined data type which is created using primitive and derived data types. It is basically a group of data members enclosed by parenthesis.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/03fdf01d-23c9-4cc5-98e4-9291f337723c/Untitled_Diagram_(18).png

spot the mistake in the given code

In a structure all members are public by default, which means we can access these members from outside the structure using a variable of the "structName" data type.

Accessing the members is done by declaring a variable whose data-type is the name of the structure.

Consider the following code.

#include using namespace std; struct student { int rollno; int Class; char name[15]; };//structure definition should always end with a semicolon ";". main() { student a;//creating a variable of data type student a.rollno=15;//assigning value to the memebers of a //structure is done outside the structure. a.Class=5; a.name="raj"; }
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6b5cae75-b01d-497f-b16a-ad253fdf75db/pseud.png

A structure definition should always end with a semicolon.

As we have already learned in the chapter data types a structure is a collection of data members which define a particular thing like a car or student or anything in the real world.

Now based on Our knowledge of structures lets create an application to store data-base of patients in a hospital. The application should accept patient details like patient ID, Name and disease.

We also display this data.

#include using namespace std; struct patient { int pId; char name[15]; char disease[20]; }; main() { patient a; cout<<"enter the Patient ID:"; cin>>a.pId; cout<<"enter the Patient Name:"; cin>>a.name; cout<<"enter the disease:"; cin>>a.disease; cout<<"Patient\tPatient Name\tDiseasel\n"; cout<
Output: enter the Patient ID:10 enter the Patient Name:leo enter the disease:pneumonia Patient Patient Name Diseasel 10 leo pneumonia
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/25903a29-0fef-4949-aafe-aafbc4ab0212/ok.png

We can also use typedef to give a different name to our structure and use it throughout the program.

There are 2 methods in which we can do this:

  1. We can write a typedef statement after the structure as shown below.
#include using namespace std; struct square { int lengtht; int breadth; }; typedef struct square sq; main() { sq a; square b; a.lenght =10; b.breadth= 5; cout<<"length of a:"<
Output: length of a:10 breadth of b:5

In the above example "sq" and "square" refer to the same structure.

  1. Another way to perform this is to write typedef before struct i.e. we include typedef before defining the structure and after defining the structure before the semi-colon we write the desired alternate name for the structure.

Syntax

typedef struct square { int lenght; int breadth; }sq;
Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?