Wednesday, December 17, 2014

Array of Object and Function Overloading and Operator Overloading in C++



Array of Object
         
          #include
          #include
          class person
          {
          char name[10];
          int age;
          public:
          void getperson(void)
          {
          cout<<"Enter the name and age\n";
          cin>>name>>age;
          }
          void dispersion(void)
          {
          cout<<"\n"<
          }
          };
          void main()
{
person ps[10];
int n,i;
clrscr();
cout<<"Enter how many person\n";
cin>>n;
cout<<"Enter"<
for(i=0;i
{
ps[i].getperson();
}
cout<<"\n**************************************";
cout<<"\n Name \t Age";
cout<<"\n **************************************";
for(i=0;i
{
ps[i].disperson();
cout<<"\n ***************************************";
getch();
}

Output
         
         Enter how many people?
          2
Enter 2 person details
Enter the name and age
Ragul
23
Enter the name and age
Ravi
25
************************************
Name                    Age
************************************
Ragul                    23
Ravi                     25
***********************************

Function Overloading
          
          #include
          #include
          class areaofshapes
          {
public:
void area(int a)
{
cout<<"Area of square: "<<(a*a);
}
void area(int l, int b)
{
cout<<"Area of rectangle:"<<(l*b);
}
void area(float r)
{
cout<<"Area of circle: "<<(3.14*r*r);
}
void area(float w, float h)
{
cout<<" Area of cylinder         : "(3.14*w*w*h);
}
};
void main()
{
int ch;
areaofshapes k;
clrscr();
cout<<"1. Square \n 2. Rectangle \n 3. Circle\n 4. Cylinder\n";
cout<<"Enter ur choice\n";
cin>>ch;
switch()
{
case 1:
int a;
cout<<"Enter side of square";
cin>>a;
k.area(a);
break;
case 2;
int l,b;
cout<<" Enter the length and breadth of rectangle";
cin>>l>>b;
k.area(l,b);
break;
case 3:
float r;
cout<<" Enter the radius circle";
cin>>r;
k.area(r);
break;
case 4:
float w,h;
cout<<"Enter the width and height of cylender";
cin>>w>>h;
k.area(w,h);
break;
default:
cout<<" Enter right choie, Thank You";
}
getch();
}

Output

1.     Square
2.     Rectangle
3.     Circle
4.     Cylinder
Enter your choice
2
Enter the length and breadth of rectangle
10
5
Area of rectangle  :         50

Operator Overloading
         
          #include
          #include
          class s
          int a,b;
          public:
          s()
          {
          a=10;
          b=30;
          }
          void operator-()
          {
          a=-a;
          b=-a;
          }
          void out()
{
cout<
}
};
void main()
{
clrscr();
s s1;
s1;
s1.out();
-s1;
s1.out();
getch();
}

Output
          
            10               30
          -10              -30

No comments:

Post a Comment