SlideShare a Scribd company logo
1 of 9
Work on Static methods OBJECTIVES -continue to practice to
write the code for the data type class -Learn what is static
members in data type class and how to write static methods -
Learn how to access static members of data type class from
main() Provide the application that helps users to calculate the
area of the following list of shapes. Square Rectangle Triangle
Circle Trapezoid Rhombus Kite Parallelogram For each shape,
you can ask for all necessary information of the selected shape
to calculate its area from the keyboard. The result should be
displayed in the following format, for example with selected
shape is Rectangle -Manage the menu with the do., while loop
to redisplayed the menu after finishing one shape -How to write
the switch statement -How to format the output in columns and
with 2 decimal digits -How to create the static method in data
type class -How to access static method of data type class from
main() -The formula to calculate the area of all above shapes -
You have to provide the pseudo code of main() at the top of
driver class or in the word document with party -Create a
project named SU2016LAB6_PART2_YourLastName then add
two classes in -Class SU2016LAB6_ShapeArea_yourLastName
that includes all the static methods that receive the information
input from main(), calculate the area of required shapes and
return its area. -Driver class
SU2016LAB6_AccessStaticMethod_yourLastName: this class
include main() where you can provide the menu to allow users
to select the shapes to calculate the area. For each shape, ask
for and read the necessary information to calculate the area
from the keyboard, then call the static method of class
SU2016LAB6_ShapeArea_yourLastName to pass the
information in to get the area. After that, display the result as
requested format
Solution
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class Shape{ //shape class
public:
virtual double area();
};
class Rectangle : public Shape {//rectangle class
private:
double width, length; //sides
public:
Rectangle(double width, double length) {
this->width = width;
this->length = length;
}
double area() {
// A = w * l
return width * length;
}
};
class Rhombus: public Shape{//rhombus class
private:
double width, length; //2 diagonals
public:
Rhombus(double width, double length) {
this->width = width;
this->length = length;
}
double area() {
return 1/2*width * length;
}
};
class Kite:public Shape{//kite class
private:
double width, length; //2 diagonals
public:
Kite(double width, double length) {
this->width = width;
this->length = length;
}
double area() {
return 1/2*width * length;
}
};
class Parallelogram:public Shape {//parallelogram class
private:double width, length; //sides
public:
Parallelogram(double width, double length) {
this->width = width;
this->length = length;
}
double area() {
return width * length;
}
};
class Circle:public Shape {//circle class
private: double radius;
public:
Circle(double radius) {
this->radius = radius;
}
double area() {
// A = p r^2
return 22/7 * radius*radius;
}
};
class Square:public Shape{//square class
private: double side;
public:
Square(double side) {
this->side = side;
}
double area() {
return side*side;
}
};
class Triangle:public Shape{//triangle class
private: double a, b, c; // sides
public:
Triangle(double a, double b, double c) {
this->a = a;
this->b = b;
this->c = c;
}
double area() {
// Heron's formula:
// A = SquareRoot(s * (s - a) * (s - b) * (s - c))
// where s = (a + b + c) / 2, or 1/2 of the perimeter of the
triangle
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
};
class Trapezoid:public Shape{//trapezoid class
private: double a, b, c; // sides
public:
Trapezoid(double a, double b, double c) {
this->a = a;
this->b = b;
this->c = c;
}
double area() {
return 1/2*(a+b)*c;
}
};
int main() {
// Square test
double side;
cout<<"Enter the side"<<endl;
cin>>side;
Shape square = Square(side);
cout<<"Shape : Square "<<"side = "<<side<<" Area = "
<< square.area()<< " ";
// Rectangle test
double width,length;
cout<<"Enter the width and length"<<endl;
cin>>width>>length;
Shape rectangle = Rectangle(width, length);
cout<<"Shape : Rectangle "<< "Width = "<<width<<"
Length = "<< length
<< " Area = " << rectangle.area()<< " ";
// Triangle test
double a, b, c;
cout<<"enter the sides of th triangle"<<endl;
cin>>a>>b>>c;
Shape triangle = Triangle(a,b,c);
cout<<"Shape : Triangle " << "Side1 = "<<a <<" "<<
"Side2 = " << b<<" Side3 = "<< c
<< " Area = " << triangle.area()<< " ";
// Circle test
double radius;
cout<<"Enter the radius"<<endl;
cin>>radius;
Shape circle = Circle(radius);
cout<<"Shape : Circle " << "Radius = "<<radius << "
Area = " << circle.area()<< " ";
// Trapezoid test
double a1, b1, c1;
cout<<"enter the sides of th trapezoid"<<endl;
cin>>a1>>b1>>c1;
Shape trapezoid =Trapezoid(a,b,c);
cout<<"Shape : Trapezoid "<< "Base1 = "<<a1 <<" Base2
= "<< b1<<" Height = "<< c1
<<" Area = " <<trapezoid.area()<< " ";
// Rhombus test
double diag1,diag2;
cout<<"Enter the diagonals"<<endl;
cin>>diag1>>diag2;
Shape rhombus =Rhombus(diag1, diag2);
cout<<"Shape : Rhombus " << "diagonal1 = "<<diag1<<"
diagonal2 = " << diag2
<< " Area = "<< rhombus.area()<< " ";
// Kite test
double diag3,diag4;
cout<<"Enter the diagonals"<<endl;
cin>>diag1>>diag2;
Shape kite =Kite(diag3, diag4);
cout<<"Shape : Kite "<< "diagonal1 = "<<diag3<<"
diagonal2 = " << diag4
<<" Area = "<< kite.area()<< " ";
// Parallelogram test
double width3, length3;
cout<<"Enter the width and length"<<endl;
cin>>width3>>length3;
Shape parallelogram = Parallelogram(width3, length3);
cout<<"Shape : parallelogram " << "Base = "<<width3
<<" Length = "<< length3
<<" Area = " << parallelogram.area()<< " ";
return 0;
}

More Related Content

Similar to Work on Static methods OBJECTIVES -continue to practice to write the .docx

10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
RihabBENLAMINE
 
3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf
atozshoppe
 
In this project you implement a program such that it simulates the p.pdf
In this project you implement a program such that it simulates the p.pdfIn this project you implement a program such that it simulates the p.pdf
In this project you implement a program such that it simulates the p.pdf
fathimafancy
 
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docxCSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
faithxdunce63732
 
14 class design (1)
14 class design (1)14 class design (1)
14 class design (1)
Satyam Gupta
 

Similar to Work on Static methods OBJECTIVES -continue to practice to write the .docx (11)

10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
 
3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf
 
Abstract Classes Interface Exceptional Handling Java
Abstract Classes Interface Exceptional Handling JavaAbstract Classes Interface Exceptional Handling Java
Abstract Classes Interface Exceptional Handling Java
 
You still work for packaging company that makes boxes and cylindrical.docx
 You still work for packaging company that makes boxes and cylindrical.docx You still work for packaging company that makes boxes and cylindrical.docx
You still work for packaging company that makes boxes and cylindrical.docx
 
In this project you implement a program such that it simulates the p.pdf
In this project you implement a program such that it simulates the p.pdfIn this project you implement a program such that it simulates the p.pdf
In this project you implement a program such that it simulates the p.pdf
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
 
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docxCSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
 
L10
L10L10
L10
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdf
 
14 class design (1)
14 class design (1)14 class design (1)
14 class design (1)
 
14 class design
14 class design14 class design
14 class design
 

More from ajoy21

Please choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docxPlease choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docx
ajoy21
 
Please answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docxPlease answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docx
ajoy21
 
Please answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docxPlease answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docx
ajoy21
 
Please answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxPlease answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docx
ajoy21
 

More from ajoy21 (20)

Please complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docxPlease complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docx
 
Please cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docxPlease cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docx
 
Please choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docxPlease choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docx
 
Please check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docxPlease check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docx
 
Please answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docxPlease answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docx
 
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docxPlease attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
 
Please answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docxPlease answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docx
 
Please answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docxPlease answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docx
 
Please answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docxPlease answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docx
 
Please answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docxPlease answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docx
 
Please answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docxPlease answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docx
 
Please answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docxPlease answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docx
 
Please answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docxPlease answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docx
 
Please answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docxPlease answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docx
 
Please answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docxPlease answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docx
 
Please answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docxPlease answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docx
 
Please answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docxPlease answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docx
 
Please answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docxPlease answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docx
 
Please answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docxPlease answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docx
 
Please answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxPlease answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docx
 

Recently uploaded

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Recently uploaded (20)

An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
Championnat de France de Tennis de table/
Championnat de France de Tennis de table/Championnat de France de Tennis de table/
Championnat de France de Tennis de table/
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 

Work on Static methods OBJECTIVES -continue to practice to write the .docx

