C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
  #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,969 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 1 Day 22 h 39 m 55 sec
Reputation Power: 184
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,969 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 1 Day 22 h 39 m 55 sec
Reputation Power: 184
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


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway