JTF.ORG Forum

General Category => General Discussion => Topic started by: IslamIsCancer on June 24, 2008, 11:32:22 AM

Title: I passed the C++ exam, 5 more to go
Post by: IslamIsCancer on June 24, 2008, 11:32:22 AM
Hi folks, if anybody knows C++ you can check my code with MSVC compiler.
The += operator overloading doesn't work but the rest is kool.
One main.cpp
3 headers and 3 cpps for the implementation.

#include <iostream>
using namespace std;

#include "time.h"
#include "dvd.h"
#include "dvdchanger.h"

//---------------------------- main() ----------------------------
void main() {
   cout << "---------- DVD changer ---------------- " << endl;
// handle Time
   Time time1(3,40,10);
   Time time2(0,19,50);
   Time tx(time1);
   cout << "test Time copy constr : " ; cout << tx <<  endl;
   Time time3 ;
   time3 = time1 + time2;
   cout << "test Time op +        : " ; cout << time3 << endl;
   cout << endl;
//------------- one R/W DVDs ---------------------------
   DVD empty_dvd("Empty", Time(0,0,0), "DVD RW" );
   cout << "DVD RW:" << endl;
   empty_dvd.show_DVD();
   cout << endl;
//------------- handle 3 DVDs ---------------------------
   DVD Rolling_Stones_dvd("Rolling Stones", Time(3,40,01) );
   Rolling_Stones_dvd.show_DVD();

   DVD Beatles_dvd("Beatles", Time(3,40,02) );
   Beatles_dvd.show_DVD();

   DVD Frank_Zappa("Frank Zappa", Time(2,59,59) );
   Frank_Zappa.show_DVD();

//------------- handle the DVD changer ----------------
   cout << endl;
   DVD_changer dvd_changer;
   dvd_changer.show_status();

   dvd_changer += Rolling_Stones_dvd;
   dvd_changer.show_status();

// put in more CDs
   dvd_changer += Beatles_dvd;
   dvd_changer += Frank_Zappa;
   dvd_changer.show_status();

// play Frank Zappa
   dvd_changer.play_dvd(2);

   cout << "Done " << endl;
}



#include <iostream>
using namespace std;
#include "time.h"
#ifndef _dvd_h_
#define _dvd_h_

class DVD {
private:
   char* dvd_title;
   Time play_time;
    char* dvd_type;
public:
   DVD(char*  = "no DVD", Time = Time(0,0,0), char* = "DVD-R");
   //dtor
   ~DVD();
   void show_DVD();
// add  may be some functions here If possible use references !!
   DVD(const DVD & original);

   char*get_title();
   char*get_type();
   Time get_time();

};

#endif //_dvd_h_

#include <iostream>
using namespace std;
#include "dvd.h"
#include "time.h"
#include <string.h>


DVD::DVD(char* a_title, Time a_time, char* a_type )
{
   //init. title
   dvd_title = new char[strlen(a_title)+1];
   strcpy(dvd_title, a_title);
   //init. type
   dvd_type = new char[strlen(a_type) +1];
   strcpy(dvd_type, a_type);
   //init. play_time
   play_time = Time(a_time);
}


DVD::DVD(const DVD & original)
{
   //title
   dvd_title = new char[strlen(original.dvd_title) +1];
   strcpy(dvd_title, original.dvd_title);
   //type
   dvd_type = new char[strlen(original.dvd_type) +1];
   strcpy(dvd_type, original.dvd_type);
   //time
   play_time = Time(original.play_time);
}

void DVD::show_DVD()
{
   cout << dvd_title << " DVD is of type " << dvd_type << ", Time ";
   cout << play_time << endl;
}

DVD::~DVD()
{
   if (dvd_title != NULL) delete[] dvd_title;
   if (dvd_title != NULL) delete[] dvd_type;
}


char*DVD::get_title()
{
   return dvd_title;
}
char*DVD::get_type()
{
   return dvd_type;
}
Time DVD::get_time()
{
   return play_time;
}

#ifndef _time_h_
#define _time_h_

#include <iostream>
using namespace std;
class Time {
private:
   int hours, min, sec;
public:
   Time();
   Time(int h, int m=0, int s=0);
   //additional CCTOR
   Time(const Time & original);
   Time operator+( Time & t);

   ~Time();
   friend ostream & operator <<(ostream &, Time &);
//   friend class DVD;
   int get_hours();
   int get_min();
   int get_sec();
};

#endif //_time_h_


#include "time.h"

//default ctor
Time::Time()
{
   hours = min = sec = 0;
}

//overloaded ctor
Time::Time(int h, int m, int s)
{
   hours = h; min = m; sec = s;
}

