OOP Programs
OOP Programs
#include <iostream>
class Pet {
public:
int age;
void getPet() {
void showPet() {
cout << "Age: " << age << " years" << endl;
int humanYears() {
};
string favoriteToy;
public:
void getDog() {
getPet();
void showDog() {
showPet();
cout << "Age in human years: " << humanYears() << endl;
};
float wingspan;
public:
void getBird() {
getPet();
}
void showBird() {
showPet();
cout << "Wingspan: " << wingspan << " cm" << endl;
cout << "Age in human years: " << humanYears() << endl;
};
int main() {
Dog myDog;
Bird myBird;
myDog.getDog();
myBird.getBird();
myDog.showDog();
myBird.showBird();
return 0;
( OUTPUT )
--- Enter Dog Details ---
Name: Buddy
Species: Labrador
Age: 5 years
Name: Tweety
Species: Parrot
Age: 2 years
Wingspan: 30.5 cm
( Program 2 )
#include <iostream>
using namespace std;
class Reservation {
protected:
int id;
string name;
string date;
public:
void getDetails() {
void showDetails() {
void modify() {
}
};
int roomNo;
public:
void getHotel() {
getDetails();
void showHotel() {
showDetails();
};
int seatNo;
public:
void getTrain() {
getDetails();
void showTrain() {
showDetails();
}
};
int main() {
int choice;
if (choice == 1) {
HotelReservation h;
h.getHotel();
h.showHotel();
if (c == 'y') h.modify();
} else if (choice == 2) {
TrainReservation t;
t.getTrain();
t.showTrain();
if (c == 'y') t.modify();
} else {
return 0;
}
( Outputs )
1. Hotel
2. Train
Choice: 1
Customer: John
Date: 15/08/2023
Room: 302
Modify? (y/n): y
Reservation Updated!
( Program 3 )
#include <iostream>
class TrafficLight {
string color;
int duration;
public:
color = newColor;
duration = newTime;
cout << "Light changed to " << color << " (" << duration << "s)\n";
void show() {
cout << "Current: " << color << " (" << duration << "s)\n";
};
int main() {
light.show();
light.change("green", 25);
light.show();
return 0;
( Outputs )
! Current: red (30s)
STOP
GO!
( Program 4 )
#include <iostream>
class Course {
protected:
int credits;
public:
void display() {
};
string platform;
int months;
public:
void checkCertificate() {
display();
cout << "Certificate: " << (months >= 3 ? "Yes" : "No") << endl;
};
int main() {
c1.checkCertificate();
return 0;
( Outputs )
Course: C++ Basics
Credits: 3
Platform: Udemy
Certificate: Yes
( Program 5 )
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
private:
string songs[10];
int totalSongs = 0;
public:
songs[totalSongs] = songName;
totalSongs++;
} else {
if (songs[i] == songName) {
totalSongs--;
found = true;
break;
}
if (!found) {
void playRandomSong() {
if (totalSongs == 0) {
return;
srand(time(0));
cout << "Now playing: **" << songs[randomIndex] << "**" <<
endl;
void showSongs() {
if (totalSongs == 0) {
return;
}
};
int main() {
MusicLibrary myPlaylist;
int choice;
string songName;
do {
switch (choice) {
case 1:
getline(cin, songName);
myPlaylist.addSong(songName);
break;
case 2:
myPlaylist.showSongs();
break;
case 3:
myPlaylist.playRandomSong();
break;
case 4:
getline(cin, songName);
myPlaylist.removeSong(songName);
break;
case 5:
break;
default:
return 0;
( Outputs )
===== Music Library Menu =====
1. Add Song
4. Remove Song
5. Exit
1. Add Song
4. Remove Song
5. Exit
1. Add Song
4. Remove Song
5. Exit
1. Bohemian Rhapsody
2. Stairway to Heaven
1. Add Song
5. Exit
1. Add Song
4. Remove Song
5. Exit
Exiting... Goodbye!