Monday, April 10, 2023

BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 7- Write a C++ program to create a class SHAPE which consists of two virtual functions calculate_area() and calculate_perimeter() to calculate area and perimeter of various figures. Derive three classes SQUARE, RECTANGLE, TRIANGLE from class Shape and Calculate Area and Perimeter of each class separately and display the result.

 

Bharathiar University

Programming in C++ Lab
C++ Program 7
Write a C++ program to create a class SHAPE which consists of two virtual functions calculate_area() and calculate_perimeter() to calculate area and perimeter of various figures. Derive three classes SQUARE, RECTANGLE, TRIANGLE from class Shape and Calculate Area and Perimeter of each class separately and display the result.
In this page

  • C++ Source Code for MS Dos Turbo C++

Source Code for MS Dos Turbo C++
#include <iostream.h>
#include<conio.h>

class Shape
{
  public:
    virtual void calculate_area()
    {
        cout<<"Area: Virtual Function";
    }
    virtual void calculate_perimeter()
    {
        cout<<"Perimeter: Virtual Function";
    }
    
};
class Square : public Shape
{
  public:
  float side;
  Square(float x)
  {
    side=x;
  }
    void calculate_area()
    {
        cout<<"Area of Square with side="<<side<<": ";
        cout<<side*side;
    }
     void calculate_perimeter()
    {
        cout<<"\nPerimeter of Square with side="<<side<<": ";
        cout<<4*side;
    }

};
class Rectangle: public Shape
{
 public:
  float length,breadth;
  Rectangle(float x,float y)
  {
    length=x;
    breadth=y;
  }
    void calculate_area()
    {
        cout<<"Area of Rectangle with length="<<length<<"and breadth="<<breadth<<": ";
        cout<<length*breadth;
    }
     void calculate_perimeter()
    {
        cout<<"\nPerimeter of with length="<<length<<"and breadth="<<breadth<<": ";
        cout<<2*(length+breadth);
    }
};

class Triangle: public Shape
{
public:
  float base, height,side1,side2;
  Triangle(float s1,float s2,float b,float h)
  {
    side1=s1;
    side2=s2;
    base=b;
    height=h;
  }
    void calculate_area()
    {
        cout<<"Area of Triangle with";
        cout<<" side1="<<side1<<" side2="<<side2;
        cout<<" base="<<base<<" height="<<height<<": ";
        cout<<(base*height)/2;
    }
     void calculate_perimeter()
    {
        cout<<"\nPerimeter of Triangle with";
        cout<<" side1="<<side1<<" side2="<<side2;
        cout<<" base="<<base<<" height="<<height<<": ";
        cout<<side1+side2+base+height;
    }
};



int main()
{
   float side,length,breadth,side1,side2,base,height;
   Shape *s;
   clrscr();

   cout<<"\nArea and Perimeter of Shapes using Virtual Funtions"<<endl;
    cout<<"===================================================="<<endl;
   cout<<"Square"<<endl;
   cout<<"Enter side of a square:";
   cin>>side;
   Square sq(side);
   s=&sq;
   s->calculate_area();
   s->calculate_perimeter();

   cout<<"\n\nRectangle"<<endl;
   cout<<"Enter length of rectangle:";
   cin>>length;
   cout<<"Enter breadth of rectangle:";
   cin>>breadth;
   Rectangle r(length,breadth);
   s=&r;
   s->calculate_area();
   s->calculate_perimeter();
        
   cout<<"\n\nTriangle"<<endl;
   cout<<"Enter side1 of Triangle:";
   cin>>side1;
   cout<<"Enter side2 of Triangle:";
   cin>>side2;
   cout<<"Enter base of Triangle:";
   cin>>base;
   cout<<"Enter height of Triangle:";
   cin>>height;
   Triangle t(side1,side2,base,height);
   s=&t;
   s->calculate_area();
   s->calculate_perimeter();  
getch();
 return 0;
}

OUTPUT:


No comments:

Post a Comment

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...