Search This Blog

Showing posts with label practicallist(IT). Show all posts
Showing posts with label practicallist(IT). Show all posts

Sunday, 28 February 2016

Operator Overloading ( == )

Practical-16

Define a circle class with radius as data member, Add necessary constructors and member function to compute area of circle. Class should overload the = = operator to compare two circle objects whether they are equal in radius. Demonstrate its use in main().

#include<iostream>
#include<math.h>
using namespace std;
class circle
{
  int radius;
  float area;
  public:
  circle(int);
  void display();
 int operator==(circle c1);
 };
int circle::operator==(circle c1)
{
  if(radius==c1.radius)
  return 1;
  else
  return 0;
}
circle::circle(int radius)
{
cout<<"radius="<<radius<<endl;
      area=M_PI*(radius*radius);
}
void circle :: display()
{

  cout<<"area="<<area<<endl;
}
int main()
{
  circle c1(2);
  c1.display();
  circle c2(2);
  c2.display();
     int x;
     x=(c1==c2);
      if(x==1)
          cout<<"they are equal in radius";
      else
          cout<<"not equal";
  return 0;
}

Multilevel Inheritance

Practical-20

Define a class student having roll_number as data member and two functions get_number() and put_number(). Derive a class test from class student having marks of two subjects and two member functions two get and display marks. Derive a class result from class test having total_marks as data member and a display function.

#include<iostream>
using namespace std;
class student
{
protected:
    int roll_number;
public:
    void get_number();
    void put_number();
};
void student::get_number()
{
    cout<<"Enter roll number";
    cin>>roll_number;
}
void student::put_number()
{
    cout<<"Roll_number="<<roll_number<<"\n";
}
class test:public student
{
protected:
    int mark[2],i;
public:
    void get_marks();
    void put_marks();
};
void test::get_marks()
{
    for(i=0;i<2;i++)
    {
        cout<<"Enter marks of subject "<<i+1<<" : ";
        cin>>mark[i];
    }
}
void test::put_marks()
{
    for(i=0;i<2;i++)
    {
        cout<<"marks of subject "<<i+1<<" : "<<mark[i]<<"\n";
    }
}
class result:public test
{
    int total_marks=0;
public:
    void total();
};
void result::total()
{
    for(i=0;i<2;i++)
    {
        total_marks=total_marks+mark[i];
    }
    cout<<"total_marks="<<total_marks;
}
int main()
{
    result r;
    r.get_number();
    r.put_number();
    r.get_marks();
    r.put_marks();
    r.total();
}


Single Inheritance

Practical-19

Assume that circle is defined using radius and cylinder is defined using radius and height. Write a circle class as base class and inherit the cylinder class from it. Develop classes such that user can compute the area of circle objects and volume of cylinder objects. Area of circle is radius*radius, while volume of cylinder is pie*(radius) ^ 2*height.


#include<iostream>
#include<math.h>
using namespace std;
class circle
{
protected:
  int radius;
  int height;
public:
  void getdata();
 };
 void circle::getdata()
 {
     cout<<"Enter radius";
     cin>>radius;
     cout<<"Enter height";
     cin>>height;
 }
class cylinder:public circle
{
    float volume;
    float area;
    public:
    void volume1();
    void display();
};
void cylinder::volume1()
{
    cout<<radius<<height;
    area=radius*radius;
    volume=(M_PI*(area)*height);
}
void cylinder :: display()
{
    cout<<"area="<<area<<"\n";
    cout<<"Volume="<<volume<<"\n";
}
int main()
{
    cylinder c2;
    c2.getdata();
    c2.volume1();
    c2.display();
    return 0;
}