C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 19th, 2002, 05:47 AM
Ratt Ratt is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 6 Ratt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old March 19th, 2002, 12:33 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,966 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 52 m 24 sec
Reputation Power: 189
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...
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old March 19th, 2002, 01:27 PM
Ratt Ratt is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 6 Ratt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old March 19th, 2002, 01:31 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,966 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 52 m 24 sec
Reputation Power: 189
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")

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Questions regarding C++ Classes

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap