Hello: I am writting a Date Class, and I want to overload operator - that can take a Date object, I wrote the code, but I felt it is stupid, Please help me: *************************************************************************** int Date::days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int Isbigger(int year, int right_year) { int bigger; bigger = year > right_year?year:right_year; returen bigger; }//get the bigger value of two int Issmaller(int year, int right_year) { int smaller; smaller = year < right_year? year:right_year; return smaller; }//get the smaller value of two int Date::operator -(Date right) { int subday; int subyear; if (year == right.year) { subyear = 0; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } } else //not in the same year { int yearDifference; yearDifference = Isbigger(year,right.year) - 1; subyear = yearDifference * 365; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } subday += subyear; } return subday; } Thank You So Much
foxele
Posts
-
operator- in Date Class -
Bookshello, I am a student I am using "Learning C++ A Hand On Approach" second edition by Eric Nagler this book is good for starter like me. just suggestion. from foxele
-
dvdplayer class and player classhello, I wrote a player class and I also wrote a dvdplayer class that is supposed to inherit from player class(both are very simple, no GUI, just simulateion) my question: 1. in dvdplayer what should I put as the type of disk tray and media reader, should I create a new class? //dvdplayer.h #ifndef DVDPLAYER_H #define DVDPLAYER_H #include "player.h" #include #include "mystring.h" #include "button.h" #include "terminal.h" class dvdplayer:public player { public: dvdplayer(const char * info); ~dvdplayer(); virtual int play(); virtual int stop(); virtual int powerOn(); virtual int powerOff(); int videoConnect(); int audioConnect(); void insert(); void remove(); private: terminal vedio_out; terminal audio_out; //disk_tray; //media_reader; }; #endif 2. Can somebody give me some suggestion on both player and dvdplayer class? I am a student who just start to learn C++, Thanks for your help! //player.h #ifndef PLAYER_H #define PLAYER_H #include "mystring.h" #include "terminal.h" #include "button.h" class player { public: Player(const char * info); virtual ~player(); void getPlayerInfo(); //enum status {Playing, Paused, Stopped, Ejected, Open, Close, connected, disconnected); void acConnect(); void setPlayerInfo(); //void acConnect(); void acDisconnect(); virtual int play() = 0; virtual int stop() = 0; virtual int powerOn() = 0; virtual int powerOff() = 0; protected: button player; button stop; button power; mystring PlayerInfo(); terminal AC; }; #endif _____________________________________ #include "player.h" player::player(const char* info) { setPlayerInfo(info); AC.setState(0); //set 0 to terminal ac denotes disconnect; } //constructor; player::~player() { delete playerInfo; delete polayer; delete power; delete stop; delete AC; } //destructor; void player::acConnect(); { if(AC.getState == 0) AC.setState(1); else cout<<"AC has already connected"<
-
how to search through an external fileI have an external file in notepad(.txt) and I have many lines of records in it: example: my name is jim I have a car it is a beautiful day . . . If I want to store the next four lines start from "my name is jim" to an array of string class, how can I do it? I was trying to use strcmp to find line, but after I locate the line, I can only store one line into a string class. How can I store the following three lines into the string classes?
-
where did I do wrong? (myString class) Please help! I have to turn in tomorrowMaxwell, did you test the myString class with your c++ compiler? I still got the same four errors about my destructor and operator<< and constructor. If it works fine in your computer, then it is the problem of my compiler. Please let me know . Thank You So Much from foxele
-
where did I do wrong? (myString class) Please help! I have to turn in tomorrowCan u send those modified code as attachment in header file and source file, cause I couldn't download them, I don't know why thanks agian!
-
where did I do wrong? (myString class) Please help! I have to turn in tomorrowThanks for the help, I haven't check it yet. but I think it will work fine. Thanks again from foxele
-
where did I do wrong? (myString class) Please help! I have to turn in tomorrowI wrote a string class and try to test with a very simple driver program, but there are some erros, can somebody help me figure out my mistakes? thank you! --------------Configuration: main - Win32 Debug-------------------- Compiling... main.cpp Linking... main.obj : error LNK2001: unresolved external symbol "public: __thiscall myString::~myString(void)" (??1myString@@QAE@XZ) main.obj : error LNK2001: unresolved external symbol "class ostream & __cdecl operator<<(class ostream &,class myString const &)" (??6@YAAAVostream@@AAV0@ABVmyString@@@Z) main.obj : error LNK2001: unresolved external symbol "public: __thiscall myString::myString(char const *)" (??0myString@@QAE@PBD@Z) Debug/main.exe : fatal error LNK1120: 3 unresolved externals Error executing link.exe. main.exe - 4 error(s), 0 warning(s) ____________________________________________________________________________ myString.h file: //myString #ifndef MYSTRING_H #define MYSTRING_H #include class myString{ friend ostream &operator<<(ostream &, const myString &); friend istream &operator>>(istream &, myString &); public: myString( const char * = ""); myString(const myString &); ~myString(); const myString &operator= (const myString &); //assign string myString &operator+= (const myString &); int operator! () const; //empty string int operator== (const myString &) const; int operator!= (const myString &) const; int operator< (const myString &) const; int operator> (const myString &) const; int operator>= (const myString &) const; int operator<= (const myString &) const; char &operator[] (int); myString &operator()(int, int); int getLength() const; // void toUpper (myString &); // void toLower (myString &); private: char* sPtr; int length; }; #endif ________________________________________________________________________ myString.cpp file //myString.cpp #include #include #include #include"myString.h" myString::myString(const char *s) { lengh = strlen(s); sPtr = new char[lengh +1]; assert(sPtr!=0); strcpy(sPtr, s); } //convention constructor myString::myString(const myString ©) { lengh = copy.length; sptr = new char [length +1]; assert(sPtr!= 0 ); strcpy(sPtr, copy.sPtr); } //copy constructor myString::~myString() { delete [] sPtr; } //destructor const myString &myString::operator = (const myString &right) { if(&right! = this) { delete [] sPtr; length = right.length;
-
DVD ClassI am a beginner of C++ programming. Please help me I came across this problem of creating a DVD class. The part I don't understand is in my handout for project, the instuctor asked us to assume DVD consists of a number of audio/video chapters, How can I store data(like lyric of a song) into those Chapters? Should I create a class named Chapters and use an array of Chapters in DVD class? I mean we only pass data through like: DVD* dvd = new DVD("show me the meaning of being lonly"; but all I can do is store one line into the class, how can I store the whole lyric into the class? hu yu