Pages

Sunday, 27 October 2013

Structures in C++

Structures:
The collection of dissimilar data types is known as Structures.
It is used to store Student data, employee data, voter data...etc.
Structure Syntax:                                             Ex:

The variable of a structure is called as members of structure or elements of a structure.
The members of a structure can not access out side of the structure directly .it will access only with help of struct type variable.

Note:
In C functions are not allowed inside the structure but in C++ functions are allowed inside the structure.
Syntax for  Struct type variable :
Struct  structname structtypevar1, structtypevar1,...etc.
Ex: struct  employee e1,e2...etc;
 Syntax for calling members of a variable:
Structtypevariable.elementname;
Ex: e1.eno, e1.ename...etc;

/*Write a C++  program to demonstrate Structure  */                 
/*Write a C++  program to demonstrate Structure and accept the data at runtime  */           
#include<iostream.h>
#include<conio.h>
struct student
{
int sno;
char sname[20];
int m1,m2,m3,tot;
float avg;
};
void main()
{
struct student s={11,"sri”,56,32,51};
clrscr( );
s.tot=s.m1+s.m2+s.m3;
s.avg=s.tot/3;
cout<<"\nsno=“<<s.sno;
cout<<"\nsname=”<<s.sname;
cout<<"\nm1=“<<s.m1;
cout<<"\nm2=“<<s.m2;
cout<<"\nm3=“<<s.m3;
cout<<"\ntotal=“<<s.tot;
cout<<"\navg=”<<s.avg;
getch( );
}
#include<iostream.h>
#include<conio.h>
struct student
{
int sno;
char sname[20];
int m1,m2,m3,tot;
float avg;
};
void main()
{
struct student s;
clrscr( );
cout<<"Enter sno,sname,m1,m2,m3:";
cin>>s.sno>>s.sname>>s.m1>>s.m2>>s.m3;
s.tot=s.m1+s.m2+s.m3;
s.avg=s.tot/3;
cout<<"\nsno=“<<s.sno;
cout<<"\nsname=”<<s.sname;
cout<<"\nm1=“<<s.m1;
cout<<"\nm2=“<<s.m2;
cout<<"\nm3=“<<s.m3;
cout<<"\ntotal=“<<s.tot;
cout<<"\navg=”<<s.avg;
getch( );
}


Result:

Output:

sno=11
sname=sri
m1=56
m2=32
m3=51
total=139
avg=46.00


Result:
Input:

Enter sno,sname,m1,m2,m3:1
abcd
89
65
41
Output:

sno=1
sname=abcd
m1=89
m2=65
m3=41
total=195
avg=65.00
/*Write a C++  program to accept 2
students data at runtime using
Structure  */
/*Write a C++  program to accept n
students data at runtime using Structure
*/

Or

/*Write a C++  program to demonstrate
Structure using arrays  */
#include<iostream.h>
#include<conio.h>
struct student
{
int sno;
char sname[20];
int m1,m2,m3,tot;
float avg;
};
void main()
{
struct student s1,s2;
clrscr( );
cout<<"\nEnter student 1 details:";
cout<<"\nEnter sno,sname,m1,m2,m3:";
cin>>s1.sno>>s1.sname>>s1.m1>>s1.m2>>s1.m3;
s1.tot=s1.m1+s1.m2+s1.m3;
s1.avg=s1.tot/3;
cout<<"\nEnter student 2 details:";
cout<<"\nEnter sno,sname,m1,m2,m3:";
cin>>s2.sno,s2.sname>>s2.m1>>s2.m2>>s2.m3;
s2.tot=s2.m1+s2.m2+s2.m3;
s2.avg=s2.tot/3;
cout<<"\nStudent 1 details:";
cout<<"\nsno=“<<s1.sno;
cout<<"\nsname=”<<s1.sname;
cout<<"\nm1=“<<s1.m1;
cout<<"\nm2=“<<s1.m2;
cout<<"\nm3=“<<s1.m3;
cout<<"\ntotal=“<<s1.tot;
cout<<"\navg=”<<s1.avg;
cout<<"\nStudent 2 details:";
cout<<"\nsno=“<<s2.sno;
cout<<"\nsname=”<<s2.sname;
cout<<"\nm1=“<<s2.m1;
cout<<"\nm2=“<<s2.m2;
cout<<"\nm3=“<<s2.m3;
cout<<"\ntotal=“<<s2.tot;
cout<<"\navg=”<<s2.avg;
getch( );
}
#include<iostream.h>
#include<conio.h>
struct student
{
int sno;
char sname[20];
int m1,m2,m3,tot;
float avg;
};
void main()
{
struct student s[20];
int i,n;
clrscr( );
cout<<"\nEnter the no of student records:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter student” <<(i+1)<<”  details:”;
cout<<"\nEnter sno,sname,m1,m2,m3:";
cin>>s[i].sno>>s[i].sname>>s[i].m1>>s[i].m2>>s[i].m3;
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].tot/3;
}
for(i=0;i<n;i++)
{
cout<<"\n student” <<(i+1)<<”  details”;
cout<<"\nsno=“<<s[i].sno;
cout<<"\nsname=”<<s[i].sname;
cout<<"\nm1=“<<s[i].m1;
cout<<"\nm2=“<<s[i].m2;
cout<<"\nm3=“<<s[i].m3;
cout<<"\ntotal=“<<s[i].tot;
cout<<"\navg=”<<s[i].avg;
}
getch( );
}


Result:
Input:

Enter student 1 detailes:
Enter sno,sname,m1,m2,m3:1
a
52
63
45

Enter student 2 detailes:
Enter sno,sname,m1,m2,m3:2
b
65
32
45

Output:
Student 1 details:
sno=1
sname=a
m1=52
m2=63
m3=45
total=160
avg=53.00
Student 2 details:
sno=2
sname=b
m1=65
m2=32
m3=45
total=142
avg=47.00

Result:
Input:


Enter the no of student records:1

Enter student 1 details:
Enter sno,sname,m1,m2,m3:1
xyz
56
85
96
Output:
student 1 details:
sno=1
sname=xyz
m1=56
m2=85
m3=96
total=237
avg=79.00



Functions in c++

A function is a sub program which performs a specific task.
                                         Or
A block of code is written outside the main.
Advantages of using functions:
Size of the program reduces
It occupies less memory
Repetition of the data will not be there because once a function is defined it can be called any number of times.
Separation of a code will be there
Easy to understand
Easy to fine errors
Execution of the program the program will be fast. 
Types of functions:
C++ is supported to following types of Functions
Predefined Functions
User defined functions
Predefined Functions:
The functions which are already defined c language.
Ex: sqrt(),pow().cos()…..etc.
User defined functions:
The functions which are defined by the user are called as user defined functions.
The contents of this function can be change when ever the user wants.
Syntax:
Returntype  functionname (argument_list)
{
}
Return type:

It represents the type of the value function is going to return.

If return type is int function will return integer value.

If return type is float function will return float value.

If return type is void it represents that function will not return any value.

Return statement is used for returning a value to the caller.

Syntax:

Return value;

Note: at a time a function can return only one value.

Function Name:

It can be any valid name.

Argument list:

It represents the type of the arguments and the no. of arguments the function takes.

Body of the Function:

The content of the function is called as body of the function.

Types of functions based on arguments list and return type.

C++- Supports 4 types functions based on argument list and return type.
  1. Function with no arguments and no return value.
  2. Function with arguments and no return value.
  3. Function with arguments and with return value.
  4. Function with no arguments and with return value. 
Function with no arguments and no return value
Function with arguments and no return value
Function with arguments and with return value
Function with no arguments and with return value
Syntax:
void  fn ( )
{
}
Syntax:
void  fn (al)
{
}
Syntax:
rt  fn (al)
{
}
Syntax:
rt  fn ( )
{
}
rt: return type , fn :function name , al: arguments list
// write a program to find sum of two numbers
#include<iostream.h>
#include<conio.h>
void sum ();
void main ();
{
clrscr();
sum();        
getch();
}
void sum()
{
int a,b,c;
cout<<“\nEnter the values of a,b:”;
cin>> a>>b;
c=a+b;
cout<<“\nsum=”<<c;
}


Result:

Input:

Enter the values of a,b: 2   3

Output:

Sum=5
#include<iostream.h>
#include<conio.h>
void sum (int a, int b);
void main ();
{
int x,y;
clrscr();
cout<<“\nEnter the values of x,y:”;
cin>> x>>y;
sum(x,y);
getch();
}
void sum(int a, int b)
{
int c;
c=a+b;
cout<<“\nsum=”<<c;
}

Result:

Input:

Enter the values of a,b: 2   3

Output:

Sum=5
#include<iostream.h>
#include<conio.h>
int sum (int a, int b);
void main ();
{
int x,y;
clrscr();
cout<<“\nEnter the values of x,y:”;
cin>> x>>y;
rv=sum(x,y);
cout<<“\nsum=”<<rv;
getch();
}
int sum(int a, int b)
{
int c;
c=a+b;
return c;
}
Result:

Input:

Enter the values of a,b: 2   3

Output:

Sum=5
#include<iostream.h>
#include<conio.h>
int sum ();
void main ();
{
int rv;
clrscr();
rv=sum();
cout<<“\nsum=”<<rv;
getch();
}
int sum()
{
int a,b,c;
cout<<“\nEnter the values of a,b:”;
cin>> a>>b;
c=a+b;
return c;
}
Result:

Input:

Enter the values of a,b: 2   3

Output:

Sum=5
Note:
After the execution of function completes control returns back to the caller.
Note:
//x, y are actual parameters.
 //a, b are formal parameters.

Actual Parameters:
The parameters passed by the user by calling the function are called as actual parameters.

Formal Parameters:
The parameters used in the function definition are called formal parameters

Note:
While calling the function we have to pass the exact no of arguments the function takes.

If we pass less no of arguments then the function takes, it generates an error message two few parameters to call the function.

If we pass more no of arguments then the function takes, it generates an error message extra parameters in calling the function.
Note:
Return statement is used for returning a value.

Syntax:
return value;

After execution of this statement control returns back to the caller.
At a time a function can return only one value.

Note: the variables declared in one function are not visible in other functions so we must declare the variables in which they are used.


//write a program to find maximum of two numbers
#include<iostream.h>
#include<conio.h>
void max ();
void main ();
{
clrscr();
max();        
getch();
}
void max()
{
int a,b;
cout<<“\nEnter the values of a,b:”;
cin>> a>>b;
if(a>b)
{
cout<<“\nmax=”<<a;
}
else
{
cout<<“\nmax=”<<b;
}

}


Result:

Input:

Enter the values of a,b: 23   36

Output:

Max=36
#include<iostream.h>
#include<conio.h>
void max (int a, int b);
void main ();
{
int x,y;
clrscr();
cout<<“\nEnter the values of x,y:”;
cin>> x>>y;
max(x,y);
getch();
}
void max(int a, int b)
{
if(a>b)
{
cout<<“\nmax=”<<a;
}
else
{
cout<<“\nmax=”<<b;
}
}



Result:

Input:

Enter the values of a,b: 23   36

Output:

Max=36
#include<iostream.h>
#include<conio.h>
int max (int a, int b);
void main ();
{
int x,y;
clrscr();
cout<<“\nEnter the values of x,y:”;
cin>> x>>y;
rv=max(x,y);
cout<<“\nmax=”<<rv;
getch();
}
int max(int a, int b)
{
if(a>b)
return a;
else
return b;
}






Result:

Input:

Enter the values of a,b: 23   36

Output:

Max=36
#include<iostream.h>
#include<conio.h>
int max ();
void main ();
{
int rv;
clrscr();
rv=max();
cout<<“\nmax=”<<rv;
getch();
}
int max()
{
int a,b;
cout<<“\nEnter the values of a,b:”;
cin>> a>>b;
if(a>b)
return a;
else
return b;
}





Result:

Input:

Enter the values of a,b: 23   36

Output:

Max=36


Recursive function:

The function called it self is known as Recursive function.

