C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 18th, 2003, 02:58 AM
flatline flatline is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 1 flatline User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
trouble with has-a class in c++

I'm just starting to learn c++ and I'm working through a book. I have a confusion relating to class usage--I'm not even sure what I don't understand but I think I can describe it. To give an example of a has-a class paraphrased from the book:

Code:
class Point     // holds x,y coordinates
 {
 public:
     void SetX(int x) { itsX = x; }
     void SetY(int y) { itsY = y; }
     int GetX()const { return itsX;}
     int GetY()const { return itsY;}
 private:
     int itsX;
     int itsY;
 };

 class  Rectangle
 {

<code omitted>

private:
     Point  itsUpperLeft;
     Point  itsUpperRight;
     Point  itsLowerLeft;
     Point  itsLowerRight;

     int    itsTop;
     int    itsLeft;
     int    itsBottom;
     int    itsRight;
};


Here's my question: WHY are we declaring variables of type Point? I can see that Point can hold 8 bytes assuming that each integer is 4 bytes (from itsX and itsY)...and I understand that Point is now a user-defined type just like int is a built-in type...but what now are the properties of itsUpperLeft, etc.? Point is then used in other places in the code as a return type for several member functions of Rectangle, which seems to present the same problem in my mind.

Many Thanks!

Reply With Quote
  #2  
Old July 17th, 2004, 11:18 AM
Mystro89 Mystro89 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 1 Mystro89 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Answer

Here's my question: WHY are we declaring variables of type Point? I can see that Point can hold 8 bytes assuming that each integer is 4 bytes (from itsX and itsY)...and I understand that Point is now a user-defined type just like int is a built-in type...but what now are the properties of itsUpperLeft, etc.? Point is then used in other places in the code as a return type for several member functions of Rectangle, which seems to present the same problem in my mind.

I copied this so I could remember the question.

I think you declare variables of type point so you have access to the member functions. If they didnt declare itsUpperLeft as type Point this
itsUpperLeft.SetX(left);
itsUpperLeft.SetY(top);
on lines 10-11(8.4 Rect.cpp) wouldn't work because they arent of type Point. The properties are the same as a Point. Since Point is now like a built in type you can return it from a function.
Hopes this helps.If not tryposting it at this website http://www.jeffcogswell.com

Reply With Quote
  #3  
Old July 17th, 2004, 12:37 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Hey guys I've been trying to find a way to say this without sounding rude but I'm not sure if its worked or not. Please don't take offence I'm just trying to help.

Based on the questions you are asking it looks like you don't understand some of the core concepts of using classes in code. While I could explain there are enough tutorials all over the net that can help you out just as well. My advice is to find one and rip apart the code until you see whats going on.

Hope this helps,

-KM-

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > trouble with has-a class in c++


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT