////////Java Program
import java.io.*;
import java.util.*;
interface shape
{
void getdata();
void area();
}
class triangle implements shape
{
public double x,y;
public void getdata()
{
System.out.println("\n Enter Base and Height of Triangle :"+"\n BAse :");
Scanner sc=new Scanner(System.in);
x=sc.nextDouble();
System.out.println("\n Height :");
y=sc.nextDouble();
}
public void area()
{
System.out.println("\n Area of Triangle is :"+(0.5*x*y));
}
}
class rectangle implements shape
{
public double x,y;
public void getdata()
{
System.out.println("\n Enter Sides of Rectangle :"+"\n Side 1 :");
Scanner sc=new Scanner(System.in);
x=sc.nextDouble();
System.out.println("\n Side 2 :");
y=sc.nextDouble();
}
public void area()
{
System.out.println("\n Area of Rectangle is :"+(x*y));
}
}
class inherit
{
public static void main(String []std)
{
int ch;
do
{
System.out.println("\n Menu 1.Triangle \n 2.Rectangle \n 3.Exit \n Enter your choice :");
Scanner s=new Scanner(System.in);
ch=s.nextInt();
switch(ch)
{
case 1: triangle t1=new triangle();
t1.getdata();
t1.area();
break;
case 2: rectangle r=new rectangle();
r.getdata();
r.area();
break;
case 3: System.exit(1);
}//switch
}while(ch!=3); //do
} //main
}//class
///// C++ Program
#include <iostream>
using namespace std;
class shape {
protected:
int width, height;
public:
shape( int a=0, int b=0)
{
width = a;
height = b;
}
virtual int area()
{
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public shape{
public:
Rectangle( int a=0, int b=0)
{
shape(a, b);
}
int area ()
{
cout << "Rectangle class area :" <<endl;
return (width * height);
}
};
class Triangle: public shape{
public:
Triangle( int a=0, int b=0)
{
shape(a, b);
}
int area ()
{
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
};
// Main function for the program
int main( )
{
shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
// store the address of Rectangle
shape = &rec;
// call rectangle area.
shape->area();
// store the address of Triangle
shape = &tri;
// call triangle area.
shape->area();
return 0;
}
import java.io.*;
import java.util.*;
interface shape
{
void getdata();
void area();
}
class triangle implements shape
{
public double x,y;
public void getdata()
{
System.out.println("\n Enter Base and Height of Triangle :"+"\n BAse :");
Scanner sc=new Scanner(System.in);
x=sc.nextDouble();
System.out.println("\n Height :");
y=sc.nextDouble();
}
public void area()
{
System.out.println("\n Area of Triangle is :"+(0.5*x*y));
}
}
class rectangle implements shape
{
public double x,y;
public void getdata()
{
System.out.println("\n Enter Sides of Rectangle :"+"\n Side 1 :");
Scanner sc=new Scanner(System.in);
x=sc.nextDouble();
System.out.println("\n Side 2 :");
y=sc.nextDouble();
}
public void area()
{
System.out.println("\n Area of Rectangle is :"+(x*y));
}
}
class inherit
{
public static void main(String []std)
{
int ch;
do
{
System.out.println("\n Menu 1.Triangle \n 2.Rectangle \n 3.Exit \n Enter your choice :");
Scanner s=new Scanner(System.in);
ch=s.nextInt();
switch(ch)
{
case 1: triangle t1=new triangle();
t1.getdata();
t1.area();
break;
case 2: rectangle r=new rectangle();
r.getdata();
r.area();
break;
case 3: System.exit(1);
}//switch
}while(ch!=3); //do
} //main
}//class
///// C++ Program
#include <iostream>
using namespace std;
class shape {
protected:
int width, height;
public:
shape( int a=0, int b=0)
{
width = a;
height = b;
}
virtual int area()
{
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public shape{
public:
Rectangle( int a=0, int b=0)
{
shape(a, b);
}
int area ()
{
cout << "Rectangle class area :" <<endl;
return (width * height);
}
};
class Triangle: public shape{
public:
Triangle( int a=0, int b=0)
{
shape(a, b);
}
int area ()
{
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
};
// Main function for the program
int main( )
{
shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
// store the address of Rectangle
shape = &rec;
// call rectangle area.
shape->area();
// store the address of Triangle
shape = &tri;
// call triangle area.
shape->area();
return 0;
}
Thanks of sharing this post…Python is the fastest growing language that helps to get your dream job in a developing area. It says every fundamental in a programming, so if you want to become an expertise in python get some training
ReplyDeleteRegards,
Python Training in Chennai
Great Work..
ReplyDelete