You are on page 1of 11

OOP ASSIGNMENT 3

Submitted by:
Debasis Behuria
R.N:1802030064(EE sec A)
Question 1
#include<iostream>
using namespace std;
class shape
{
public:
double x,y;
void get_data(double m,double n)
{
x=m;y=n;
cout<<"dimension 1 : "<<x<<endl;
cout<<"dimension 2 : "<<y<<endl;
}
virtual void display_area()
{
cout<<"this is the virtual function";
}
};
class triangle: public shape
{
public:
void display_area()
{ cout<<"the area of triangle is "<<0.5*x*y<<endl; }

};
class rectangle: public shape
{
public:
void display_area()
{
cout<<"the area of rectangle is "<<x*y<<endl;
}
};
int main()
{
shape s,*ptr;
triangle t;
rectangle r;
cout<<"Area of triangle : "<<endl;
ptr=&t;
ptr->get_data(2,4);
ptr->display_area();
cout<<endl<<"Area of rectangle : "<<endl;
ptr=&r;
ptr->get_data(2,4);
ptr->display_area();
return 0;
}
OUTPUT OF QUE 1
QUESTION 2
#include<iostream>
using namespace std;
class shape
{
public:
double x,y;
void get_data(double m,double n=0)
{
x=m;
cout<<"dimension 1 : "<<x<<endl;
y=n;
cout<<"dimension 2 : "<<y<<endl;
}
virtual void display_area()
{
cout<<"this is the virtual function";
}
};
class triangle: public shape
{
public:
void display_area()
{
cout<<"the area of triangle is "<<0.5*x*y<<endl;
}

};
class rectangle: public shape
{
public:
void display_area()
{
cout<<"the area of triangle is "<<x*y<<endl;
}

};
class circle: public shape
{
public:
void display_area()
{
cout<<"the area of rectangle is "<<3.14*x*x<<endl;
}

};
int main()
{
shape s,*ptr;
triangle t;
rectangle r;
circle c;
cout<<"Area of triangle : "<<endl;
ptr=&t;
ptr->get_data(2,4);
ptr->display_area();
cout<<endl<<"Area of rectangle : "<<endl;
ptr=&r;
ptr->get_data(2,4);
ptr->display_area();
cout<<endl<<"Area of circle : "<<endl;
ptr=&c;
ptr->get_data(2);
ptr->display_area();
return 0;
}
OUTPUT OF QUE 2
QUESTION 3
#include<iostream>
using namespace std;
class shape
{
public:
double x,y;
void get_data(double m,double n=0)
{
x=m;
cout<<"dimension 1 : "<<x<<endl;
y=n;
cout<<"dimension 2 : "<<y<<endl;
}
virtual void display_area()
{
cout<<"this is the virtual function from base class";
}
};
class triangle: public shape
{
public:
void display_area()
{
cout<<"the area of triangle is "<<0.5*x*y<<endl;
}

};
class rectangle: public shape
{
public:
void display_area()
{
cout<<"the area of triangle is "<<x*y<<endl;
}

};
class circle: public shape
{
public:
//function defination is removed.

/*void display_area()
{
cout<<"the area of rectangle is "<<3.14*x*x<<endl;
}*/

};
int main()
{
shape s,*ptr;
triangle t;
rectangle r;
circle c;
cout<<"Area of triangle : "<<endl;
ptr=&t;
ptr->get_data(2,4);
ptr->display_area();
cout<<endl<<"Area of rectangle : "<<endl;
ptr=&r;
ptr->get_data(2,4);
ptr->display_area();
cout<<endl<<"Area of circle : "<<endl;
ptr=&c;
ptr->get_data(2);
ptr->display_area();
return 0;
}

//comment:
/*when we removed the function defination from any derived class ,then the
display_area() function of that class did not run.
The display_area() virtual function from base class is supposed to be run. And it is
because the pointer fails to find any display_area() function in that derived class
and then it bounds to call the virtual function from base class. */

OUTPUT OF QUE 3

You might also like