0% found this document useful (0 votes)
10 views17 pages

OOP Programs

The document contains five C++ programs demonstrating different functionalities. Program 1 manages pet information, Program 2 handles reservations for hotels and trains, Program 3 simulates a traffic light system, Program 4 showcases an online course management system, and Program 5 implements a music library. Each program includes user interaction and outputs relevant information based on user input.

Uploaded by

syedex14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views17 pages

OOP Programs

The document contains five C++ programs demonstrating different functionalities. Program 1 manages pet information, Program 2 handles reservations for hotels and trains, Program 3 simulates a traffic light system, Program 4 showcases an online course management system, and Program 5 implements a music library. Each program includes user interaction and outputs relevant information based on user input.

Uploaded by

syedex14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

( PROGRAM 1 )

#include <iostream>

using namespace std;

class Pet {

public:

string name, species;

int age;

void getPet() {

cout << "Enter pet name: ";

cin >> name;

cout << "Enter pet species: ";

cin >> species;

cout << "Enter pet age: ";

cin >> age;

void showPet() {

cout << "Name: " << name << endl;

cout << "Species: " << species << endl;

cout << "Age: " << age << " years" << endl;

int humanYears() {

return age * 7; // Simple conversion for pet years


}

};

class Dog : public Pet {

string favoriteToy;

public:

void getDog() {

getPet();

cout << "Enter favorite toy: ";

cin >> favoriteToy;

void showDog() {

showPet();

cout << "Favorite Toy: " << favoriteToy << endl;

cout << "Age in human years: " << humanYears() << endl;

};

class Bird : public Pet {

float wingspan;

public:

void getBird() {

getPet();

cout << "Enter wingspan (cm): ";

cin >> wingspan;

}
void showBird() {

showPet();

cout << "Wingspan: " << wingspan << " cm" << endl;

cout << "Age in human years: " << humanYears() << endl;

};

int main() {

Dog myDog;

Bird myBird;

cout << "--- Enter Dog Details ---" << endl;

myDog.getDog();

cout << "\n--- Enter Bird Details ---" << endl;

myBird.getBird();

cout << "\n--- Dog Information ---" << endl;

myDog.showDog();

cout << "\n--- Bird Information ---" << endl;

myBird.showBird();

return 0;

( OUTPUT )
--- Enter Dog Details ---

Enter pet name: Buddy


Enter pet species: Labrador

Enter pet age: 5

Enter favorite toy: Tennis Ball

--- Enter Bird Details ---

Enter pet name: Tweety

Enter pet species: Parrot

Enter pet age: 2

Enter wingspan (cm): 30.5

--- Dog Information ---

Name: Buddy

Species: Labrador

Age: 5 years

Favorite Toy: Tennis Ball

Age in human years: 35

--- Bird Information ---

Name: Tweety

Species: Parrot

Age: 2 years

Wingspan: 30.5 cm

Age in human years: 14

( Program 2 )
#include <iostream>
using namespace std;

class Reservation {

protected:

int id;

string name;

string date;

public:

void getDetails() {

cout << "Reservation ID: ";

cin >> id;

cout << "Customer Name: ";

cin >> name;

cout << "Date (dd/mm/yyyy): ";

cin >> date;

void showDetails() {

cout << "\nReservation ID: " << id << endl;

cout << "Customer: " << name << endl;

cout << "Date: " << date << endl;

void modify() {

cout << "New Name: ";

cin >> name;

cout << "New Date: ";

cin >> date;

cout << "Reservation Updated!\n";

}
};

class HotelReservation : public Reservation {

int roomNo;

public:

void getHotel() {

getDetails();

cout << "Room No: ";

cin >> roomNo;

void showHotel() {

showDetails();

cout << "Room: " << roomNo << endl;

};

class TrainReservation : public Reservation {

int seatNo;

public:

void getTrain() {

getDetails();

cout << "Seat No: ";

cin >> seatNo;

void showTrain() {

showDetails();

cout << "Seat: " << seatNo << endl;

}
};

int main() {

int choice;

cout << "1. Hotel\n2. Train\nChoice: ";

cin >> choice;

if (choice == 1) {

HotelReservation h;

h.getHotel();

h.showHotel();

cout << "Modify? (y/n): ";

char c; cin >> c;

if (c == 'y') h.modify();

} else if (choice == 2) {

TrainReservation t;

t.getTrain();

t.showTrain();

cout << "Modify? (y/n): ";

char c; cin >> c;

if (c == 'y') t.modify();

} else {

cout << "Invalid Choice!\n";

return 0;

}
( Outputs )
1. Hotel

2. Train

Choice: 1

Reservation ID: 101

Customer Name: John

Date (dd/mm/yyyy): 15/08/2023

Room No: 302

Reservation ID: 101

Customer: John

Date: 15/08/2023

Room: 302

Modify? (y/n): y

New Name: John Doe

New Date: 20/08/2023

Reservation Updated!

( Program 3 )

#include <iostream>

using namespace std;

class TrafficLight {

string color;
int duration;

public:

TrafficLight(string c, int d) : color(c), duration(d) {}

void change(string newColor, int newTime) {

color = newColor;

duration = newTime;

cout << "Light changed to " << color << " (" << duration << "s)\n";

void show() {

cout << "Current: " << color << " (" << duration << "s)\n";

if (color == "red") cout << "STOP!\n";

else if (color == "green") cout << "GO!\n";

};

int main() {

TrafficLight light("red", 30);

light.show();

light.change("green", 25);

light.show();

return 0;

( Outputs )
! Current: red (30s)

STOP

Light changed to green (25s)


Current: green (25s)

GO!

( Program 4 )
#include <iostream>

using namespace std;

class Course {

protected:

string name, instructor;

int credits;

public:

Course(string n, string i, int c) : name(n), instructor(i), credits(c) {}

void display() {

cout << "Course: " << name << endl;

cout << "Instructor: " << instructor << endl;

cout << "Credits: " << credits << endl;

};

class OnlineCourse : public Course {

string platform;

int months;

public:

OnlineCourse(string n, string i, int c, string p, int m)

: Course(n, i, c), platform(p), months(m) {}

void checkCertificate() {
display();

cout << "Platform: " << platform << endl;

cout << "Certificate: " << (months >= 3 ? "Yes" : "No") << endl;

};

int main() {

OnlineCourse c1("C++ Basics", "Dr. Smith", 3, "Udemy", 4);

c1.checkCertificate();

return 0;

( Outputs )
Course: C++ Basics

Instructor: Dr. Smith

Credits: 3

Platform: Udemy

Certificate: Yes

( Program 5 )
#include <iostream>

#include <string>

#include <cstdlib>

#include <ctime>

using namespace std;


class MusicLibrary {

private:

string songs[10];

int totalSongs = 0;

public:

void addSong(string songName) {

if (totalSongs < 10) {

songs[totalSongs] = songName;

totalSongs++;

cout << "Song added successfully!" << endl;

} else {

cout << "Library full! Max 10 songs allowed." << endl;

void removeSong(string songName) {

bool found = false;

for (int i = 0; i < totalSongs; i++) {

if (songs[i] == songName) {

// Shift songs left to fill the gap

for (int j = i; j < totalSongs - 1; j++) {

songs[j] = songs[j + 1];

totalSongs--;

found = true;

cout << " Song removed successfully!" << endl;

break;
}

if (!found) {

cout << " Song not found!" << endl;

void playRandomSong() {

if (totalSongs == 0) {

cout << "No songs in the library!" << endl;

return;

srand(time(0));

int randomIndex = rand() % totalSongs;

cout << "Now playing: **" << songs[randomIndex] << "**" <<
endl;

void showSongs() {

if (totalSongs == 0) {

cout << " Library is empty!" << endl;

return;

cout << "\n Your Music Library:" << endl;

for (int i = 0; i < totalSongs; i++) {

cout << i + 1 << ". " << songs[i] << endl;

}
};

int main() {

MusicLibrary myPlaylist;

int choice;

string songName;

do {

cout << "\n===== Music Library Menu =====" << endl;

cout << "1. Add Song" << endl;

cout << "2. Show All Songs" << endl;

cout << "3. Play Random Song" << endl;

cout << "4. Remove Song" << endl;

cout << "5. Exit" << endl;

cout << "Choose an option (1-5): ";

cin >> choice;

cin.ignore(); // Clear input buffer

switch (choice) {

case 1:

cout << "Enter song name to add: ";

getline(cin, songName);

myPlaylist.addSong(songName);

break;

case 2:

myPlaylist.showSongs();

break;

case 3:
myPlaylist.playRandomSong();

break;

case 4:

cout << "Enter song name to remove: ";

getline(cin, songName);

myPlaylist.removeSong(songName);

break;

case 5:

cout << "Exiting... Goodbye!" << endl;

break;

default:

cout << "Invalid choice! Try again." << endl;

} while (choice != 5);

return 0;

( Outputs )
===== Music Library Menu =====

1. Add Song

2. Show All Songs

3. Play Random Song

4. Remove Song

5. Exit

Choose an option (1-5): 1

Enter song name to add: Bohemian Rhapsody


Song added successfully!

===== Music Library Menu =====

1. Add Song

2. Show All Songs

3. Play Random Song

4. Remove Song

5. Exit

Choose an option (1-5): 1

Enter song name to add: Stairway to Heaven

Song added successfully!

===== Music Library Menu =====

1. Add Song

2. Show All Songs

3. Play Random Song

4. Remove Song

5. Exit

Choose an option (1-5): 2

📋 Your Music Library:

1. Bohemian Rhapsody

2. Stairway to Heaven

===== Music Library Menu =====

1. Add Song

2. Show All Songs

3. Play Random Song


4. Remove Song

5. Exit

Choose an option (1-5): 3

Now playing: **Stairway to Heaven**

===== Music Library Menu =====

1. Add Song

2. Show All Songs

3. Play Random Song

4. Remove Song

5. Exit

Choose an option (1-5): 5

Exiting... Goodbye!

You might also like