//Coding for Sun

glLoadIdentity();
glBegin(GL_POLYGON);
glColor3ub(255.0f, 255.0f, 128.0f);
float angle10;
for (int i=0;i<360;i++)
{
angle10=i*3.1416/180;
glVertex2f(0.0+0.12*cos(angle10),0.9+0.12*sin(angle10));

}
glEnd();

//Coding for Moon


glLoadIdentity();
glBegin(GL_POLYGON);
glColor3ub(255, 255, 255);
GLfloat x5=.7f;
GLfloat y5=.7f;
GLfloat radius5 =.07f;
GLfloat twicePi5 = 2.0f * PI;
GLfloat triangleAmount5=100;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x5, y5); // center of circle
for(int i = 0; i <= triangleAmount5; i++)
{
glVertex2f(
x5 + (radius5 * cos(i * twicePi5 / triangleAmount5)),
y5 + (radius5 * sin(i * twicePi5 / triangleAmount5))
);
}
glEnd();

//Code for Airplane



glLoadIdentity();


glPushMatrix();
glScalef(0.4,0.4,0);
glTranslatef(0.0f,1.5f, 0.0f);
glTranslatef(position6, 0.0f, 0.0f);

glBegin(GL_POLYGON); // airplane outer body
glColor3ub(234, 229, 229); // white
glVertex2f(-0.3f, 0.2f);
glVertex2f(0.3f, 0.2f);
glVertex2f(0.4f, 0.3f);
glVertex2f(0.45f, 0.4f);
glVertex2f(0.45f, 0.5f);
glVertex2f(0.3f, 0.4f);
glVertex2f(0.0f, 0.4f);
glVertex2f(-0.2f, 0.4f);
glVertex2f(-0.3f, 0.4f);
glVertex2f(-0.35f, 0.38f);
glVertex2f(-0.4f, 0.3f);
glVertex2f(-0.35f, 0.23f);
glVertex2f(-0.3f, 0.2f);
glEnd();
glBegin(GL_POLYGON); // airplane front window
glColor3ub(117, 69, 160); // purple one
glVertex2f(-0.37f, 0.35f);
glVertex2f(-0.32f, 0.35f);
glVertex2f(-0.3f, 0.36f);
glVertex2f(-0.3f, 0.38f);
glVertex2f(-0.35f, 0.38f);
glEnd();
glBegin(GL_LINES); // airplane front window
glColor3ub(255, 255, 255); // white
glVertex2f(-0.35f, 0.35f);
glVertex2f(-0.33f, 0.38f);
glVertex2f(-0.33f, 0.35f);
glVertex2f(-0.31f, 0.38f);
glEnd();
glBegin(GL_LINES); // airplane front door
glColor3ub(144, 126, 181); // purple one
glVertex2f(-0.25f, 0.26f);
glVertex2f(-0.23f, 0.24f);
glVertex2f(-0.23f, 0.24f);
glVertex2f(-0.18f, 0.24f);
glVertex2f(-0.18f, 0.24f);
glVertex2f(-0.16f, 0.26f);
glVertex2f(-0.16f, 0.26f);
glVertex2f(-0.16f, 0.36f);
glVertex2f(-0.16f, 0.36f);
glVertex2f(-0.18f, 0.38f);
glVertex2f(-0.18f, 0.38f);
glVertex2f(-0.23f, 0.38f);
glVertex2f(-0.23f, 0.38f);
glVertex2f(-0.25f, 0.36f);
glVertex2f(-0.25f, 0.36f);
glVertex2f(-0.25f, 0.26f);
glEnd();
glBegin(GL_QUADS); // Airplane front wind
glColor3ub(117, 69, 160); // purple one
glVertex2f(0.02f, 0.15f);
glVertex2f(0.05f, 0.15f);
glVertex2f(-0.03f, 0.26f);
glVertex2f(-0.1f, 0.26f);
glEnd();
glBegin(GL_QUADS); // Airplane back wind
glColor3ub(117, 69, 160); // purple one
glVertex2f(-0.08f, 0.4f);
glVertex2f(0.0f, 0.4f);
glVertex2f(0.07f, 0.49f);
glVertex2f(0.04f, 0.49f);
glEnd();
// Airplane window
glColor3ub(117, 69, 160); // purple one
GLfloat x2=-0.09f;
GLfloat y2=0.35f;
GLfloat radius1 =.02f;
int triangleAmount1 = 100;
GLfloat twicePi1 = 2.0f * PI;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x2, y2); // center of circle
for(int i = 0; i <= triangleAmount1; i++)
{
glVertex2f(
x2 + (radius1 * cos(i * twicePi1 / triangleAmount1)),
y2 + (radius1 * sin(i * twicePi1 / triangleAmount1))
);
}
glEnd();
// Airplane window
glColor3ub(117, 69, 160); // purple one
GLfloat x3=-0.02f;
GLfloat y3=0.35f;
radius1 =.02f;
twicePi1 = 2.0f * PI;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x3, y3); // center of circle
for(int i = 0; i <= triangleAmount1; i++)
{
glVertex2f(
x3 + (radius1 * cos(i * twicePi1 / triangleAmount1)),
y3 + (radius1 * sin(i * twicePi1 / triangleAmount1))
);
}
glEnd();
// Airplane window
glColor3ub(117, 69, 160); // purple one
GLfloat x4=0.06f;
GLfloat y4=0.35f;
radius1 =.02f;
twicePi1 = 2.0f * PI;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x4, y4); // center of circle
for(int i = 0; i <= triangleAmount1; i++)
{
glVertex2f(
x4 + (radius1 * cos(i * twicePi1 / triangleAmount1)),
y4 + (radius1 * sin(i * twicePi1 / triangleAmount1))
);
}
glEnd();

glPopMatrix();

Code for Birds

glLoadIdentity();
glPushMatrix();
glTranslatef(position5,0.0f, 0.0f);
glBegin(GL_POLYGON);
glColor3ub(0.0, 0.0, 0.0);
float angle41;
for (int i=0;i<360;i++)
{
angle41=i*3.1416/180;
glVertex2f(0.8+0.04*cos(angle41),0.7+0.02*sin(angle41));

}
glEnd();

glBegin(GL_POLYGON);
glColor3ub(0.0, 0.0, 0.0);
float angle42;
for (int i=0;i<360;i++)
{
angle42=i*3.1416/180;
glVertex2f(0.75+0.02*cos(angle42),0.71+0.02*sin(angle42));

}
glEnd();

glBegin(GL_POLYGON);
glColor3ub(255.0, 255.0, 255.0);
float angle43;
for (int i=0;i<360;i++)
{
angle43=i*3.1416/180;
glVertex2f(0.74+0.005*cos(angle43),0.71+0.005*sin(angle43));

}
glEnd();
//wing

glBegin(GL_TRIANGLES);
glColor3ub(255.0, 255.0, 255.0);
glVertex2f(0.8f,0.75f);
glVertex2f(0.79,0.71f);
glVertex2f(0.785f,0.8f);
glEnd();

//lip
glBegin(GL_TRIANGLES);
glColor3ub(255.0, 0.0, 0.0);
glVertex2f(0.73f,0.72f);
glVertex2f(0.69f,0.71f);
glVertex2f(0.73f,0.70f);
glEnd();
 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}