#include <iostream> #include <cstring> #include <vector> using namespace std; // forward declaration class OOPMP; class Song { private: char *name; char *lyrics; protected: virtual void privatePlay() { play(); } public: Song(const char *name, const char *lyrics) { // Allocate memory this->name = new char[strlen(name) + 1]; this->lyrics = new char[strlen(lyrics) + 1]; // copy strings strcpy(this->name, name); strcpy(this->lyrics, lyrics); } const char *getName() { return name; } const char *getLyrics() { return lyrics; } ~Song() { delete[] name; delete[] lyrics; } virtual void play() = 0; friend OOPMP; }; class MP3 : public Song { char *type; public: MP3(const char *name, const char *lyrics, const char *type) : Song(name, lyrics) { this->type = new char[strlen(type) + 1]; strcpy(this->type, type); } virtual void play() { cout << "TYPE:\n"; cout << type << '\n'; cout << "LYRICS:\n"; cout << getLyrics() << "\n\n"; } ~MP3() { delete[] type; } }; class MP4 : public Song { char *video; public: MP4(const char *name, const char *lyrics, const char *video) : Song(name, lyrics) { this->video = new char[strlen(video) + 1]; strcpy(this->video, video); } const char *getVideo() { return video; } void play() { cout << "LYRICS:\n"; cout << getLyrics() << '\n'; cout << "VIDEO:\n"; cout << video << "\n\n"; } ~MP4() { delete[] video; } }; class sMP4 : public MP4 { protected: virtual void privatePlay() { MP4::play(); } public: sMP4(const char *name, const char *lyrics, const char *video) : MP4(name, lyrics, video) {} void play() { cout << "Lyrics:\nbleeeeeeep\n"; cout << "Video:\nglitchhhh\n\n"; } }; class OOPMP { vector<Song*> songs; public: void addToPlayList(Song *song) { songs.push_back(song); } void playAll() { for (Song *song : songs) { cout << "Currently Playing: " << song->getName() << endl; song->privatePlay(); } } }; int main() { /*Creating MP3 song, arguments are name of song, lyrics and type*/ MP3 *s1 = new MP3("Happy", "Because I am happy...", "pop"); // play s1, it will just print the lyrics s1->play(); /*Creating MP4 song, arguments are name of song, lyrics and description of video*/ MP4 *s2 = new MP4("Happy", "Because I am happy...", "A man walking.."); s2->play(); /*Creating sMP4 song, arguments are name of song, lyrics and description of video*/ sMP4 *s3 = new sMP4("Born Free", "Born free...", "Different pictures\nappearing on screen..."); // play s1, it will just print the lyrics s3->play(); /*note that playing song of sMP4 does not show real lyrics and video*/ // Creating a music player of type OOPMP OOPMP myPlayer; // Adding songs to OOPMP myPlayer.addToPlayList(s1); myPlayer.addToPlayList(s2); myPlayer.addToPlayList(s3); /*invoking member functions to play all songs in playlist note that now sMP4 song shows the real lyrics and video*/ myPlayer.playAll(); return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}