C++ Programming Tutorial

 
 
 

Object Oriented Programming

Structures

Example of structures Passing structures to function Passing structure pointers to function Pointers and an array of structure Relationship between union and structure Array of structure Nested structures Self-referential structures Linked List

Classes

Classes with inline functions Classes without using inline functions Example of constructor in classes Passing values to constructor in classes Overloading of constructors in classes Example of destructor in classes Example of static class data Example of static member functions Constant objects, constant member functions Strings as member of classes Use of arrays as data items in classes Example of array of objects in classes Use of pointer this using header file this.h Obj initialize, assign by default member wise copy

Inheritance - Containership

Example of Inheritance Over riding base class member fn in derived Diff in public, protected, private inheritance Multi-level inheritance Multiple inheritance Example of containership

Virtual Functions - Polymorphism

Example of Polymorphism Example of Pure Virtual functions

Operator Overloading - Type Casting

Increment operator ++ overloading w/o return type Increment operator ++ overloading - return type Decrement operator -- overloading w/o return type Decrement operator -- overloading - return type Operator overloading for strings Comparision operator < overloading Binary operator + overloading creating new object Binary operator + overloading w/o creating object Binary operator - overloading creating object Binary operator - overloading w/o creating object Binary operator * overloading w/o creating object Stream insertion >> , extraction << operators Data conversion b/w built-in data types Data conversion - built-in, user defined data type Conversion- built-in, user defined data type- char Data conversion user defined data type - function Conversion user defined data type - constructor

Friend Functions and Classes

Use of friend functions Use of friend classes Operator overloading using friend function All Operator overloading using friend function Addition of 2 matrix using friend function Example of friend function of a class

Template Functions and Classes

Interchange value using function template Example of template classes Template based array input Template to find minimum value in an array Template of vector class

File Processing - Command Line Arguments

Open file File operation program - diff type of mode Read from one text file, writing in new text file File read, write, update and display operations Read decimal from file - convert into hexadecimal Read words from file and remove palindrome Read device data from text file and show extension Example of binary files

General Programs

Number of days b/w two given dates Capitalize first charcter of every word in string Copy n num of characters from one to another Word is palindrome or not using classes & pointers Simulation of the Solar System Add and Subtract functions on Big Number Class
 
 
 # include <iostream.h>
 # include <graphics.h>
 # include <stdlib.h>
 # include <conio.h>
 # include <math.h>
 # include <dos.h>



 //-------------------------------  Planet  ------------------------------//


 class Planet
 {
    private:
       int x;
       int y;
       int rx;
       int ry;
       int color;
       int radius;
       int position;

       float displacement;

       char* Buffer;

    public:
       Planet( )   { x=y=rx=ry=radius=0; }
       ~Planet( )  { delete Buffer; }
       Planet(const int,const int,const int,const float);

       void Show( );
       void Move( );
       void ShowOrbit( );
       void GetNextPosition( );
 };


 //-----------------------------  Planet( )  -----------------------------//


 Planet::Planet(const int _rx,const int _ry,const int _radius,
                         const float _displacement)
 {
    rx=_rx;
    ry=_ry;

    x=(319+rx);
    y=240;

    radius=_radius;
    displacement=_displacement;
    position=0;
 }


 //----------------------------  ShowOrbit( )  ---------------------------//


 void Planet::ShowOrbit( )
 {
    setcolor(1);
      ellipse(320,240,0,360,rx,ry);
 }


 //--------------------------------  Show( )  ----------------------------//


 void Planet::Show( )
 {
    color=(random(14)+8);

    if(color==8 || color==14 || color==16)
       color=(random(6)+1);

    Buffer=new char[imagesize((x-(radius+3)),(y-(radius+3)),
                        (x+radius+3),(y+radius+3))];

    getimage((x-(radius+3)),(y-(radius+3)),(x+radius+3),(y+radius+3),Buffer);

    setcolor(color);
    setfillstyle(1,color);
      pieslice(x,y,0,360,radius);

    setcolor(11);
      circle(x,y,radius);
 }


 //--------------------------------  Move  -------------------------------//


 void Planet::Move( )
 {
    putimage((x-(radius+3)),(y-(radius+3)),Buffer,0);

    delete Buffer;

    GetNextPosition( );

    Buffer=new char[imagesize((x-(radius+3)),(y-(radius+3)),
                    (x+(radius+3)),(y+(radius+3)))];

    getimage((x-(radius+3)),(y-(radius+3)),(x+(radius+3)),
                        (y+(radius+3)),Buffer);

    setcolor(color);
    setfillstyle(1,color);
      pieslice(x,y,0,360,radius);

    setcolor((15-color));
      circle(x,y,radius);
 }


 //-------------------------  GetNextPosition( )  ------------------------//


 void Planet::GetNextPosition( )
 {
    float angle=(displacement*position);

    x=(int)(rx*cosl(angle)+320+0.5);
    y=(int)(ry*sinl(angle)+240+0.5);

    if((y==240 || y==239) && x>320)
       position=1;

    position++;
 }



 /*******************************  main( )  *******************************/



 void main( )
 {
    int driver=VGA;
    int mode=VGAHI;
    int error_code;

    initgraph(&driver,&mode,\"..\\\\Bgi\");

    error_code=graphresult( );

    if(error_code!=grOk)
    {
       restorecrtmode( );
       textmode(BW80);
       clrscr( );

       cout<<\" \\n Fatal Error  : Graphic Driver not initialized\"<<endl;
       cout<<\" Error Reason : \"<<grapherrormsg(error_code)<<endl;
       cout<<\" \\n Press any key to exit...\";

       getch( );
       exit(1);
    }

    setlinestyle(0,0,3);

    setcolor(7);
      rectangle(0,0,getmaxx( ),getmaxy( ));

    setlinestyle(0,0,0);

    for(int count=0;count<5000;count++)
       putpixel(random(640),random(480),random(15));

    settextstyle(2,0,7);
      setcolor(9);
    outtextxy(15,10,\"Solar\");
    outtextxy(16,10,\"Solar\");
    outtextxy(16,11,\"Solar\");

      setcolor(3);
    outtextxy(72,10,\"System\");
    outtextxy(73,10,\"System\");
    outtextxy(73,11,\"System\");

      setcolor(11);
    outtextxy(22,27,\"Simulation\");
    outtextxy(23,27,\"Simulation\");
    outtextxy(23,28,\"Simulation\");

    setcolor(7);
    settextstyle(2,0,4);
      outtextxy(10,465,\"Press any key to exit.\");

    setcolor(14);
    setfillstyle(1,14);
      pieslice(320,240,0,360,12);

   setcolor(12);
     circle(320,240,12);

   setcolor(4);
     circle(320,240,13);

    Planet Mercury(50,30,8,0.0175);
    Planet Venus(80,55,8,0.0155);
    Planet Earth(110,80,8,0.0135);
    Planet Mars(140,105,8,0.0115);
    Planet Jupiter(170,130,8,0.0095);
    Planet Saturn(200,155,8,0.0075);
    Planet Uranus(230,180,8,0.0055);
    Planet Neptune(260,205,8,0.0035);
    Planet Pluto(290,230,8,0.0015);

    Mercury.ShowOrbit( );
    Mercury.Show( );

    Venus.ShowOrbit( );
    Venus.Show( );

    Earth.ShowOrbit( );
    Earth.Show( );

    Mars.ShowOrbit( );
    Mars.Show( );

    Jupiter.ShowOrbit( );
    Jupiter.Show( );

    Saturn.ShowOrbit( );
    Saturn.Show( );

    Uranus.ShowOrbit( );
    Uranus.Show( );

    Neptune.ShowOrbit( );
    Neptune.Show( );

    Pluto.ShowOrbit( );
    Pluto.Show( );

    do
    {
       delay(25);

       Mercury.Move( );
       Venus.Move( );
       Earth.Move( );
       Mars.Move( );
       Jupiter.Move( );
       Saturn.Move( );
       Uranus.Move( );
       Neptune.Move( );
       Pluto.Move( );
    }
    while(!kbhit( ));

    getch( );
    closegraph( );
 }

    Related Post:
  1. EXAMPLE OF WRITE FUNCTION

  2. To parse a string using First and Follow algorithm and LL-1 parser

  3. Linear Interpolation

  4. Program that provides an example of binary files

  5. Program to get possible prime numbers like number 6158 contains the substrings 6, 1, 5, 8, 61, 15, 58, 615, 158, and 6158.Find the largest substring..

  6. Program of LL Parser

  7. Program to find largest integer among the three and display it using function with no return type

  8. Program to read a Linear System of Equations,then evaluate it by using Guass-Seidel Itrative Method and show the result

  9. Program to convert infix expression to postfix / suffix expression using paranthesize

  10. Program to illustrate the implementation of Scaling Transformation along a Fixed Point

  11. Program to estimate value of First Derivative of the function at the given points from the given data using Backward Difference Formula , Forward diff

  12. Program to perform operator overloading converting one class object to another

  13. write a class to represent a vector (a series of float values)

  14. Program that reads two integers and an operator , evaluates and displays the result using switch-case statement

  15. Program to show the projection of 3D objects using General Perspective Projection

  16. Program to covert a given Decimal Number into English Code

  17. Program to implement a Translator that reads an Infix Expression translates it into a Postfix Expression and evaluate the Postfix Expression

  18. Program to print a histogram

  19. Program to fill a Circle using Scan-Line Circle Fill Algorithm

  20. Program to construct Newtons Divided Difference Interpolation Formula from the given distinct data points and estimate the value of the function

 
 
Didn't find what you were looking for? Find more on Program to show the simulation of the Solar System