2 Replies - 708 Views - Last Post: 20 March 2016 - 09:28 AM Rate Topic: -----

#1 rosier7   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 25-October 15

Question about Structure/Class (C++)

Posted 20 March 2016 - 04:43 AM

Hello. I have a question about struct and class in C++. Here is my code:
I was suppose to be able to add song (max 50 song) and display the song list.

#include <iostream>
#include <string>
using namespace std;


class song_class
{
//private:
	//int i;
public :
	void display_info(int a);
	int add_song(int a);
	//void del_song();
	struct song
	{
		string artist[50];
		string song_title[50];
	};//there will be 50 song inside
};

int song_class::add_song(int i)
{
	struct song track;
	cout<<"Enter Song title: ";
	getline (cin,track.song_title[i]);
	getline (cin,track.song_title[i]);
	cout<<"\n\n"<<"Enter artist: ";
	getline (cin,track.artist[i]);
	cout<<"\n\n"<<track.song_title[i]<<" by "<<track.artist[i]<<" has been added to record"<<endl;
	return i;
}
void song_class::display_info(int a)
{
	int track_num;
	struct song track;
	cout<<"Record"<<endl;
	for (track_num = 0;track_num<a;track_num++)
	{
		cout<<"Song title: ";
		cout<<track.song_title[track_num];
		cout<<endl<<"Artist: ";
		cout<<track.artist[track_num]<<endl;
		cout<<"\n\n";
	}
}

int main ()
{
	song_class func;//define class for song_inf
	int choice,i = 0;
	char option;
	do
	{
	cout<<"[][][][][][][][][][][][][]"<<endl;
	cout<<"[]                      []"<<endl;
	cout<<"[]    1. Add Song       []"<<endl;
	cout<<"[]    2. Delete Song    []"<<endl;
	cout<<"[]    3. Print List     []"<<endl;
	cout<<"[]                      []"<<endl;
	cout<<"[][][][][][][][][][][][][]"<<endl;
	cout<<endl;
	cout<<"Please enter your choice: ";
	cin>>choice;
	cout<<"\n\n";

		switch (choice)
		{
		case 1:
			i = func.add_song(i);
			i++;
			break;
			
		/*case 2:
			func.del_song();
			break;*/
			
		case 3:
			func.display_info(i);
			break;
		}
		cout<<"Enter other option? (Y/N): ";
		cin>>option;
		cout<<"\n\n";
	}while (option == 'Y' || option == 'y');

	return 0;
}



My question is,
1. It is appropriate to put struct inside a class?
2. Does struct save the data we insert in it? because I couldn't display the song i insert from the add_song function into display function. (all this time I thought, when you insert data in a struct, it is saved and can be view across all function).
3. How does a constructor/destructor work? I mean why do we need to use the constructor but we still destruct it afterward?

Thank you in advanced. I hope someone can help me because i am still kind of blurry in this topic

Is This A Good Question/Topic? 0
  • +

Replies To: Question about Structure/Class (C++)

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 990
  • View blog
  • Posts: 3,888
  • Joined: 13-June 14

Re: Question about Structure/Class (C++)

Posted 20 March 2016 - 05:17 AM

Classes are supposed to model the things in your system and your design doesn't make much sense. You have the struct song, which should represent a single song, but instead contains arrays for the title and artist. You should use the facilities provided by the standard template library (e.g. std::array or std::vector) to manage a collection of these objects (i.e. a collection of songs). You also have an extra layer song_class, which you probably don't really need.

Also, remember that things are destroyed when they go out of scope. In your add_song() method, you create an instance of song that's local to that function. When that function returns, that object is destroyed.
Was This Post Helpful? 1
  • +
  • -

#3 UG Cyber   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 624
  • Joined: 24-July 08

Re: Question about Structure/Class (C++)

Posted 20 March 2016 - 09:28 AM

Like what ndc said, your struct should only carry one song, then your class should have some sort of an array or vector of your song structure
for example (modified in browser....not tested)
class song_class
{
public :
	void display_info(int a);
	int add_song(int a);
	//void del_song();
	struct song
	{
		string artist; //Removed array of 50
		string song_title; //Removed array of 50
	};//there will be 50 song inside
private :
        vector<song> list; //created a private vector to store songs
};

int song_class::add_song() //removed argument "int i", not needed
{
	song track; //removed "struct"
	cout<<"Enter Song title: ";
	getline (cin,track.song_title); //removed array index int "i"
	//getline (cin,track.song_title[i]); //Commented out double line input for single string
	cout<<"\n\n"<<"Enter artist: ";
	getline (cin,track.artist); //removed array index int "i"
        /*Now that your struct has been filled with data, you want to add it to the vector "list"
          that was added to your class, as ndc stated, once the function exits, it destroys the
          locally defined struct "track"*/
        list.push_back(track);
	cout<<"\n\n"<<track.song_title<<" by "<<track.artist<<" has been added to record"<<endl;
	//replaced "return i;" to return amount of songs stored in your vector
        return list.size();
}
void song_class::display_info()//This function should display entire list. removed argument "int a"
{
	int track_num;
	//struct song track; <- removed, this is a blank structure. To access data, you must access it from the class declaration
	cout<<"Record"<<endl;
	for (track_num = 0;track_num<list.size();track_num++) //replaced a with list.size() which returns amount of songs in list
	{
		cout<<"Song title: ";
		cout<<list[track_num].song_title; //replaced struct "track" with vector stored in class
		cout<<endl<<"Artist: ";
		cout<<list[track_num].artist<<endl; //" "
		cout<<"\n\n";
	}
}



This is just an example, I commented on all of my changes, you may want to read through it to get a good understanding of how its suppose to work, but vectors would really work well for what you are trying to do.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1