Accessing a constructor of a different class

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 78
Reputation: nizbit is an unknown quantity at this point 
Solved Threads: 0
nizbit nizbit is offline Offline
Junior Poster in Training

Accessing a constructor of a different class

 
0
  #1
Sep 16th, 2008
I have a class named SString, which is a user defined c style string class. The constructor for SString dynamically allocates a char array. Then I have a CD class which contains data about a specific CD, like artist, title, year, etc. They are defined as SString instances. I want to allocate an array using SString's constructor for the different CD data. Can I make a call to SString's constructor? Or since it is an instance of the SString class, the default constructor was called? Or should I be implementing this in my get methods? I'm not exactly sure how to do this.


Here is the code:

  1. SString::SString(char *array, int size)
  2. {
  3. int i;
  4. _string = new char[size + 1]; // +1 for the string's null terminator character
  5. for(i=0; i < size; i++)
  6. {
  7. _string[i] = array[i];
  8. }
  9. _string[i] = 0; // add string's null terminating character
  10. }
  1. class CD
  2. {
  3. private:
  4. SString artist, title, descrip;
  5. int year;
  6.  
  7. public:
  8. CD();
  9. CD(SString artist, SString title, SString descrip, int year);
  10. SString get_artist();
  11. SString get_title();
  12. SString get_descrip();
  13. int get_year();
  14. void set_artist(char &newArtist);
  15. void set_title(char &newTitle);
  16. void set_descrip(char &descrip);
  17. void set_year(int newYear);
  18. void print(SString artist, SString title, SString descrip, int year);
  19. friend std::istream& operator >>(std::istream &is, CD &CD);
  20. };
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Accessing a constructor of a different class

 
0
  #2
Sep 16th, 2008
call the constructors like in the following snippet:
  1. class A{
  2. string x,y,z;
  3. public:
  4. A(string a, string b, string c) : x(a), y(b), z(c) {}
  5. };
Last edited by dougy83; Sep 16th, 2008 at 12:24 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC