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

Assignment Help:

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


Related Discussions:- C program to find area of rectangle

Explain the ways of initializing the arrays, Various ways of initializing t...

Various ways of initializing the Arrays. - The for loop initializes 10 elements with the value of their index.      void main()     {                 const in

Looping, #questiStarting with a blank solution, write a program to prompt t...

#questiStarting with a blank solution, write a program to prompt the user for an employee number, hourly rate and hours worked. Compute and display the employee number, gross weekl

Program to find a greatest string: c - program , Program to find a Greatest...

Program to find a Greatest String: C - Program: Write a program find largest string by c program. int main( int argc, char *argv[] ) {     if( argc         {

Define register simply with bit fields, Define register with bit fields? ...

Define register with bit fields? We could define register simply with bit fields: struct DISK_REGISTER { unsigned ready:1; unsigned error_occured:1; unsigned disk_spinni

Where are longjmp and setjmp used in c++, Where are longjmp and setjmp used...

Where are longjmp and setjmp used in C++? -Setjmp and longjmp must not be used in C++. - Longjmp jumps out of the function without unwinding stack. This means that local obj

Padovan string, A Padovan string P(n) for a natural number n is defined as:...

A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes string concate

String, A string is said to be "Beautiful"€, if it contains only non repet...

A string is said to be "Beautiful"€, if it contains only non repetitive alphabets

Lennie, Lennie McPherson, proprietor of Lennie''''s Bail Bonds, needs to ca...

Lennie McPherson, proprietor of Lennie''''s Bail Bonds, needs to calculate the amount due for setting the bail. Lennie requires something of value as collateral, and his fee is 10%

Integer parameters, write a static method with one integer parameter, x tha...

write a static method with one integer parameter, x that returns the value of the polynomial 3x(2)- 7x + 2

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd