C program to find area of rectangle, C/C++ Programming

Aim: To implement a program to find area of rectangle, surface area of box and volume of box using virtual functions.

Code:                      

class rect

{

            double l,b;

            public:

                        virtual void getdata();

                        virtual void area();

};

void rect::getdata()

{

            cout<<"Enter the length and breadth of rectangle:\n";

            cin>>l>>b;

}

void rect::area()

{

            cout<<"Area of rectangle = "<

}

class box:public rect

{

            double l,b,h;

            public:

                        void getdata();

                        void area();

                        void volume();

};

void box::getdata()

{

            cout<<"\nEnter the length, breadth and height of the box:\n";

            cin>>l>>b>>h;

}

void box::area()

{

            cout<<"Surface area of the box = "<<2*(l*b+b*h+l*h)<<" sq. units\n";

}

void box::volume()

{

            cout<<"Volume of the box = "<

}

void main()

{

            rect r,*ptr;

            box b;

            clrscr();

            ptr=&r;

            ptr->getdata();

            ptr->area();

            ptr=&b;

            ptr->getdata();

            ptr->area();

            ((box *)ptr)->volume();

            getch();

}

 

Output:

Enter the length and breadth of rectangle:

10

15

Area of rectangle = 150 sq. units

 

Enter the length, breadth and height of the box:

5

12

6

Surface area of the box = 324 sq. units

Volume of the box = 360 cu. units

Posted Date: 9/29/2012 4:15:01 AM | Location : United States







Related Discussions:- C program to find area of rectangle, Assignment Help, Ask Question on C program to find area of rectangle, Get Answer, Expert's Help, C program to find area of rectangle Discussions

Write discussion on C program to find area of rectangle
Your posts are moderated
Related Questions
Project Description: Online game development Online Live Baccarat Game is needed - client program. - server program(IOCP or ect..). - web program(ASP or PHP or etc..

Luminous Jewels - The Polishing Game Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various lum

THEORY OF COMPUTER PROGRAMMING

F u nction Returning Object: This program is like to the previous program except the function returns object.  The main rule to be remembered is the function returning obj

Explain the parts of operators Assignment Operators The equal (=) sign is used for assigning a value to another. The left hand side has to be a variable (lvalue, which exc

make a marksheet of 2 student with 5 subject

Illustrate the Function Definition? The C code that explains what a function does is called the function definition. A function definition has the following form Type fun


Simpson's Rule is a more accurate method of numerical integration than the method described in class. Using Simpson's Rule, the integral of a function f between a and b is approxim

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec