Tuesday, December 9, 2014

C++ Excellant pro



Provides an example to return an object from a function in C++ Programming

#include
#include
class data1
{
    public:
        int a,b;
        void get1();
};
class data2
{
    public:
        int c,d;
        void get2();
};
class data3
{
    int x,y;
    void show();
    public:
        void sum(data1,data2);
};
int main()
{
    clrscr();
    data1 d1;
    data2 d2;
    data3 d3;
    d1.get1();
    d2.get2();
    d3.sum(d1,d2);
}
void data1::get1()
{
    cout<<"Enter for a: ";
    cin>>a;
    cout<<"Enter for b: ";
    cin>>b;
}
void data2::get2()
{
    cout<<"Enter for c: ";
    cin>>c;
    cout<<"Enter for d: ";
    cin>>d;
}
void data3::sum(data1 a1,data2 a2)
{
    x=a1.a+a2.c;
    y=a1.b+a2.d;
    show();
}
void data3::show()
{
    cout<<"X= "<
    cout<<"Y= "<
}

Passing objects to function using call by reference method in C++

#include
#include
class data2;
class data1
{
    int a;
    public:
    void get(int no)
    {
        a=no;
    }
    friend void swap(data1 &, data2 &);
    void show()
    {
        cout<<"a= "<
    }
};
class data2
{
    int b;
    public:
    void get(int no)
    {
        b=no;
    }
    friend void swap(data1 &,data2 &);
    void show()
    {
        cout<<"b= "<
    }
};
void swap(data1 &a1,data2 &a2)
{
    int temp;
    temp=a1.a;
    a1.a=a2.b;
    a2.b=temp;
}
int main()
{
    data1 a;
    data2 b;
    a.get(10);
    b.get(20);
    cout<<"\n\n Before swapping \n\n";
    a.show();
    b.show();
    swap(a,b);
    cout<<"\n\n After swapping\n\n";
    a.show();
    b.show();
}

Read marks of 10 students for 4 subjects and compute and display total marks and status of each student in C++ Programming

 #include
 #include
 #include
 float total_marks(float []);
 int main()
    {
       clrscr();
       float marks[10][4]={0};
       cout<<"\n Enter the marks of the ten students : "<
       for(int count_1=0;count_1<10 count_1="" span="">
      {
         cout<<"\n Enter the marks obtained by student "<
           " :"<
         cout<<"\t Subject-1 = ";
         cin>>marks[count_1][0];
         cout<<"\t Subject-2 = ";
         cin>>marks[count_1][1];
         cout<<"\t Subject-3 = ";
         cin>>marks[count_1][2];
         cout<<"\t Subject-4 = ";
         cin>>marks[count_1][3];
      }
       getch();
       clrscr();
       cout<<"\n *********************************  Result Sheet  *****************************"<
       cout<<"\n Student No.  Subject-1  Subject-2  Subject-3  Subject-4  ";
       cout<<"Total Marks  Status\n"<
       for(int count_2=0;count_2<10 count_2="" span="">
      {
         cout<
         cout<
         cout<
         cout<
         cout<
         cout<
      cout<=200)?"Pass":"Fail");
         cout<
      }
          cout<<"\n****************************************************";
       getch();
       return 0;
    }
/*************************************************************************///-----------------------  total_marks(float [])  ---------------------///*************************************************************************/
float total_marks(float marks[])
    {
       float sum=0;
       for(int count=0;count<4 count="" span="">
      sum+=marks[count];
       return sum;
    }

No comments:

Post a Comment