Inheritance:
Q1: Create a base class "Animal" with derived classes "Dog" and "Cat." Add properties and methods to each derived class.
output.cpp
// C++ code
#include <iostream>
#include <string>
using namespace std;
class Animal {
protected:
string name;
int age;
public:
Animal(const string& animalName, int animalAge) : name(animalName), age(animalAge) {}
virtual void makeSound() {
cout << "Animal makes a sound." << endl;
}
};
class Dog : public Animal {
public:
Dog(const string& dogName, int dogAge) : Animal(dogName, dogAge) {}
void makeSound() override {
cout << "Dog barks." << endl;
}
void fetch() {
cout << "Dog fetches a ball." << endl;
}
};
class Cat : public Animal {
public:
Cat(const string& catName, int catAge) : Animal(catName, catAge) {}
void makeSound() override {
cout << "Cat meows." << endl;
}
void climb() {
cout << "Cat climbs a tree." << endl;
}
};
int main() {
Animal* myDog = new Dog("Buddy", 2);
Animal* myCat = new Cat("Whiskers", 3);
myDog->makeSound();
myCat->makeSound();
Dog* myDogDerived = dynamic_cast<Dog*>(myDog);
if (myDogDerived) {
myDogDerived->fetch();
}
Cat* myCatDerived = dynamic_cast<Cat*>(myCat);
if (myCatDerived) {
myCatDerived->climb();
}
delete myDog;
delete myCat;
return 0;
}
Q2: Implement a base class "Shape" and create subclasses "Circle" and "Rectangle" to calculate area and perimeter.
output.cpp
// C++ code
#include <iostream>
#include <cmath>
using namespace std;
class Shape {
public:
virtual double calculateArea() const = 0;
virtual double calculatePerimeter() const = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea() const override {
return M_PI * radius * radius;
}
double calculatePerimeter() const override {
return 2 * M_PI * radius;
}
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double calculateArea() const override {
return length * width;
}
double calculatePerimeter() const override {
return 2 * (length + width);
}
};
int main() {
Circle circle(5.0);
Rectangle rectangle(4.0, 6.0);
cout << "Circle Area: " << circle.calculateArea() << " Perimeter: " << circle.calculatePerimeter() << endl;
cout << "Rectangle Area: " << rectangle.calculateArea() << " Perimeter: " << rectangle.calculatePerimeter() << endl;
return 0;
}
Q3: Design a base class "Vehicle" with subclasses "Car" and "Bike" to represent different vehicle types.
output.cpp
// C++ code
#include <iostream>
#include <string>
using namespace std;
class Vehicle {
protected:
string type;
int wheels;
public:
Vehicle(const string& vehicleType, int numWheels)
: type(vehicleType), wheels(numWheels) {}
void displayInfo() {
cout << "Type: " << type << ", Wheels: " << wheels << " wheels" << endl;
}
};
class Car : public Vehicle {
public:
Car() : Vehicle("Car", 4) {}
};
class Bike : public Vehicle {
public:
Bike() : Vehicle("Bike", 2) {}
};
int main() {
Car myCar;
Bike myBike;
myCar.displayInfo();
myBike.displayInfo();
return 0;
}
Q4: Extend the "Vehicle" hierarchy with additional subclasses like "Truck" and "Motorcycle."
output.cpp
// C++ code
#include <iostream>
#include <string>
using namespace std;
class Vehicle {
protected:
string type;
int wheels;
public:
Vehicle(const string& vehicleType, int numWheels)
: type(vehicleType), wheels(numWheels) {}
void displayInfo() {
cout << "Type: " << type << ", Wheels: " << wheels << " wheels" << endl;
}
};
class Car : public Vehicle {
public:
Car() : Vehicle("Car", 4) {}
};
class Bike : public Vehicle {
public:
Bike() : Vehicle("Bike", 2) {}
};
class Truck : public Vehicle {
public:
Truck() : Vehicle("Truck", 6) {}
};
class Motorcycle : public Vehicle {
public:
Motorcycle() : Vehicle("Motorcycle", 2) {}
};
int main() {
Car myCar;
Bike myBike;
Truck myTruck;
Motorcycle myMotorcycle;
myCar.displayInfo();
myBike.displayInfo();
myTruck.displayInfo();
myMotorcycle.displayInfo();
return 0;
}
Q5: Create an abstract base class "Shape" with methods for calculating area and perimeter and then create concrete shapes that inherit from it.
output.cpp
// C++ code
#include <iostream>
using namespace std;
class Shape {
public:
virtual double calculateArea() const = 0;
virtual double calculatePerimeter() const = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea() const override {
return 3.14159265359 * radius * radius;
}
double calculatePerimeter() const override {
return 2 * 3.14159265359 * radius;
}
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double calculateArea() const override {
return length * width;
}
double calculatePerimeter() const override {
return 2 * (length + width);
}
};
int main() {
Circle circle(5.0);
Rectangle rectangle(4.0, 6.0);
cout << "Circle Area: " << circle.calculateArea() << " Perimeter: " << circle.calculatePerimeter() << endl;
cout << "Rectangle Area: " << rectangle.calculateArea() << " Perimeter: " << rectangle.calculatePerimeter() << endl;
return 0;
}
Q6: Develop a class hierarchy representing different types of employees (e.g., "Manager," "Developer") with common and specific attributes/methods.
output.cpp
// C++ code
#include <iostream>
#include <string>
using namespace std;
class Employee {
protected:
string name;
int employeeId;
double salary;
public:
Employee(const string& empName, int empId, double empSalary)
: name(empName), employeeId(empId), salary(empSalary) {}
virtual void displayDetails() {
cout << "Name: " << name << ", Employee ID: " << employeeId << ", Salary: $" << salary << endl;
}
virtual void work() {
cout << "Employee is working." << endl;
}
};
class Manager : public Employee {
int teamSize;
public:
Manager(const string& empName, int empId, double empSalary, int size)
: Employee(empName, empId, empSalary), teamSize(size) {}
void displayDetails() override {
cout << "Manager Details - ";
Employee::displayDetails();
cout << "Team Size: " << teamSize << " members" << endl;
}
void organizeMeetings() {
cout << "Manager is organizing meetings." << endl;
}
};
class Developer : public Employee {
string programmingLanguage;
public:
Developer(const string& empName, int empId, double empSalary, const string& language)
: Employee(empName, empId, empSalary), programmingLanguage(language) {}
void displayDetails() override {
cout << "Developer Details - ";
Employee::displayDetails();
cout << "Programming Language: " << programmingLanguage << endl;
}
void writeCode() {
cout << "Developer is writing code." << endl;
}
};
int main() {
Manager manager("Alice", 101, 75000, 5);
Developer developer("Bob", 201, 60000, "Python");
manager.displayDetails();
manager.organizeMeetings();
developer.displayDetails();
developer.writeCode();
return 0;
}
Q7: Implement a class hierarchy for a game with a base class "GameObject" and subclasses "Player," "Enemy," and "Obstacle."
output.cpp
// C++ code
#include <iostream>
#include <string>
using namespace std;
class GameObject {
protected:
string name;
int x, y;
public:
GameObject(const string& objName, int posX, int posY)
: name(objName), x(posX), y(posY) {}
virtual void displayInfo() {
cout << "Name: " << name << ", Position: (" << x << ", " << y << ")" << endl;
}
};
class Player : public GameObject {
public:
Player(const string& playerName, int posX, int posY)
: GameObject(playerName, posX, posY) {}
void displayInfo() override {
cout << "Player Info - ";
GameObject::displayInfo();
cout << "Type: Player" << endl;
}
void move(int newX, int newY) {
x = newX;
y = newY;
cout << "Player moved to position (" << x << ", " << y << ")." << endl;
}
};
class Enemy : public GameObject {
public:
Enemy(const string& enemyName, int posX, int posY)
: GameObject(enemyName, posX, posY) {}
void displayInfo() override {
cout << "Enemy Info - ";
GameObject::displayInfo();
cout << "Type: Enemy" << endl;
}
void attack() {
cout << "Enemy attacks the player." << endl;
}
};
class Obstacle : public GameObject {
public:
Obstacle(const string& obstacleName, int posX, int posY)
: GameObject(obstacleName, posX, posY) {}
void displayInfo() override {
cout << "Obstacle Info - ";
GameObject::displayInfo();
cout << "Type: Obstacle" << endl;
}
void blockPath() {
cout << "Obstacle blocks the path." << endl;
}
};
int main() {
Player player("Hero", 2, 3);
Enemy enemy("Dragon", 6, 9);
Obstacle obstacle("Wall", 4, 4);
player.displayInfo();
player.move(5, 6);
enemy.displayInfo();
enemy.attack();
obstacle.displayInfo();
obstacle.blockPath();
return 0;
}
Q8: Create a class hierarchy for a geometric shapes library, allowing users to create custom shapes by inheriting from a base "Shape" class.
output.cpp
// C++ code
#include <iostream>
#include <cmath>
using namespace std;
class Shape {
public:
virtual double area() const = 0;
virtual double perimeter() const = 0;
virtual void displayInfo() const {
cout << "Shape: Unknown" << endl;
}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override {
return M_PI * radius * radius;
}
double perimeter() const override {
return 2 * M_PI * radius;
}
void displayInfo() const override {
cout << "Shape: Circle, Radius: " << radius << endl;
}
};
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 perimeter() const override {
return 2 * (width + height);
}
void displayInfo() const override {
cout << "Shape: Rectangle, Width: " << width << ", Height: " << height << endl;
}
};
int main() {
Circle circle(5.0);
Rectangle rectangle(4.0, 6.0);
Shape* customShape = new Circle(3.0);
circle.displayInfo();
cout << "Circle Area: " << circle.area() << " Perimeter: " << circle.perimeter() << endl;
rectangle.displayInfo();
cout << "Rectangle Area: " << rectangle.area() << " Perimeter: " << rectangle.perimeter() << endl;
customShape->displayInfo();
cout << "Custom Shape Area: " << customShape->area() << " Perimeter: " << customShape->perimeter() << endl;
delete customShape;
return 0;
}
Q9: Develop a class hierarchy for a banking system, including classes like "Account," "SavingsAccount," and "CheckingAccount" with inheritance relationships.
output.cpp
// C++ code
#include <iostream>
#include <string>
using namespace std;
class Account {
protected:
string accountNumber;
double balance;
public:
Account(const string& accNumber, double initialBalance)
: accountNumber(accNumber), balance(initialBalance) {}
virtual void deposit(double amount) {
balance += amount;
}
virtual void withdraw(double amount) {
if (balance >= amount)
balance -= amount;
else
cout << "Insufficient funds!" << endl;
}
virtual void displayInfo() const {
cout << "Account Number: " << accountNumber << ", Balance: $" << balance << endl;
}
};
class SavingsAccount : public Account {
double interestRate;
public:
SavingsAccount(const string& accNumber, double initialBalance, double rate)
: Account(accNumber, initialBalance), interestRate(rate) {}
void applyInterest() {
balance += balance * interestRate;
}
void displayInfo() const override {
cout << "Savings Account Number: " << accountNumber << ", Balance: $" << balance << ", Interest Rate: " << (interestRate * 100) << "%" << endl;
}
};
class CheckingAccount : public Account {
double overdraftLimit;
public:
CheckingAccount(const string& accNumber, double initialBalance, double limit)
: Account(accNumber, initialBalance), overdraftLimit(limit) {}
void withdraw(double amount) override {
if (balance + overdraftLimit >= amount)
balance -= amount;
else
cout << "Exceeded overdraft limit! Cannot withdraw." << endl;
}
void displayInfo() const override {
cout << "Checking Account Number: " << accountNumber << ", Balance: $" << balance << ", Overdraft Limit: $" << overdraftLimit << endl;
}
};
int main() {
SavingsAccount savings("SA12345", 1000, 0.03);
CheckingAccount checking("CA67890", 500, 200);
savings.displayInfo();
savings.deposit(200);
savings.applyInterest();
savings.displayInfo();
checking.displayInfo();
checking.withdraw(700);
checking.displayInfo();
return 0;
}