Helpful Information
 
 
Category: C Programming
Questions regarding C++ Classes

Hi, teaching myself C++ and require some help / assistance.

Source Code 1


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

class Time;

// date class
class Date {
int mo, da, yr;
public:
Date(int m, int d, int y) {mo = m; da = d; yr = y;}
friend void display(const Date&, const Time&); // bridge function
};

// time class
class Time{
int hr, min, sec;
public:
Time (int h, int m, int s) {hr = h; min = m; sec = s;}
friend void display(const Date&, const Time&); // bridge function
};

// a bridge friend function
void display(const Date& dt, const Time& tm)
{
cout << dt.mo << '/' << dt.da << '/' << dt.yr;
cout << ' ';
cout << tm.hr << ':' << tm.min << ':' << setfill('0') << right << setw(2) << tm.sec;
}

int main()
{
Date dt(2, 16, 97);
Time tm(10, 55, 5);
display(dt, tm);
return 0;
}

1st Question:-
I've added the iomanip header and cout manipulators [ setfill('0') << right << setw(2) ] to the orignal source so that the display shows:
2/16/97 10:55:05 instead of 2/16/97 10:55:5

Is this an acceptable way of manipulating the data members or are there better alternatives?


Source Code 2


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

// date class
class Date {
int mo, da, yr;
char *month;
public:
Date(int m = 0, int d = 0, int y = 0);
~Date();
void display() const;
};

Date::Date(int m, int d, int y)
{
static char *mos[] ={ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};

mo = m; da = d; yr = y;
if (m !=0) {
month = new char[strlen(mos[m-1])+1];
strcpy(month, mos[m-1]);
}
else
month = 0;
}

// destructor
Date::~Date()
{
delete[] month;
}

// display member function
void Date::display() const
{
if (month != 0)
cout << month << ' ' << da << ", " << yr;
else
cout << da << ", " << yr;
}

int main()
{
Date dt(5, 24, 2000);
dt.display();
return 0;
}

2nd Question:-
Referring to the Date function in the Date class, I can't understand the following assignment:

month = new char[strlen(mos[m-1])+1];

Since the following assignment will work just fine:

month = new char[strlen(mos[m-1])];

Are there any reasons to assign month the former way?

to your second question:

strings are terminated with a trailing \0. this is why you need to add one to the actual string length or it will start overwriting your next variable.
a single byte would do no harm on small programs since the 8 MSBs are anyway 0. but if you use a lot of memory or write drivers (or some other things probably too...) then it will kill the next variable on the heap when you strcpy() to it...

Thanks for the help. :)

Since I'm not familiar with programming terminology and acronyms, when you mention MSB do you mean Memory Storage Buffer?

i referred to "Most Significant Bits" - if you have 32 bit (like in all pointers in 32-bit C), this is the left-most 8 that i talked about :) (=one byte for your "\0")










privacy (GDPR)