In c++ create a test program (containing main() ) that defines a Circle object a
ID: 658492 • Letter: I
Question
In c++ create a test program (containing main() ) that defines a Circle object and a Rectangle object.
The program should contain a vector (size 2) of BasicShape pointers. The elements of vector will point to objects of classes Circle and Rectangle.
Loop through the vector to process the BasicShapes polymorphically. For each element of vector, invoke calcArea function and print function.
Test Example
// Fig. 12.17: fig12_17.cpp
// Processing Employee derived-class objects individually
// and polymorphically using dynamic binding.
#include <iostream>
#include <iomanip>
#include <vector>
#include "Employee.h"
#include "SalariedEmployee.h"
#include "CommissionEmployee.h"
#include "BasePlusCommissionEmployee.h"
using namespace std;
void virtualViaPointer( const Employee * const ); // prototype
void virtualViaReference( const Employee & ); // prototype
int main()
{
// set floating-point output formatting
cout << fixed << setprecision( 2 );
//error as abstract
//Employee e( "Adnan", "Khan", "111-11-1111");
// create derived-class objects
SalariedEmployee salariedEmployee(
"John", "Smith", "111-11-1111", 800 );
CommissionEmployee commissionEmployee(
"Sue", "Jones", "333-33-3333", 10000, .06 );
BasePlusCommissionEmployee basePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
cout << "Employees processed individually using static binding: ";
// output each Employee
Explanation / Answer
The following program is similar and easy for circle object.
#include<iostream.h>
#include<conio.h>
// start class
class circle
{
private :
float radius;
float x , y;
public :
void get_radius(float r)
{ radius = r; }
void get_xycoor(float a, float b)
{ x = a;
y = b;
}
void area( )
{
cout<< " The area of the circle of radius "<< radius <<" is " << 3.14 * radius * radius ;
}
void circum()
{
cout<< " The circumference of the circle of radius "<< radius <<" is " << 2 *3.14 * radius ;
}
};
//End of class
void main()
{
circle c1;
float rad;
cout<< " Enter radius :";
cin >> rad;
c1.get_radius(rad);
c1.area();
c1.circum();
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.