// program
#include<iostream.h>
#include<conio.h>
long int  fact (long int n);
void main ();
{
int n;
clrscr();
cout<<“\nEnter the value of n:”;
cin>> n;
rv=fact(n);
cout<<“\nfact of “<<n<<”=”<<rv;
getch();
}
long int fact(long int n)
{
if(n==0)
return 1;
else
return n*fact(n-1);
}


Result:

Input:

Enter the value of n: 5

Output:

Fact of 5=120
Function with default arguments:

In C while calling the function we have to pass exact elements the function takes where as in C++ it is optional to pass all the arguments if a function is initialized from with default value.

Default value must be initialized from right to left, without initializing the right one we can’t initialize left.

Correct initialization
wrong initialization
sum(int a,int b,int c=1);
sum(int a,int b=1,int c=1);
sum(int a=1,int b=1,int c=1);
sum(int a=1,int b,int c);
sum(int a,int b=1,int c);
sum(int a=1,int b,int c=1);
sum(int a=1,int b=1,int c);


// program
#include<iostream.h>
#include<conio.h>
void sum(int a=1,int b=1,int c=1);
void main()
{
clrscr();
sum(10,20,30);
sum(2,3);
sum(12);
sum();
getch();
}
void sum(int a,int b,int c)
{
cout<<"\n\na="<<a;
cout<<"\nb="<<b;
cout<<"\nc="<<c;
cout<<"\nsum="<<(a+b+c);
}
Result:

Output:

a=10
b=20
c=30
sum=60

a=2
b=3
c=1
sum=6

a=12
b=1
c=1
sum=14

a=1
b=1
c=1
sum=3


Function Overloading:

In C, within one name we have only one function where as in C++ with one name we have any no. of functions varying different types of arguments is called as function overloading.

In this function identified, if the function name is same then the compiler check the no. of arguments.

ex:

sum(int,int,int)
sum(int,int);

If the no. of arguments is also same then it will be check the type of arguments.

ex:

sum(int,int);
sum(float,float);

If the type of arguments is also same then it will be check the order of arguments.

ex:
sum(int,float);
sum(float,int)

If the order of arguments is also same then the compiler displays the error message.

// program
// program
#include<iostream.h>
#include<conio.h>
void sum(int a,int b,int c);
void sum(int a,int b);
void main()
{
clrscr();
sum(20,90,63);
sum(52,23);
getch();
}
void sum(int a,int b,int c)
{
cout<<"\n\na="<<a;
cout<<"\nb="<<b;
cout<<"\nc="<<c;
cout<<"\nsum="<<(a+b+c);
}
void sum(int a,int b)
{
cout<<"\n\na="<<a;
cout<<"\nb="<<b;
cout<<"\nsum="<<(a+b);
}
#include<iostream.h>
#include<conio.h>
void findarea(float s);
void findarea(float l,float b);
void main()
{
clrscr();
findarea(6.3);
findarea(5.2,2.3);
getch();
}
void findarea(float s)
{
cout<<"\n\nside of the square="<<s;
cout<<"\narea of the square="<<(s*s);
}
void findarea(float l,float b)
{
cout<<"\n\nlength of the rectangle="<<l;
cout<<"\n breadth of the rectangle="<<b;
cout<<"\narea of the rectangle="<<(l*b);
}
Result:

Output:

a=20
b=90
c=63
sum=173

a=52
b=23
sum=75
Result:

Output:

side of the square=6.3
area of the square=39.690002

length of the rectangle=5.2
breadth of the rectangle=2.3
area of the rectangle=11.959999



Inline Function:

The function is preceding with key word inline then it is called inline function.

The inline functions are similar to macros which replace code at function call.

The difference is macros can’t handle exception(runtime errors) where as inline functions can handle runtime exceptions.

When the function is small i.e. maximum one or two lines then it can be declared as inline function.

// program
#include<iostream.h>
#include<conio.h>
Inline int sum(int a,int b,int c)
{
return a+b;
}
void main()
{
int a,b,rv;
clrscr();
cout<<”enter the values of a,b:”;
cin>>a>>b;
rv=sum(a,b);
cout<<”\nsum=”<<rv;
getch();
}

Result:

Input:

enter the values of a,b:5  
6

Output:

sum=11