//copy ctor
Time::Time(const Time & original)
{
   hours = original.hours;
   min = original.min;
   sec = original.sec;
}

//overloading + operator
Time Time::operator+( Time & t )
{
   Time time_sum;
   time_sum.sec = (hours * 3600 + min * 60 + sec)
   + (t.hours * 3600 + t.min * 60 + t.sec);

   time_sum.min = time_sum.sec / 60;
   time_sum.hours = time_sum.min / 60;
   time_sum.min = time_sum.min % 60;
   time_sum.sec = time_sum.sec % 60;
   return time_sum;
}


//dtor
Time::~Time()
{
   //new is not used, there's nothing to delete
}

ostream & operator<<(ostream & os, Time & tm)
{
   os<< tm.hours << " : " << tm.min << " : " << tm.sec <<endl;
   return os;
}

int Time::get_hours()
{
   return hours;
}

int Time::get_min()
{
   return min;
}
int Time::get_sec()
{
   return sec;
}

#include <iostream>
using namespace std;
#include "time.h"
#include "dvd.h"

#ifndef _dvdchanger_h_
#define _dvdchanger_h_

class DVD_changer {
private:
   DVD **pDVD;
   int current_number_of_DVDs;
   Time total_play_time;
public:
   DVD_changer();
   ~DVD_changer();
   void show_status() ;
   void operator+=(DVD &);
   void play_dvd(int i);
};

#endif //_dvdchanger_h_

#include "dvdchanger.h"
#include "dvd.h"
#include "time.h"

DVD_changer::DVD_changer()
{
   pDVD = NULL;
   current_number_of_DVDs = 0;
   total_play_time = Time(0,0,0);
}

DVD_changer::~DVD_changer()
{
}

void DVD_changer::show_status()
{
   cout << "DVD changer contains " << current_number_of_DVDs << " DVDs, total play time is ";
   cout << total_play_time << endl;
}


void DVD_changer::operator+=(DVD & a_dvd)
{
   current_number_of_DVDs++;
// total_play_time = total_play_time + a_dvd.get_time;

   DVD**placeholder;
   placeholder = pDVD;

   if (pDVD != 0) delete pDVD;
   current_number_of_DVDs++;

   pDVD = new DVD*[current_number_of_DVDs + 1];

   memcpy(pDVD, placeholder, current_number_of_DVDs-1);
   
   pDVD[current_number_of_DVDs] = &a_dvd;
}


void DVD_changer::play_dvd(int i)
{
   cout << "playing..., " << pDVD->get_title << " DVD is of type " << pDVD->get_type << ", Time ";
   cout << pDVD->get_time;
}
/*
DVD_changer::~DVD_changer()
{

}*/

Title: Re: I passed the C++ exam, 5 more to go
Post by: mord on June 24, 2008, 11:34:02 AM
I have no idea but congratulations O0 O0 O0 O0
Title: Re: I passed the C++ exam, 5 more to go
Post by: Dan on June 24, 2008, 11:35:59 AM
Congrats ISC!  O0
 ...can you briefly describe you achievement?
Title: Re: I passed the C++ exam, 5 more to go
Post by: Ulli on June 24, 2008, 11:39:19 AM
Congratulations IslamisCancer. I wish for you only the best  O0
Title: Re: I passed the C++ exam, 5 more to go
Post by: Dr. Dan on June 24, 2008, 11:40:54 AM
Does it solve the energy crisis in the world?
Title: Re: I passed the C++ exam, 5 more to go
Post by: IslamIsCancer on June 24, 2008, 11:47:08 AM
Thanks mord, Dan, pheasant =)

Dan, Time class shows you the time hours mins and seconds.
You can add times like 2 hours 40 mins + 1 hour 30 mins,
it won't be 3 hours and 70 mins but 4 hours and 10 mins.
So this is needed for our DVD changer.
Basically this is supposed to be a quasi DVD player lol :)
Title: Re: I passed the C++ exam, 5 more to go
Post by: Boeregeneraal on June 24, 2008, 11:53:00 AM
ah fantastic! Congrats!

Im getting into C# myself
Title: Re: I passed the C++ exam, 5 more to go
Post by: Americanhero1 on June 24, 2008, 11:53:15 AM
Congrats IslamIsCancer O0 O0
(http://www.freejigsawpuzzles.com/images/CONGRATULATIONS.jpg)
Title: Re: I passed the C++ exam, 5 more to go
Post by: Еврей on June 24, 2008, 03:00:19 PM
Very cool!

I'm actually learning Java, myself.

Eventually I plan on learning C++... good stuff!  O0