Vous êtes sur la page 1sur 5

STRUCTURE

It is the user defined derived data type. A Structure is the collection of variables that may be of similar or
dissimilar type. It is basically used to collect the different data types or items referenced under the same
name.
In case of Structure memory is allocated separately for the individual members.
Syntax - struct < tagname >
{
<number Var .1 declaration>;
-------------------------------
-------------------------------
};
Q. WAP to define a structure for the student and then keep the records.

#include<iostream.h>
#include<conio.h>
struct student
{
int roll;
char name[10];
float marks;
} S;
void main( )
{
student P={7, “Ram”, 78.9};
student Q;
Q.roll=5;
Q.marks=90.57;
strcpy(Q.name, “Anu”);
cout<<”\n Enter the Roll, Name & Marks:”;
cin>>S.roll>>S.name>>S.marks;
cout<<”\nStudent 1 :”;
cout<<”\n Roll=”<<P.roll<<”\tName=”<<P.name<<”\tMarks=”<<P.marks;
cout<<”\n Student 2:”;
cout<<”\n Roll=”<<Q.roll<<”\tName=”<<Q.name<<”\tMarks=”<<Q.marks;
cout<<”\n Student 3:”;
cout<<”\n Roll=”<<S.roll<<”\tName=”<<S.name<<”\tMarks=”<<S.marks;
getch( );
}
STRUCTURE & ARRAY

Q. WAP to define a structure for the student and then keep the records of 5
students.

#include<iostream.h>
#include<conio.h>
struct student
{
int roll;
char name[10];
float marks;
};
void main( )
{
student S[5];
int i;
cout<<”\n Input Details:”;
for(i=0; i<5; i++)
{
cout<<”\n Student “<<i+1<<”-:”;
cout<<”\n Enter the Roll, Name & Marks:”;
cin>>S[i].roll>>S[i].name>>S[i].marks;
}
cout<<”\nOutput Details:”;
for(i=0; i<5; i++)
{
cout<<”\nStudent”<<i+1<<”-:”;
cout<<”\n Roll=”<<S[i].roll<<”\tName=”<<S[i].name<<”\tMarks=”<<S[i].marks;
}
getch();
}
STRUCTURE & FUNCTION
Q. WAP to define a structure for the complex no. and then using a function add
the two complex no’s.

#include<iostream.h>
#include<conio.h>
struct complex
{
int Real, imag;
}
complex Add(complex P,complex Q );
void main( )
{
complex A,B,C;
cout<<”\n Enter the first complex no:”;
cin>>A.Real>>A.imag;
cout<<”\n Enter the Second complex no:”;
cin>>B.Real>>B.imag;
C=Add(A,B);
cout<<”\n Resultant complex no:”<<C.Real<<”+I”<<C.imag;
getch( );
}
complex Add(complex P, complex Q)
{
complex R;
R.Real=P.Real + Q.Real;
R.imag= P.imag + Q.imag;
retun R;
}

NESTING OF STURCTURE

Q. WAP to define a structure for the employee and then keep the records.
#include<iostream.h>
#include<conio.h>
struct employee
{
int ID;
char name[20];
float sal, total;
struct
{
float TA,DA;
}allowance;
};
void main( )
{
employee E;
float T,D;
cout<<”\n Enter ID, name & Salary: “;
cin>>E.ID>>E.name>>E.sal;
T = E.allowance.TA = E.sal*0.1;
D=E.allowance.DA = E.sal*0.05;
E.total = E.sal+T +D;
cout<<”\n Employee Details :”;
cout<<”\n ID : “<<E.ID<<”\tName :”<<E.name<<”\tSalary :”<<E.sal;
cout<<”\n TA :”<<T<<”\t DA:”<<D<<”\n Total Salary:”<<E.total;
getch( );
}

UNION
It is also the user defined derived datatype. Which is same as structure i.e.it is also the collection of
variable that may be of similar or dissimilar type. But it differs from the structure in case of memory
allocation.
In case of structure memory is allocated separately for the individual members but in case of union the
same memory block is shared by all the members and its size depends upon the member which needs
maximum memory.
Syntax –
union <tagneme>
{
<number of variables>;
----------------------------
----------------------------
};
DYNAMIC MEMORY ALLOCATION

When the memory is allocated during the run time of the program then it is called as DYNAMIC
MEMORY ALLOCATION.
The ‘new’ operator is used to allocate memory of the specified type dynamically and return the address
of this newly created block to a pointer i.e.it sets a pointer to the newly created block.
The ‘delete’ operator is used to free or release the memory allocated by the new operator and if this
dynamically created block is not released then it is called as memory leakage.

Q. WAP to show the working of new and delete operator.

#include<iostream.h>
#include<conio.h>
Void main( )
{
int *p;
p=new int;
cout<<”\n enter the no :”;
cin>>*p;
cout<<”\n Square of” <<*p <<”=” <<(*p)*(*p);
delete p;
}

Vous aimerez peut-être aussi