Description
There are 4 classes, Figure is the base class,both Triangle, Rectangle and Circle are all inherited from it. Figure class is like following:
class FIGURE { public: void set_size(double x, double y = 0); virtual double get_area() = 0; protected: double x_size, y_size; };
You should implement Figure, Triangle, Rectange, Circle class.
Output
Area of triangle is 60 Area of rectangle is 120 Area of circle is 706.858
//Figure.h
#ifndef FIGURE_H
#define FIGURE_H
const double PI = 3.14159;
class FIGURE
{
public:
void set_size(double x, double y = 0);
virtual double get_area() = 0;
protected:
double x_size, y_size;
};
#endif
//main.cpp
#include "Figure.h"
#include "Triangle.h"
#include "Rectangel.h"
#include "Circle.h"
#include <iostream>
using namespace std;
int main()
{
TRIANGLE triangle;
RECTANGLE rectangle;
CIRCLE circle;
// 处理三角形
triangle.set_size(15, 8); // 设置三角形的底和高
cout << "Area of triangle is " << triangle.get_area()
<< "\n";
// 处理矩形
rectangle.set_size(15, 8); // 设置矩形的长和宽
cout << "Area of rectangle is " << rectangle.get_area()
<< "\n";
// 处理圆
circle.set_size(15); // 设置圆的半径
cout << "Area of circle is " << circle.get_area()
<< "\n";
return 0;
}
Need to write: Figure.cpp, Triangle.h, Rectangle.h, Circle.h files
Above code can't be changed
Figure.cpp
#include<iostream>
using namespace std;
// parent class figure
class FIGURE {
public:
void set_size(double x, double y = 0);
virtual double get_area() = 0;
protected:
double x_size, y_size; // data members
};
// child class inherited from class Figure
class TRIANGLE:public FIGURE
{
public:
void set_size(double x, double y = 0)
{
x_size=x;
y_size=y;
}
double get_area()
{
return x_size*y_size/2;
}
};
// child class inherited from class Figure
class RECTANGLE:public FIGURE
{
public:
void set_size(double x, double y = 0)
{
x_size=x;
y_size=y;
}
double get_area()
{
return x_size*y_size;
}
};
// child class inherited from class Figure
class CIRCLE:public FIGURE
{
public:
void set_size(double x, double y = 0)
{
x_size=x;
}
double get_area()
{
return 3.14*x_size*x_size;
}
};
int main()
{
// creating object of TRIANGLE class
TRIANGLE triangle;
// creating object of RECTANGLE class
RECTANGLE rectangle;
// creating object of CIRCLE class
CIRCLE circle;
triangle.set_size(15, 8); // 60
cout << "Area of triangle is " <<
triangle.get_area() << "\n";
rectangle.set_size(15, 8); // 120
cout << "Area of rectangle is " <<
rectangle.get_area() << "\n";
circle.set_size(15); // 706.5
cout << "Area of circle is " <<
circle.get_area() << "\n";
return 0;
}
Output
Description There are 4 classes, Figure is the base class,both Triangle, Rectangle and Circle are all...
Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...
/* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) { x=y=0;} shape(int xvalue, int yvalue); void setShape(int new_x, int new_y); void setX(int new_x); void setY(int new_y); int getX( ) const; int getY( ) const; virtual void move(int x, int y) = 0; virtual void shift(int dx, int dy) = 0; virtual void draw( ) = 0; virtual void rotate(double r) = 0; virtual void print(ostream&)const; friend ostream & operator<<(ostream & os, const shape& s);...
java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...
in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base classes. Circle int r; void area(); void perimeter(); void volume(); //No volume for circle Sphere int r; void area();//Sphere surface area= 4 × pi × radius2 void perimeter(); //No perimeter for Sphere void volume();//Sphere volume= 4/3 × pi × radius2 Cylinder int r, height; void area();// Cylinder surface area=perimeter of...
4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a) Run the following code and observe the output. #include <iostream> #include <string> using namespace std; class Vehicle public: void print() { cout << "Print: I am a vehicle. \n"; } void display() { cout << "Display: I am a vehicle. \n"; } }; class Car: public Vehicle { public: void print() { cout << "Print: I am a car.\n"; } void display() { cout...
C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private: string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){ return name; } }; class Circle : public...
Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...
#include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius; // get function for radius public: double getRadius(){ return radius; } // set function for radius void setRadius(double rad){ radius=rad; } // returning area = 3.14159 * radius * radius double getArea(){ return (3.14159 * radius * radius); } }; // Sample run int main() { // Declaring object of Circle Circle myCircle; myCircle.setRadius(5); // printing radius of circle cout<<"Radius of circle is: "<<(myCircle.getRadius())<<endl;...
Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve breaking the problem down into multiple classes that can be tied together into a single solution. In this project, you are given the task of writing some classes that would work together for providing a solution to a problem involving some basic computations. Learning Objectives The focus of this assignment is on the following learning objectives: • Be able to identify the contents of...
I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the program until they enter "q" to quit. However, my program runs through one time, the prompt appears again, but then it terminates before the user is allowed to respond to the prompt again. I'm not able to debug why this...