vector of pointers

Started by
2 comments, last by alvaro 1 year ago

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;
}
Advertisement

I am guessing it's waiting for your input (the length of the square, it seems). Until you type that (and answer all the other questions as well), nothing seems to be drawn as far as I can see.

You typically don't ask for what to draw inside the render function. The idea is that you can paint at very high speeds, say 60 times / second for smooth animations. However you can't type that fast.

So move input out of the drawing. First fill the shapes with their sizes, then setup and run rendering of them.

Alberth is probably right. I have to ask, what material are you following to learn how to do this? You seem to be a beginner (that's totally fine), but you have written a program with double inheritance in it (Square inherits from both Rectangle and Shape). I have programmed in C++ for 20 years and I think I've needed double inheritance zero times. You probably want to have Square inherit from Rectangle only. Or you may want to skip inheritance altogether while you learn how to write an OpenGL program.

This topic is closed to new replies.

Advertisement