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
(a) Write a procedure called (add x y) that adds two numbers in a recursive manner. Specifically, note that x + y = (x + 1) + (y - 1) and x + 0 = x. (b) Using the substitution m

Presently we are using several spreadsheets to give weekly activity and income reports. Much of the information is being frequently re-keyed into several spreadsheets - we are look

This problem familiarizes you with using random numbers in C++. The program is to compute a good approximation of π using a simulation method called "Monte Carlo". The following fi

The code contains two built-in pitfalls. First one, if it executes in a member function for an static, extern or automatic object, the program will possibly crash as soon as the de

Use of random function: int main(void) {    int i,j;         for(j=0;j       {      // randomize();       for(i=0;i                  printf("%d\n", ran

A: Using the extern "C" linkage specification around the C function declarations. Programmers must know about mangled function names and type-safe linkages. After that they must

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

Project Description: Need to develop an expert advisor base on sthocastic oscilator with my own setting.. Very basic touch line OB/OS only but on my setting n condition Skill

luminous jewel polishing necklace, c/c++ 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