| | |
Accessing a constructor of a different class
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 78
Reputation:
Solved Threads: 0
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:
Here is the code:
C++ Syntax (Toggle Plain Text)
SString::SString(char *array, int size) { int i; _string = new char[size + 1]; // +1 for the string's null terminator character for(i=0; i < size; i++) { _string[i] = array[i]; } _string[i] = 0; // add string's null terminating character }
C++ Syntax (Toggle Plain Text)
class CD { private: SString artist, title, descrip; int year; public: CD(); CD(SString artist, SString title, SString descrip, int year); SString get_artist(); SString get_title(); SString get_descrip(); int get_year(); void set_artist(char &newArtist); void set_title(char &newTitle); void set_descrip(char &descrip); void set_year(int newYear); void print(SString artist, SString title, SString descrip, int year); friend std::istream& operator >>(std::istream &is, CD &CD); };
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
call the constructors like in the following snippet:
cpp Syntax (Toggle Plain Text)
class A{ string x,y,z; public: A(string a, string b, string c) : x(a), y(b), z(c) {} };
Last edited by dougy83; Sep 16th, 2008 at 12:24 pm.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Template class not converting
- Next Thread: Can anyone figure out what is wrong in my program?
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data database delete desktop directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





