The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Questions regarding C++ Classes
Discuss Questions regarding C++ Classes in the C Programming forum on Dev Shed. Questions regarding C++ Classes C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 19th, 2002, 05:47 AM
|
|
Junior Member
|
|
Join Date: Mar 2002
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Questions regarding C++ Classes
Hi, teaching myself C++ and require some help / assistance.
Source Code 1
Code:
#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
Code:
#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?
|

March 19th, 2002, 12:33 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
|
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...
|

March 19th, 2002, 01:27 PM
|
|
Junior Member
|
|
Join Date: Mar 2002
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Thanks for the help.
Since I'm not familiar with programming terminology and acronyms, when you mention MSB do you mean Memory Storage Buffer?
|

March 19th, 2002, 01:31 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
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")
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|