Polymorphism:
Q1: Create a class "Animal" with a method "makeSound." Create instances of "Dog" and "Cat" and call the "makeSound" method.
output.cpp
// C++ code
#include <iostream>
#include <string>
using namespace std;
class Animal {
public:
virtual void makeSound() {
Question21cout << "The animal makes a generic sound." << Question21endl;
}
};
class Dog : public Animal {
public:
void makeSound() override {
Question21cout << "The dog barks." << Question21endl;
}
};
class Cat : public Animal {
public:
void makeSound() override {
Question21cout << "The cat meows." << Question21endl;
}
};
int main() {
Animal* genericAnimal = new Animal();
Animal* dog = new Dog();
Animal* cat = new Cat();
genericAnimal->makeSound();
dog->makeSound();
cat->makeSound();
delete genericAnimal;
delete dog;
delete cat;
return 0;
}
Q2: Implement a function that takes a list of shapes (circles and rectangles) and calculates their areas using polymorphism.
output.cpp
// C++ code
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class Shape {
public:
virtual double area() const = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override {
return M_PI * radius * radius;
}
};
class Rectangle : public Shape {
private:
double width;
double height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
double area() const override {
return width * height;
}
};
double totalArea(const Question21vector<Shape*>& shapes) {
double total = 0.0;
for (const Shape* shape : shapes) {
total += shape->area();
}
return total;
}
int main() {
Circle circle(5.0);
Rectangle rectangle(4.0, 6.0);
Question21vector<Shape*> shapes;
shapes.push_back(&circle);
shapes.push_back(&rectangle);
double total = totalArea(shapes);
Question21cout << "Total area of shapes: " << total << Question21endl;
return 0;
}
Q3: Create a program that displays different messages depending on the type of vehicle (car, bike) using polymorphism.
output.cpp
// C++ code
#include <iostream>
#include <string>
using namespace std;
class Vehicle {
public:
virtual void displayInfo() {
cout << "This is a generic vehicle." << endl;
}
};
class Car : public Vehicle {
public:
void displayInfo() override {
cout << "This is a car. It can carry passengers." << endl;
}
};
class Bike : public Vehicle {
public:
void displayInfo() override {
cout << "This is a bike. It is for individual use." << endl;
}
};
int main() {
Vehicle* genericVehicle = new Vehicle();
Vehicle* car = new Car();
Vehicle* bike = new Bike();
genericVehicle->displayInfo();
car->displayInfo();
bike->displayInfo();
delete genericVehicle;
delete car;
delete bike;
return 0;
}
Q4: Extend the "Animal" hierarchy with more animal types and override the "makeSound" method for each.
output.cpp
// C++ code
#include <iostream>
#include <string>
using namespace std;
class Animal {
public:
virtual void makeSound() {
cout << "The animal makes a generic sound." << endl;
}
};
class Dog : public Animal {
public:
void makeSound() override {
cout << "The dog barks." << endl;
}
};
class Cat : public Animal {
public:
void makeSound() override {
cout << "The cat meows." << endl;
}
};
class Cow : public Animal {
public:
void makeSound() override {
cout << "The cow moos." << endl;
}
};
int main() {
Animal* genericAnimal = new Animal();
Animal* dog = new Dog();
Animal* cat = new Cat();
Animal* cow = new Cow();
genericAnimal->makeSound();
dog->makeSound();
cat->makeSound();
cow->makeSound();
delete genericAnimal;
delete dog;
delete cat;
delete cow;
return 0;
}
Q5: Build a drawing application where different shapes can be drawn on a canvas using polymorphism.
output.cpp
// C++ code
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class Shape {
public:
virtual void draw() {
// Default implementation
}
};
class Circle : public Shape {
private:
double radius;
int x, y;
public:
Circle(double r, int cx, int cy) : radius(r), x(cx), y(cy) {}
void draw() override {
cout << "Drawing a circle at (" << x << ", " << y << ") with radius " << radius << endl;
}
};
class Rectangle : public Shape {
private:
int width, height;
int x, y;
public:
Rectangle(int w, int h, int cx, int cy) : width(w), height(h), x(cx), y(cy) {}
void draw() override {
cout << "Drawing a rectangle at (" << x << ", " << y << ") with width " << width << " and height " << height << endl;
}
};
int main() {
vector<Shape*> canvas;
Circle circle1(5.0, 10, 10);
Rectangle rectangle1(8, 6, 20, 20);
canvas.push_back(&circle1);
canvas.push_back(&rectangle1);
for (Shape* shape : canvas) {
shape->draw();
}
return 0;
}
Q6: Implement a program that simulates a traffic signal with different vehicle types using polymorphism.
output.cpp
// C++ code
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
using namespace std;
class Vehicle {
public:
virtual void move() {
cout << "Generic vehicle is moving." << endl;
}
};
class Car : public Vehicle {
public:
void move() override {
cout << "Car is moving on the road." << endl;
}
};
class Bicycle : public Vehicle {
public:
void move() override {
cout << "Bicycle is pedaling on the bike lane." << endl;
}
};
class TrafficSignal {
public:
void simulate(const vector<Vehicle*>& vehicles, int duration) {
for (int i = 0; i < duration; i++) {
cout << "Green light - Go!" << endl;
for (Vehicle* vehicle : vehicles) {
vehicle->move();
}
this_thread::sleep_for(chrono::seconds(2));
cout << "Red light - Stop!" << endl;
this_thread::sleep_for(chrono::seconds(1));
}
}
};
int main() {
Car car;
Bicycle bike;
vector<Vehicle*> vehicles = {&car, &bike};
TrafficSignal signal;
signal.simulate(5, vehicles);
return 0;
}
Q7: Design a music player application that can play different audio formats (mp3, wav) using polymorphism.
output.cpp
// C++ code
#include <iostream>
#include <string>
using namespace std;
class AudioFile {
public:
virtual void play() {
cout << "Playing audio." << endl;
}
};
class MP3File : public AudioFile {
public:
void play() override {
cout << "Playing MP3 audio." << endl;
}
};
class WAVFile : public AudioFile {
public:
void play() override {
cout << "Playing WAV audio." << endl;
}
};
int main() {
MP3File mp3;
WAVFile wav;
AudioFile* currentTrack = &mp3;
currentTrack->play();
currentTrack = &wav;
currentTrack->play();
return 0;
}
Q8: Create a graphical user interface (GUI) library that can render various UI elements (buttons, text fields) with polymorphic rendering functions.
output.cpp
// C++ code
#include <iostream>
using namespace std;
// Base class for UI elements
class UIElement {
public:
virtual void render() {
cout << "Rendering a generic UI element." << endl;
}
};
// Subclass for rendering buttons
class Button : public UIElement {
public:
void render() override {
cout << "Rendering a button." << endl;
}
};
// Subclass for rendering text fields
class TextField : public UIElement {
public:
void render() override {
cout << "Rendering a text field." << endl;
}
};
int main() {
UIElement* elements[] = {new Button(), new TextField()};
for (UIElement* element : elements) {
element->render();
delete element;
}
return 0;
}
Q9: Implement a plugin system for a text editor where different plugins provide syntax highlighting for various programming languages using polymorphism.
output.cpp
// C++ code
#include <iostream>
#include <vector>
#include <map>
using namespace std;
// Base class for syntax highlight plugins
class SyntaxHighlightPlugin {
public:
virtual void highlightSyntax(const string& code) {
// Default implementation
cout << "No syntax highlighting for this language." << endl;
}
};
// Concrete plugins for different programming languages
class CppSyntaxHighlightPlugin : public SyntaxHighlightPlugin {
public:
void highlightSyntax(const string& code) override {
cout << "Syntax highlighting for C++ code." << endl;
// Actual syntax highlighting logic for C++ here
}
};
class PythonSyntaxHighlightPlugin : public SyntaxHighlightPlugin {
public:
void highlightSyntax(const string& code) override {
cout << "Syntax highlighting for Python code." << endl;
// Actual syntax highlighting logic for Python here
}
};
int main() {
// Simulating a text editor with plugins
vector<SyntaxHighlightPlugin*> plugins;
plugins.push_back(new CppSyntaxHighlightPlugin());
plugins.push_back(new PythonSyntaxHighlightPlugin());
string code = "int main() {\n // Your code here\n}";
for (SyntaxHighlightPlugin* plugin : plugins) {
plugin->highlightSyntax(code);
}
for (SyntaxHighlightPlugin* plugin : plugins) {
delete plugin;
}
return 0;
}