  • 1. Work on Static methods OBJECTIVES -continue to practice to write the code for the data type class -Learn what is static members in data type class and how to write static methods - Learn how to access static members of data type class from main() Provide the application that helps users to calculate the area of the following list of shapes. Square Rectangle Triangle Circle Trapezoid Rhombus Kite Parallelogram For each shape, you can ask for all necessary information of the selected shape to calculate its area from the keyboard. The result should be displayed in the following format, for example with selected shape is Rectangle -Manage the menu with the do., while loop to redisplayed the menu after finishing one shape -How to write the switch statement -How to format the output in columns and with 2 decimal digits -How to create the static method in data type class -How to access static method of data type class from main() -The formula to calculate the area of all above shapes - You have to provide the pseudo code of main() at the top of driver class or in the word document with party -Create a project named SU2016LAB6_PART2_YourLastName then add two classes in -Class SU2016LAB6_ShapeArea_yourLastName that includes all the static methods that receive the information input from main(), calculate the area of required shapes and return its area. -Driver class SU2016LAB6_AccessStaticMethod_yourLastName: this class include main() where you can provide the menu to allow users to select the shapes to calculate the area. For each shape, ask for and read the necessary information to calculate the area from the keyboard, then call the static method of class SU2016LAB6_ShapeArea_yourLastName to pass the information in to get the area. After that, display the result as requested format
  • 2. Solution #include <iostream> #include <conio.h> #include <math.h> using namespace std; class Shape{ //shape class public: virtual double area(); }; class Rectangle : public Shape {//rectangle class private: double width, length; //sides public: Rectangle(double width, double length) { this->width = width; this->length = length; } double area() { // A = w * l return width * length; }
  • 3. }; class Rhombus: public Shape{//rhombus class private: double width, length; //2 diagonals public: Rhombus(double width, double length) { this->width = width; this->length = length; } double area() { return 1/2*width * length; } }; class Kite:public Shape{//kite class private: double width, length; //2 diagonals public: Kite(double width, double length) { this->width = width; this->length = length; } double area() { return 1/2*width * length; } };
  • 4. class Parallelogram:public Shape {//parallelogram class private:double width, length; //sides public: Parallelogram(double width, double length) { this->width = width; this->length = length; } double area() { return width * length; } }; class Circle:public Shape {//circle class private: double radius; public: Circle(double radius) { this->radius = radius; } double area() { // A = p r^2 return 22/7 * radius*radius; } }; class Square:public Shape{//square class private: double side; public:
  • 5. Square(double side) { this->side = side; } double area() { return side*side; } }; class Triangle:public Shape{//triangle class private: double a, b, c; // sides public: Triangle(double a, double b, double c) { this->a = a; this->b = b; this->c = c; } double area() { // Heron's formula: // A = SquareRoot(s * (s - a) * (s - b) * (s - c)) // where s = (a + b + c) / 2, or 1/2 of the perimeter of the triangle double s = (a + b + c) / 2; return sqrt(s * (s - a) * (s - b) * (s - c)); } }; class Trapezoid:public Shape{//trapezoid class
  • 6. private: double a, b, c; // sides public: Trapezoid(double a, double b, double c) { this->a = a; this->b = b; this->c = c; } double area() { return 1/2*(a+b)*c; } }; int main() { // Square test double side; cout<<"Enter the side"<<endl; cin>>side; Shape square = Square(side); cout<<"Shape : Square "<<"side = "<<side<<" Area = " << square.area()<< " "; // Rectangle test double width,length; cout<<"Enter the width and length"<<endl; cin>>width>>length;
  • 7. Shape rectangle = Rectangle(width, length); cout<<"Shape : Rectangle "<< "Width = "<<width<<" Length = "<< length << " Area = " << rectangle.area()<< " "; // Triangle test double a, b, c; cout<<"enter the sides of th triangle"<<endl; cin>>a>>b>>c; Shape triangle = Triangle(a,b,c); cout<<"Shape : Triangle " << "Side1 = "<<a <<" "<< "Side2 = " << b<<" Side3 = "<< c << " Area = " << triangle.area()<< " "; // Circle test double radius; cout<<"Enter the radius"<<endl; cin>>radius; Shape circle = Circle(radius); cout<<"Shape : Circle " << "Radius = "<<radius << " Area = " << circle.area()<< " "; // Trapezoid test double a1, b1, c1; cout<<"enter the sides of th trapezoid"<<endl; cin>>a1>>b1>>c1;
  • 8. Shape trapezoid =Trapezoid(a,b,c); cout<<"Shape : Trapezoid "<< "Base1 = "<<a1 <<" Base2 = "<< b1<<" Height = "<< c1 <<" Area = " <<trapezoid.area()<< " "; // Rhombus test double diag1,diag2; cout<<"Enter the diagonals"<<endl; cin>>diag1>>diag2; Shape rhombus =Rhombus(diag1, diag2); cout<<"Shape : Rhombus " << "diagonal1 = "<<diag1<<" diagonal2 = " << diag2 << " Area = "<< rhombus.area()<< " "; // Kite test double diag3,diag4; cout<<"Enter the diagonals"<<endl; cin>>diag1>>diag2; Shape kite =Kite(diag3, diag4); cout<<"Shape : Kite "<< "diagonal1 = "<<diag3<<" diagonal2 = " << diag4 <<" Area = "<< kite.area()<< " "; // Parallelogram test double width3, length3;
  • 9. cout<<"Enter the width and length"<<endl; cin>>width3>>length3; Shape parallelogram = Parallelogram(width3, length3); cout<<"Shape : parallelogram " << "Base = "<<width3 <<" Length = "<< length3 <<" Area = " << parallelogram.area()<< " "; return 0; }