ballonpoking(A GAME IN C++)
This game will help all new C Language Programmers in understanding the concepts of graphics and object oriented programming in […]
This game will help all new C Language Programmers in understanding the concepts of graphics and object oriented programming in […]
Example of a program with a little information?hiding contained in it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
/******************************************************* * MYCPLUS Sample Code - https://www.mycplus.com * * * * This code is made available as a service to our * * visitors and is provided strictly for the * * purpose of illustration. * * * * Please direct all inquiries to saqib at mycplus.com * *******************************************************/ #include <iostream.h> class one_datum { int data_store; public: void set(int in_value); int get_value(void); }; void one_datum::set(int in_value) { data_store = in_value; } int one_datum::get_value(void) { return data_store; } main() { one_datum dog1, dog2, dog3; int piggy; dog1.set(12); dog2.set(17); dog3.set(-13); piggy = 123; // dog1.data_store = 115; This is illegal in C++ // dog2.data_store = 211; This is illegal in C++ cout << "The value of dog1 is " << dog1.get_value() << "\n"; cout << "The value of dog2 is " << dog2.get_value() << "\n"; cout << "The value of dog3 is " << dog3.get_value() << "\n"; cout << "The value of piggy is " << piggy << "\n"; } // Result of execution // // The value of dog1 is 12 // The value of dog2 is 17 // The value of dog3 is -13 // The value of piggy is 123 |
SWAT GAME
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
/******************************************************* * MYCPLUS Sample Code - https://www.mycplus.com * * * * This code is made available as a service to our * * visitors and is provided strictly for the * * purpose of illustration. * * * * Please direct all inquiries to saqib at mycplus.com * *******************************************************/ #include <graphics.h> #include <conio.h> #include <stdlib.h> #include <dos.h> class swat{ public: void draw_swat( int x ){ setcolor( LIGHTCYAN ); setlinestyle( SOLID_LINE,0,3 ); line( x,470,x,275); setfillstyle( XHATCH_FILL, LIGHTCYAN ); rectangle( x-80,50,x+80,275); floodfill( x,200, LIGHTCYAN ); } } swats; int final_x, final_y; class fly{ private: int move_x, move_y, current_x, current_y; public: void draw_fly( int& ); void show_fly(){ setcolor( WHITE ); setfillstyle( SOLID_FILL, getcolor() ); circle( final_x, final_y, 4 ); floodfill( final_x, final_y, getcolor() ); } } fly; void fly :: draw_fly( int &time ) { randomize(); current_x = random(600); current_y = random(275); for( int i=0; i<10; i++ ){ setcolor( WHITE ); setfillstyle( SOLID_FILL, getcolor() ); circle( current_x, current_y, 4 ); floodfill( current_x, current_y, getcolor() ); sound(200); delay( time ); setcolor( BLACK ); setfillstyle( SOLID_FILL, getcolor() ); circle( current_x, current_y, 4 ); floodfill( current_x, current_y, getcolor() ); nosound(); final_x = current_x; final_y = current_y; move_x = random(300); move_y = random(175); if ( current_x + move_x > 600 ) current_x -= 1.25 * move_x; else current_x += move_x; if ( current_x <= 0 ) current_x += 0.75 * move_x; if ( current_y + move_y > 275 ) current_y -= move_y; else if( current_y + move_y < 50 ) current_y += 2 * move_y; else current_y += move_y; } } void main() { int pos; int swat_number; int time = 700; enum state{ WIN,LOSE }; state user; int driver=DETECT; int mode=DETECT; initgraph(&driver,&mode,"\\borlandc\\bgi <file://\\borlandc\\bgi>"); highvideo(); do{ user = LOSE; pos = 120; cleardevice(); fly.draw_fly( time ); time-= 50; for( int i=0; i<3; i++ ){ swats.draw_swat( pos ); pos+=200; } settextstyle(DEFAULT_FONT,HORIZ_DIR,2); outtextxy( 10,5, "Enter swat number?" ); swat_number = getch(); cleardevice(); switch ( swat_number ){ case '1' : if( ( final_x < 220 && final_x > 0 )) user = WIN; break; case '2' : if( ( final_x < 420 && final_x > 220 ) ) user = WIN; break; case '3' : if( ( final_x < 620 && final_x > 420 ) ) user = WIN; break; } cleardevice(); if( time <= 70 && user == WIN ){ settextstyle(TRIPLEX_FONT,HORIZ_DIR,11); for( int i=1; i<15; i++ ){ setcolor(i); outtextxy( 180,10,"FLY" ); outtextxy( 30,150,"SWATTING" ); outtextxy( 10,300,"CHAMPION" ); delay(1000); } exit( EXIT_SUCCESS ); } fly.show_fly(); settextstyle(TRIPLEX_FONT,HORIZ_DIR,9); if( user == WIN ){ setcolor(LIGHTGREEN); outtextxy( 40,350,"YOU WON !!!!" ); } else{ setcolor(LIGHTRED); outtextxy( 40,350,"YOU LOSE !!!!" ); } getch(); }while( user == WIN ); } |
This C++ date class is intended to illustrate how to write a non- trivial class in C++. Even though this
First hint of object oriented programming using C++.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream.h> #include <string.h> main() { int index; float distance; char letter; char name[25]; index = -23; distance = 12.345; letter = 'X'; strcpy(name,"John Doe"); cout << "The value of index is " << index << "\n"; cout << "The value of distance is " << distance << "\n"; cout << "The value of letter is " << letter << "\n"; cout << "The value of name is " << name << "\n"; index = 31; cout << "The decimal value of index is " << dec << index << "\n"; cout << "The octal value of index is " << oct << index << "\n"; cout << "The hex value of index is " << hex << index << "\n"; cout << "The character letter is " << (char)letter << "\n"; cout << "Input a decimal value --> "; cin >> index; cout << "The hex value of the input is " << index << "\n"; } |
Output of the C++ Program: Result of execution The value of
The following C++ program demonstrates the concept of constructors and destructors in C++.
Simple class program
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
/******************************************************* * MYCPLUS Sample Code - https://www.mycplus.com * * * * This code is made available as a service to our * * visitors and is provided strictly for the * * purpose of illustration. * * * * Please direct all inquiries to saqib at mycplus.com * *******************************************************/ #include class vehicle { protected: int wheels; float weight; public: void initialize(int in_wheels, float in_weight); int get_wheels(void) {return wheels;} float get_weight(void) {return weight;} float wheel_loading(void) {return weight/wheels;} }; class car : public vehicle { int passenger_load; public: void initialize(int in_wheels, float in_weight, int people = 4); int passengers(void) {return passenger_load;} }; class truck : public vehicle { int passenger_load; float payload; public: void init_truck(int how_many = 2, float max_load = 24000.0); float efficiency(void); int passengers(void) {return passenger_load;} }; main() { vehicle unicycle; unicycle.initialize(1, 12.5); cout << "The unicycle has " << unicycle.get_wheels() << " wheel.\n"; cout << "The unicycle's wheel loading is " << unicycle.wheel_loading() << " pounds on the single tire.\n"; cout << "The unicycle weighs " << unicycle.get_weight() << " pounds.\n\n"; car sedan; sedan.initialize(4, 3500.0, 5); cout << "The sedan carries " << sedan.passengers() << " passengers.\n"; cout << "The sedan weighs " << sedan.get_weight() << " pounds.\n"; cout << "The sedan's wheel loading is " << sedan.wheel_loading() << " pounds per tire.\n\n"; truck semi; semi.initialize(18, 12500.0); semi.init_truck(1, 33675.0); cout << "The semi weighs " << semi.get_weight() << " pounds.\n"; cout << "The semi's efficiency is " << 100.0 * semi.efficiency() << " percent.\n"; } // initialize to any data desired void vehicle::initialize(int in_wheels, float in_weight) { wheels = in_wheels; weight = in_weight; } void car::initialize(int in_wheels, float in_weight, int people) { passenger_load = people; wheels = in_wheels; weight = in_weight; } void truck::init_truck(int how_many, float max_load) { passenger_load = how_many; payload = max_load; } float truck::efficiency(void) { return payload / (payload + weight); } // Result of execution // // The unicycle has 1 wheel. // The unicycle's wheel loading is 12.5 pounds on the single tire. // The unicycle weighs 12.5 pounds. // // The sedan carries 5 passengers. // The sedan weighs 3500 pounds. // The sedan's wheel loading is 875 pounds per tire. // // The semi weighs 12500 pounds. // The semi's efficiency is 72.929072 percent. |
In order to keep the program as simple as possible, all of the member methods are defined as inline functions.?
program, the objects are instantiated from an?inherited class and the intent of this program? is to illustrate that there is
Demonstration of inheritance in c++. This example contains the following files. VEHICLE.H VEHICLE.CPP ALLVEHIC.CPP CAR.H CAR.CPP TRANSPORT.CPP TRUCK.H TRUCK.CPP Vehicle.h
The basic concept of multiple inheritance (MI) sounds simple enough: you create a new type by inheriting from more than
This is a simple C++ program to make a car race game. It uses object oriented approach to generate different objects in the game such as using Car, Obstacle and Timer classes.