Author Topic: I passed the C++ exam, 5 more to go  (Read 986 times)

0 Members and 1 Guest are viewing this topic.

Offline IslamIsCancer

  • Master JTFer
  • ******
  • Posts: 1346
  • My name is Stalinashvili.
I passed the C++ exam, 5 more to go
« 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()
{

}*/

« Last Edit: June 24, 2008, 11:33:58 AM by IslamIsCancer »
Free Javakh!!!
I defecate on Stalin's grave.

Offline mord

  • Global Moderator
  • Platinum JTF Member
  • *
  • Posts: 25853
Re: I passed the C++ exam, 5 more to go
« Reply #1 on: June 24, 2008, 11:34:02 AM »
I have no idea but congratulations O0 O0 O0 O0
Thy destroyers and they that make thee waste shall go forth of thee.  Isaiah 49:17

 
Shot at 2010-01-03

Offline Dan

  • Moderator
  • Ultimate JTFer
  • *
  • Posts: 4308
Re: I passed the C++ exam, 5 more to go
« Reply #2 on: June 24, 2008, 11:35:59 AM »
Congrats ISC!  O0
 ...can you briefly describe you achievement?

Offline Ulli

  • Honorable Winged Member
  • Gold Star JTF Member
  • *
  • Posts: 10946
Re: I passed the C++ exam, 5 more to go
« Reply #3 on: June 24, 2008, 11:39:19 AM »
Congratulations IslamisCancer. I wish for you only the best  O0
"Cities run by progressives don't know how to police. ... Thirty cities went up last night, I went and looked at every one of them. Every one of them has a progressive Democratic mayor." Rudolph Giuliani

Offline Dr. Dan

  • Forum Administrator
  • Gold Star JTF Member
  • *
  • Posts: 12588
Re: I passed the C++ exam, 5 more to go
« Reply #4 on: June 24, 2008, 11:40:54 AM »
Does it solve the energy crisis in the world?
If someone says something bad about you, say something nice about them. That way, both of you would be lying.

In your heart you know WE are right and in your guts you know THEY are nuts!

"Science without religion is lame; Religion without science is blind."  - Albert Einstein

Offline IslamIsCancer

  • Master JTFer
  • ******
  • Posts: 1346
  • My name is Stalinashvili.
Re: I passed the C++ exam, 5 more to go
« Reply #5 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 :)
Free Javakh!!!
I defecate on Stalin's grave.

Offline Boeregeneraal

  • Master JTFer
  • ******
  • Posts: 2257
Re: I passed the C++ exam, 5 more to go
« Reply #6 on: June 24, 2008, 11:53:00 AM »
ah fantastic! Congrats!

Im getting into C# myself

Offline Americanhero1

  • Silver Star JTF Member
  • ********
  • Posts: 7617
  • I ain't going anywhere
Re: I passed the C++ exam, 5 more to go
« Reply #7 on: June 24, 2008, 11:53:15 AM »
Congrats IslamIsCancer O0 O0

Offline Еврей

  • Senior JTFer
  • ****
  • Posts: 451
Re: I passed the C++ exam, 5 more to go
« Reply #8 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
"My name is Daniel Pearl. I am a Jewish American"
"My family follows Judaism. We've made numerous family visits to Israel."
~Daniel Pearl

"We are a small nation, but strong."
~Moshe Dayan

"First of all, Arafat is wrong. Jerusalem is Israel's capital, will never be divided, and will remain the capital of the State of Israel, the capital of the Jewish people, for ever and ever. "
~Benjamin Netanyahu

ביחד נשרוד, לחוד ניפול
Together we survive, apart we fall
~Subliminal