#include
#include
class video
{
char title[20];
float length;
char type;
public:
void getvideo()
{
cout<<"Title ";
cin>>title;
cout<<"Length (In Hours) ";
cin>>length;
cout<<"Type (M For Music, D for Dance ";
cin>>type;
}
void printvideo()
{
cout<<"Title "<
cout<<"Length "<
cout<<"Type (M-Music,D-Dance "<
}
int gettype()
{
return type;
}
};
void main()
{
clrscr();
int mcnt=0,dcnt=0;
video v[3];
for(int i=0;i<3;i++)
{
cout<<"\nEnter Details of video "<
v[i].getvideo();
}
for(i=0;i<3;i++)
{
v[i].printvideo();
if(v[i].gettype()=='M' || v[i].gettype()=='m')
mcnt++;
else
dcnt++;
cout<<"\n===========================";
}
cout<<"\nNo. of Music Videos "<
cout<<"\nNo. of Dance Videos "<
}
Description
#include
class video
{
char title[20];
float length;
char type;
public:
void getvideo()
{
cout<<"Title ";
cin>>title;
cout<<"Length (In Hours) ";
cin>>length;
cout<<"Type (M For Music, D for Dance ";
cin>>type;
}
void printvideo()
{
cout<<"Title "<
cout<<"Length "<
cout<<"Type (M-Music,D-Dance "<
}
int gettype()
{
return type;
}
};
void main()
{
clrscr();
int mcnt=0,dcnt=0;
video v[3];
for(int i=0;i<3;i++)
{
cout<<"\nEnter Details of video "<
v[i].getvideo();
}
for(i=0;i<3;i++)
{
v[i].printvideo();
if(v[i].gettype()=='M' || v[i].gettype()=='m')
mcnt++;
else
dcnt++;
cout<<"\n===========================";
}
cout<<"\nNo. of Music Videos "<
cout<<"\nNo. of Dance Videos "<
}
Description
The above code has a class named "video", which contains 3 member variables - title,length and type. The methods "getvideo" and "printvideo" have been used to enter and display their values respectively. The third method named "gettype()" is used to return the "type" of the video which is either "M" or "D" for "Music" and "Dance". Inside "main()", instead of declaring three different objects of the class we have created a single object named "v" but in the form of an array. In terms of memory occupied, this is equivalent to declaring three individual objects of the class but declaring an array object gives us the freedom to use a loop which can not be used with individual objects.
Inside the first loop we have called the "getvideo()" which asks the user to enter the details of the video. After accepting the value for 3 videos, the program runs the second loop which not only prints the data but also checks for their type by calling the "gettype()". Two counter variables named "mcnt" and "dcnt" are incremented each time a "music" or "dance" video is found and the result is printed on the screen.