class Shape { public: virtual float area() = 0; virtual float perimeter() = 0; }; class Triangle : public Shape { public: // Implement area and perimeter functions }; class Circle : public Shape { public: // Implement area and perimeter functions }; class Rectangle : public Shape { public: // Implement area and perimeter functions }; // Main function int main() { // Create objects of each derived class Shape* t = new Triangle(); Shape* c = new Circle(); Shape* r = new Rectangle(); // Call area and perimeter functions for each object using polymorphism cout << "Triangle area: " << t->area() << endl; cout << "Circle perimeter: " << c->perimeter() << endl; cout << "Rectangle area: " << r->area() << endl; // Deallocate memory delete t; delete c; delete r; return 0; }
class BankAccount { private: int acctNumber; float balance; public: BankAccount(int acctNum, float initialBalance) { // Constructor to initialize the account } float getBalance() { // Return current balance } void deposit(float amount) { // Add amount to balance } void withdraw(float amount) { // Subtract amount from balance } void transfer(BankAccount* destAcct, float amount) { // Transfer money from this account to the destination account } }; // Main function int main() { // Create a bank account BankAccount myAcct(12345, 1000.00); // Deposit some money myAcct.deposit(500.00); cout << "New balance: " << myAcct.getBalance() << endl; // Withdraw some money myAcct.withdraw(200.00); cout << "New balance: " << myAcct.getBalance() << endl; // Transfer money to another account BankAccount destAcct(67890, 0.00); myAcct.transfer(&destAcct, 300.00); cout << "My new balance: " << myAcct.getBalance() << endl; cout << "Destination balance: " << destAcct.getBalance() << endl; return 0; }This code example represents an OOP example in C++. The package library used is a part of the standard C++ library.