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:

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
    }
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);
};

call the constructors like in the following snippet:

class A{
   string x,y,z;
public:
   A(string a, string b, string c) : x(a), y(b), z(c) {}
   };
This article has been dead for over six months. Start a new discussion instead.