C++ samples

Forum dedicated to computer hardware and software, mobile phones and electronic gadgets.
User avatar
brizanden
Posts: 6064
Joined: Mon Apr 30, 2007 8:04 am
Car: thrased kouki ftw
Contact:

Post

ill start with headers#ifndef SONG_H#define SONG_H#include <string>#include "Date.h"#include "Time.h"using namespace std;// class Song definitionclass Song{public: Song( ); // default constructor void setSong(); void print(); //prints Song in standard formatprivate: string title; Date dateAdded; string artist; int size; // in Mbytes Time length; Date lastPlayed; string fileName;}; // end class Song

#endif

// Fig. 9.1: Time.h// Declaration of class Time.// Member functions are defined in Time.cpp

// prevent multiple inclusions of header file#ifndef TIME_H#define TIME_H

// Time class definitionclass Time {public: Time(); // fefault constructor void setTime( int, int, int ); // set hour, minute and second void print(); //prints out time in standard format void clocktick();private: int hour; // 0 - 23 (24-hour clock format) int minute; // 0 - 59 int second; // 0 - 59}; // end class Time

#endif

#ifndef DATE_H#define DATE_H

// class Date definitionclass Date {public: Date( int = 1, int = 1, int = 2000 ); // default constructor void setDate(int =1, int=1, int =2000); void print(); //prints Date in standard formatprivate: int month; int day; int year;}; // end class Date

#endif

then source files

// Song.cpp#include <iostream>#include "Song.h" // include definition of class Time from Time.husing namespace std;// Time constructor initializes each data member to zero.// Ensures all Time objects start in a consistent state.Song::Song() { cout<<"New song constructed\n";} // end Song constructor

// set new Time value using universal time; ensure that// the data remains consistent by setting invalid values to zerovoid Song::setSong(){ cout<<"Enter title:\n "; string title1; cin>> title1; title=title1; cout<< "Enter date added: \n"; int d,m,y; cin >> d >> m>> y; dateAdded.setDate(d,m,y); } // end function setSong

// print Songvoid Song::print(){ cout <<"Title: "<< title << endl; cout << "Date added: "; dateAdded.print(); } // end function print

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

#include <iomanip>using std::setfill;using std::setw;

#include "Time.h" // include definition of class Time from Time.h

// Time constructor initializes each data member to zero.// Ensures all Time objects start in a consistent state.Time::Time() { int curtime = (unsigned)time(0); int secFromMidnight = (curtime-(3600*5))%(3600*24); int hourFromMidnight = (secFromMidnight/3600); minute = (secFromMidnight-(hourFromMidnight*3600))/60; second = curtime%60; hour = hourFromMidnight;

} // end Time constructor

// set new Time value using universal time; ensure that// the data remains consistent by setting invalid values to zerovoid Time::setTime( int h, int m, int s ){ hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute second = ( s >= 0 && s < 60 ) ? s : 0; // validate second} // end function setTime

// print Time in standard-time format (HH:MM:SS AM or PM)void Time::print(){ cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":" << setfill( '0' ) << setw( 2 ) << minute << ":" << setw( 2 ) << second << ( hour < 12 ? " AM" : " PM" );} // end function print

void Time::clocktick(){ second++; if ( second == 60) { second = 0; minute++; if (minute == 60) { minute = 0; hour++; if (hour == 24) hour = 0; } }}

#include <iostream>using namespace std;#include "Date.h" // include definition of class Date from Date.h

// Date constructor (should do range checking)Date::Date( int m, int d, int y ){ month = m; day = d; year = y;} // end constructor Date

void Date::setDate( int m, int d, int y ){ month = m; day = d; year = y;} // end Date::setDate

// print Date in the format mm/dd/yyyyvoid Date::print() { cout << month << '/' << day << '/' << year; } // end function print

lastly main

int main(){ Time t; // instantiate object t of class Time Date d;

// Test code for Time class cout << "The initial universal time is "; t.print(); // 12:00:00 AM

//Example using Date class Date date1( 3, 4, 2008 ); Date date2; // date2 defaults to 1/1/2000

cout << "date1 = "; date1.print(); cout << "\ndate2 = "; date2.print();

date2 = date1; // default memberwise assignment

cout << "\n\nAfter default memberwise assignment, date2 = "; date2.print();

for (int i = 0; i < 60; i++) { t.clocktick(); cout << "your time after clockticks \n"; t.print(); }

char x; cin>>x; return 0; } // end main

see max fairly basic and i had writing this stupid pointless bs programs for classes. when do i get to learn cool stuff?edit: **** u nico u ruined my nice format ugh lol



User avatar
GEO
Posts: 6449
Joined: Mon Jul 07, 2003 3:15 pm
Car: 95 240sx KA-T
Contact:

Post

paste it at pastebin.com so it keeps structure, then I will look at it.

User avatar
MaxFNPower
Posts: 524
Joined: Sat Jan 12, 2008 10:23 pm
Car: 93 240sx SE Coupe KA24DE (My Dorthy) 97 240sx Coupe KA24DE(Un-Named) 87 Nissan Pick-up (Tin Man)

Post

****Dark Basic***`setupset display mode 800,600,32cls#include enemy.dbahide mouseEnable TnLSET Emulation OFFsync onsync rate 100backdrop oncolor backdrop rgb(100,100,100)hide mouse`load soundsload sound "explode2.wav",1load sound "tone.wav",2`load imagesload image "ship1.jpg",1load image "ship2.jpg",2load image "bullet.bmp",1000load image "smallbullet.bmp",1050load image "alien2.bmp",500load image "stars.bmp",65000`load musicload music "Overclocked.mid",1`sprites yay`shipsprite 1,100,100,1`enemysprite 2,400,300,500`ammosprite 3,2000,2000,1000set sprite 1,1,1set sprite 2,1,1`int settexture backdrop 65000play music 1shipx=200shipy=300shipxd=50shipyd=100 shipimg=1shipspeed=3shot=0bulletlittle=1`bullet delay varbd=12rem do loopdorem externals ********************************************************enemy()

`***********************************************************************debug()sync`ship managmentif shipimg=2 then shipimg=1 else shipimg=2if shipx>740 then shipstopright=1 else shipstopright=0if shipy>565 then shipstopdown=1 else shipstopdown=0 if shipx<0 then shipstopleft=1 else shipstopleft=0if shipy<0 then shipstopup=1 else shipstopup=0sprite 1,shipx,shipy,shipimg `ship controls

`Little gun sprite number 4-20`Control`Bullet 1if controlkey()=1 and bulletlittle1life=0 and bulletlittle=1 and littledelay=0bulletlittle1x=shipx+60bulletlittle1y=shipy+10bulletlittle1life=1bulletlittle=2play sound 2littledelay=bdendif`Bullet 2if controlkey()=1 and bulletlittle2life=0 and bulletlittle=2 and littledelay=0bulletlittle2x=shipx+60bulletlittle2y=shipy+10bulletlittle2life=1bulletlittle=3play sound 2littledelay=bdendif`Bullet 3if controlkey()=1 and bulletlittle3life=0 and bulletlittle=3 and littledelay=0bulletlittle3x=shipx+60bulletlittle3y=shipy+10bulletlittle3life=1bulletlittle=4play sound 2littledelay=bdendif`Bullet 4if controlkey()=1 and bulletlittle4life=0 and bulletlittle=4 and littledelay=0bulletlittle4x=shipx+60bulletlittle4y=shipy+10bulletlittle4life=1bulletlittle=5play sound 2littledelay=bdendif`Bullet 5if controlkey()=1 and bulletlittle5life=0 and bulletlittle=5 and littledelay=0bulletlittle5x=shipx+60bulletlittle5y=shipy+10bulletlittle5life=1bulletlittle=6play sound 2littledelay=bdendif`Bullet 6if controlkey()=1 and bulletlittle6life=0 and bulletlittle=6 and littledelay=0bulletlittle6x=shipx+60bulletlittle6y=shipy+10bulletlittle6life=1bulletlittle=7play sound 2littledelay=bdendif`Bullet 7if controlkey()=1 and bulletlittle7life=0 and bulletlittle=7 and littledelay=0bulletlittle7x=shipx+60bulletlittle7y=shipy+10bulletlittle7life=1bulletlittle=8play sound 2littledelay=bdendif`Bullet 8if controlkey()=1 and bulletlittle8life=0 and bulletlittle=8 and littledelay=0bulletlittle8x=shipx+60bulletlittle8y=shipy+10bulletlittle8life=1bulletlittle=9play sound 2littledelay=bdendif`Bullet 9if controlkey()=1 and bulletlittle9life=0 and bulletlittle=9 and littledelay=0bulletlittle9x=shipx+60bulletlittle9y=shipy+10bulletlittle9life=1bulletlittle=10play sound 2littledelay=bdendif`Bullet 10if controlkey()=1 and bulletlittle10life=0 and bulletlittle=10 and littledelay=0bulletlittle10x=shipx+60bulletlittle10y=shipy+10bulletlittle10life=1bulletlittle=11play sound 2littledelay=bdendif`Bullet 11if controlkey()=1 and bulletlittle11life=0 and bulletlittle=11 and littledelay=0bulletlittle11x=shipx+60bulletlittle11y=shipy+10bulletlittle11life=1bulletlittle=12play sound 2littledelay=bdendif`Bullet 12if controlkey()=1 and bulletlittle12life=0 and bulletlittle=12 and littledelay=0bulletlittle12x=shipx+60bulletlittle12y=shipy+10bulletlittle12life=1bulletlittle=13play sound 2littledelay=bdendif`Bullet 13if controlkey()=1 and bulletlittle13life=0 and bulletlittle=13 and littledelay=0bulletlittle13x=shipx+60bulletlittle13y=shipy+10bulletlittle13life=1bulletlittle=14play sound 2littledelay=bdendif`Bullet 14if controlkey()=1 and bulletlittle14life=0 and bulletlittle=14 and littledelay=0bulletlittle14x=shipx+60bulletlittle14y=shipy+10bulletlittle14life=1bulletlittle=15play sound 2littledelay=bdendif`Bullet 15if controlkey()=1 and bulletlittle15life=0 and bulletlittle=15 and littledelay=0bulletlittle15x=shipx+60bulletlittle15y=shipy+10bulletlittle15life=1bulletlittle=16play sound 2littledelay=bdendif`Bullet 16if controlkey()=1 and bulletlittle16life=0 and bulletlittle=16 and littledelay=0bulletlittle16x=shipx+60bulletlittle16y=shipy+10bulletlittle16life=1bulletlittle=17play sound 2littledelay=bdendif`Bullet 17if controlkey()=1 and bulletlittle17life=0 and bulletlittle=17 and littledelay=0bulletlittle17x=shipx+60bulletlittle17y=shipy+10bulletlittle17life=1bulletlittle=18play sound 2littledelay=bdendif`returnerif bulletlittle=18 then bulletlittle=1if littledelay>0 then littledelay=littledelay-1`Action`Bullet 1if bulletlittle1life=1sprite 4,bulletlittle1x,bulletlittle1y,1050bulletlittle1x=bulletlittle1x+5if bulletlittle1x>1000 then bulletlittle1life=0endif`Bullet 2if bulletlittle2life=1sprite 5,bulletlittle2x,bulletlittle2y,1050bulletlittle2x=bulletlittle2x+5if bulletlittle2x>1000 then bulletlittle2life=0endif`Bullet 3if bulletlittle3life=1sprite 6,bulletlittle3x,bulletlittle3y,1050bulletlittle3x=bulletlittle3x+5if bulletlittle3x>1000 then bulletlittle3life=0endif`Bullet 4if bulletlittle4life=1sprite 7,bulletlittle4x,bulletlittle4y,1050bulletlittle4x=bulletlittle4x+5if bulletlittle4x>1000 then bulletlittle4life=0endif`Bullet 5if bulletlittle5life=1sprite 8,bulletlittle5x,bulletlittle5y,1050bulletlittle5x=bulletlittle5x+5if bulletlittle5x>1000 then bulletlittle5life=0endif`Bullet 6if bulletlittle6life=1sprite 9,bulletlittle6x,bulletlittle6y,1050bulletlittle6x=bulletlittle6x+5if bulletlittle6x>1000 then bulletlittle6life=0endif`Bullet 7if bulletlittle7life=1sprite 10,bulletlittle7x,bulletlittle7y,1050bulletlittle7x=bulletlittle7x+5if bulletlittle7x>1000 then bulletlittle7life=0endif`Bullet 8if bulletlittle8life=1sprite 11,bulletlittle8x,bulletlittle8y,1050bulletlittle8x=bulletlittle8x+5if bulletlittle8x>1000 then bulletlittle8life=0endif`Bullet 9if bulletlittle9life=1sprite 12,bulletlittle9x,bulletlittle9y,1050bulletlittle9x=bulletlittle9x+5if bulletlittle9x>1000 then bulletlittle9life=0endif`Bullet 10if bulletlittle10life=1sprite 13,bulletlittle10x,bulletlittle10y,1050bulletlittle10x=bulletlittle10x+5if bulletlittle10x>1000 then bulletlittle10life=0endif`Bullet 11if bulletlittle11life=1sprite 14,bulletlittle11x,bulletlittle11y,1050bulletlittle11x=bulletlittle11x+5if bulletlittle11x>1000 then bulletlittle11life=0endif`Bullet 12if bulletlittle12life=1sprite 15,bulletlittle12x,bulletlittle12y,1050bulletlittle12x=bulletlittle12x+5if bulletlittle12x>1000 then bulletlittle12life=0endif`Bullet 13if bulletlittle13life=1sprite 16,bulletlittle13x,bulletlittle13y,1050bulletlittle13x=bulletlittle13x+5if bulletlittle13x>1000 then bulletlittle13life=0endif`Bullet 14if bulletlittle14life=1sprite 17,bulletlittle14x,bulletlittle14y,1050bulletlittle14x=bulletlittle14x+5if bulletlittle14x>1000 then bulletlittle14life=0endif`Bullet 15if bulletlittle15life=1sprite 18,bulletlittle15x,bulletlittle15y,1050bulletlittle15x=bulletlittle15x+5if bulletlittle15x>1000 then bulletlittle15life=0endif`Bullet 16if bulletlittle16life=1sprite 19,bulletlittle16x,bulletlittle16y,1050bulletlittle16x=bulletlittle16x+5if bulletlittle16x>1000 then bulletlittle16life=0endif`Bullet 17if bulletlittle17life=1sprite 20,bulletlittle17x,bulletlittle17y,1050bulletlittle17x=bulletlittle17x+5if bulletlittle17x>1000 then bulletlittle17life=0endif`**************************************************`Big Gun`controlif spacekey()=1 and bulletbiglife=0 bulletbigx=shipxbulletbigy=shipybulletbiglife=1play sound 1endif`actionif bulletbiglife=1sprite 3,bulletbigx,bulletbigy,1000bulletbigx=bulletbigx+10if bulletbigx>1000 then bulletbiglife=0endif`shipif upkey()=1 and shipstopup=0 then shipy=shipy-shipspeedif downkey()=1 and shipstopdown=0 then shipy=shipy+shipspeedif leftkey()=1 and shipstopleft=0 then shipx=shipx-shipspeedif rightkey()=1 and shipstopright=0 then shipx=shipx+shipspeed`if shot=1 and bulletlife=0`sprite 3,shipx,shipy,1000`endif

loop

function debug()gg=screen fps()print " fps ",gg,""endfunction

User avatar
GEO
Posts: 6449
Joined: Mon Jul 07, 2003 3:15 pm
Car: 95 240sx KA-T
Contact:

Post

ok, we got C++ and VB.. format them and I will take a look.

User avatar
MaxFNPower
Posts: 524
Joined: Sat Jan 12, 2008 10:23 pm
Car: 93 240sx SE Coupe KA24DE (My Dorthy) 97 240sx Coupe KA24DE(Un-Named) 87 Nissan Pick-up (Tin Man)

Post

Actually mine was dark basic, and we are just doing this to compare languages.

To really look at mine you need DB demo and my folder full of stuff. If I still had DB on my computer I would compile and pack you an .exe and let you check it out and also take a look at my code.

If your interested it was a barely started attempt at an R-Type style shooter. And I wouldn't mind zipping and emailing you the folder.

User avatar
brizanden
Posts: 6064
Joined: Mon Apr 30, 2007 8:04 am
Car: thrased kouki ftw
Contact:

Post

^do me do me lolps. i dont know a thing about programming games or the dark basic lang, but it was as i suspected a lot of if statements im guessing that require a key to be pressed then a sutibale sound/ animation goes along. am i correct?

User avatar
GEO
Posts: 6449
Joined: Mon Jul 07, 2003 3:15 pm
Car: 95 240sx KA-T
Contact:

Post

Dark basic? Never heard of it. Whats it based off?

User avatar
MaxFNPower
Posts: 524
Joined: Sat Jan 12, 2008 10:23 pm
Car: 93 240sx SE Coupe KA24DE (My Dorthy) 97 240sx Coupe KA24DE(Un-Named) 87 Nissan Pick-up (Tin Man)

Post

I think it's based on Visual Basic, but it's more slighted for making 3D games. It also does 2d pretty well.

I don't have it installed but as soon as I do my reinstall I'll have to get it back up. I'm going to play with creating simple AI.


Return to “Computers / Electronics”