Search This Blog

Sunday, December 29, 2013

Static data type function

#include <iostream>
using namespace std;
class student
{
    char name[30];
    int roll;
    int marks;
    static int batch;
    static char faculty[50];
public:
    void input();
    void display();

};
int student::batch=2069;
char student::faculty[50]="electronics and communication";
void student::input()
{
    cout<<endl<<"please enter your name  ";
    cin>>name;
    cout<<endl<<"please enter you roll  ";
    cin>>roll;
    cout<<endl<<"please emter your mark  ";
    cin>>marks;
}
void student::display()
{
    cout<<endl<<name;
    cout<<endl<<roll;
    cout<<endl<<marks;
    cout<<endl<<batch;
    cout<<endl<<faculty;
}
int main()
{
    student s[5];
    cout<<endl<<"STUDENT RECORDS"<<endl;
    for(int i=0;i<5;i++)
    {
        s[i].input();
    }
    for(int i=0;i<5;i++)
    {
        s[i].display();
    }
    return 0;
}

Working of friend function

#include <iostream>
using namespace std;
class array
{
    int a[10];
public:
    void input();
    friend float average(array);
};
void array::input()
{
    for(int i=0;i<10;i++)
    {
        cin>>a[i];
    }
}
float average(array a1)
{
    float sum=0;
    for(int i=0;i<10;i++)
    {
        sum=sum+a1.a[i];
    }
    return (sum/10);
}
int main()
{
    array a1;
    cout<<endl<<"enter 10 elements of an array";
    a1.input();
    float avg;
    avg=average(a1);
    cout<<endl<<avg;
    return 0;
}