#include<iostream>  
#include<stdlib.h>  
#ifdef __APPLE__  
#include<openGL/openGL.h>  
#include<GLUT/glut.h>  
#else   
#include<GL/glut.h>  
#endif  
using namespace std;  
GLfloat ballX = -0.8f;  GLfloat ballY = -0.3f;  
GLfloat ballZ = -1.2f;  
GLfloat colR=3.0;  
GLfloat colG=1.5;  
GLfloat colB=1.0;  
GLfloat bgColR=0.0;  
GLfloat bgColG=0.0;  
GLfloat bgColB=0.0;  
static int flag=1;  
void drawBall(void) {  
  glColor3f(colR,colG,colB); //set ball colour      
  glTranslatef(ballX,ballY,ballZ); 
//moving it toward the screen a bit on creation     
glutSolidSphere (0.05, 30, 
30); //create ball.  
 }  
void drawAv(void) {  
        glBegin(GL_POLYGON);  
        glColor3f(1.0,1.0,1.0);  
        glVertex3f(-0.9,-0.7,-1.0);  
        glVertex3f(-0.5,-0.1,-1.0);  
        glVertex3f(-0.2,-1.0,-1.0);  
        glVertex3f(0.5,0.0,-1.0);  
        glVertex3f(0.6,-0.2,-1.0);  
        glVertex3f(0.9,-0.7,-1.0);  
   glEnd();  
  }  
void drawClouds(){}  
void keyPress(int key, int x, int y)  
{   if(key==GLUT_KEY_RIGHT)  
        ballX -= 0.05f;  
    if(key==GLUT_KEY_LEFT)  
        ballX  += 0.05f;    
        glutPostRedisplay();  
}  
  
void initRendering() {   
    glEnable(GL_DEPTH_TEST);  
    glEnable(GL_COLOR_MATERIAL);  
    glEnable(GL_LIGHTING); //Enable lighting    
    glEnable(GL_LIGHT0); //Enable light #0     
    glEnable(GL_LIGHT1); //Enable light #1    
    glEnable(GL_NORMALIZE); //Automatically normalize normals  
    glShadeModel(GL_SMOOTH); //Enable smooth shading  
}  
  
//Called when the window is resized  
void handleResize(int w, int h) {  
    //Tell OpenGL how to convert from coordinates to pixel values   
    glViewport(0, 0, w, h);  
      
    glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective  
      
    //Set the camera perspective    
    glLoadIdentity(); 
//Reset the camera     
gluPerspective(45.0,                  
//The camera angle  
                   (double)w / (double)h, //The width-to-height ratio  
                   1.0,                   //The near z clipping coordinate  
                   200.0);                //The far z clipping coordinate  
}  
void drawScene()  
{  
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);    
    glClearColor(bgColR,bgColG,bgColB,0.0);    
    glMatrixMode(GL_MODELVIEW);  
    glLoadIdentity();  
     
    //Add ambient light  
    GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)     
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);  
     
    //Add positioned light  
    GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)  
    GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)     
glLightfv(GL_LIGHT0, 	GL_DIFFUSE, 	lightColor0);     
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);  
     
    //Add directed light  
    GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)  
    //Coming from the direction (-1, 0.5, 0.5)  
    GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};    
    glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);    
    glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);  
  
    //drawing the SUN    
    glPushMatrix();      
    drawBall();    
    glPopMatrix();      //drawing the Mount Avarest    
    glPushMatrix();        
    drawAv();    
    glPopMatrix();  
  
    //drawing the Clouds   
    glPushMatrix();        
    drawClouds();    
    glPopMatrix();  
    glutSwapBuffers();  
}  
  
//float _angle = 30.0f;  
void update(int value) {  
     
    If(ballX>0.9f)  
    {  
        ballX = -0.8f;         ballY = -0.3f;         flag=1;         colR=2.0;         colG=1.50;         colB=1.0;  
  
        bgColB=0.0;  
    }  
     
    If(flag)  
    {  
    ballX += 0.001f;     ballY +=0.0007f;     colR-=0.001;     //colG+=0.002;     colB+=0.005;     bgColB+=0.001;         if(ballX>0.01)  
       {  
           Flag=0;  
  
       }  
    }  
    If (!flag)  
    {  
        ballX += 0.001f;         ballY -=0.0007f;         colR+=0.001;         colB-
=0.01;  
  
        bgColB-=0.001;  
  
        if(ballX<-0.3)  
       {  
           Flag=1;  
  
       }  
    }  
     
    glutPostRedisplay(); //Tell GLUT that the display has changed  
     
    //Tell GLUT to call update again in 25 milliseconds   
    glutTimerFunc(25, update, 0);  
}  
  
int main(int argc,char** argv)  
{  
    glutInit(&argc,argv);     
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);  
     
    glutInitWindowSize(400,400);  
     
    glutCreateWindow(“Sun”);  
     
    initRendering();  
     
    glutDisplayFunc(drawScene);  
  
    glutFullScreen();  
     
    glutSpecialFunc(keyPress);  
    glutReshapeFunc(handleResize);  
  
    glutTimerFunc(25, update, 0);  
  
    glutMainLoop();  
     
    return(0);  
}  
 

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
}