Wednesday, March 28, 2018

Write a C++ program to illustrate pur virtual function and calculate the area of different shapes by using abstract class.


AIM: Write a C++ program to illustrate pur virtual function and calculate the area of different shapes by using abstract class.
THEORY:
In C++, if a class has at least one pure virtual function, then the class becomes abstract. Unlike C++, in Java, a separate keyword abstract is used to make a class abstract.
// An example abstract class in Java
abstract class Shape {
    int color;

    // An abstract function (like a pure virtual function in C++)
    abstract void draw();
}

SOURCE CODE:
#include"as.h"
#include<iostream>
class square:public A
{
            public:
            int w;
            square(int x)
            {
                        w=2;
                        }         
                        int area()
                        {
                                    return w*w;
                        }
};
class rectangle:public A
{
            public:
            int l,w;
            rectangle(int x,int y)   
            {
                        l=y;
                        w=x;
            }
            int area()
            {
                        return w*l;
            }
};
class cube:public A
{
            public:
            int w;
            cube(int x)      
            {
                        w=x;
            }
            int area()
            {
                        return 6*w*w;
            }
};
class AB:public A
{
           
};
int main()
{
            square *p,s(5);
            p=&s;
            cout<<"\n Area of square:"<<p->area();
            rectangle r(10,3);
            cout<<"\n Area of reactangle:"<<r.area();
            cube c(3);
            cout<<"\n Area of cube:"<<c.area();
            AB a;
            cout<<a.area();
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home