well when I code the shap[0]→area() it gives a white screen and it locks up I am unsure of how to proceed further.
I am working a little drawing program.
#include <stdlib.h>
#include <glut.h>
#include <iostream>
#include <vector>
using namespace std;
class Shape
{
public:
Shape() {}
virtual double area()=0;
// virtual void plot()=0;
private:
};
class Rectangle : public Shape
{
public:
virtual double area();
// virtual void plot();
private:
};
double Rectangle::area()
{
int len, wid;
cout << "Enter length: ";
cin >> len;
cout << endl;
cout << "Enter width: ";
cin >> wid;
cout << endl;
return len * wid;
}
class Triangle: public Shape
{
public:
virtual double area();
// virtual void plot();
private:
};
double Triangle::area()
{
int len, wid;
cout << "Enter length: ";
cin >> len;
cout << endl;
cout << "Enter width: ";
cin >> wid;
cout << endl;
return 0.5*len * wid;
}
class Square: public Rectangle , public Shape
{
public:
virtual double area();
// virtual void plot();
private:
};
double Square::area()
{
int len;
cout << "Enter length: ";
cin >> len;
cout << endl;
return len * len;
}
void draw()
{
glBegin(GL_LINE_LOOP);
glVertex2i(-120, 90);
glVertex2i(-100, 90);
glVertex2i(-100, 75);
glVertex2i(-120, 75);
glVertex2i(-120, 90);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2i(-120, 90);
glVertex2i(-100, 90);
glVertex2i(-100, -20);
glVertex2i(-120, -20);
glVertex2i(-120, 90);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2i(-115, 85);
glVertex2i(-105, 85);
glVertex2i(-105, 80);
glVertex2i(-115, 80);
glVertex2i(-115, 85);
glEnd();
}
void renderScene()
{
vector <Shape*> shap;
shap.push_back(new Rectangle());
shap.push_back(new Triangle());
shap.push_back(new Square());
glClear(GL_COLOR_BUFFER_BIT);
shap[0]->area();
// shap[0]->plot();
// shap[1]->area();
// shap[1]->plot();
// shap[2]->area();
// shap[2]->plot();
draw();
glFlush();
}
void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat aspectRatio;
if (h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio = (GLfloat)w / (GLfloat)h;
if (w <= h)
glOrtho(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
else
glOrtho(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(0, 0);
glutInitWindowSize(800, 600);
glutCreateWindow("Car");
glutDisplayFunc(renderScene);
glutReshapeFunc(ChangeSize);
glutMainLoop();
return 0;
}