Docsity

C++ Classes and Objects: Practical Examples, Schemes and Mind Maps of Object Oriented Programming

Toggle sidebar
bg1
1.
#include <iostream>
using namespace std;
class Item {
private:
string itemName;
float unitPrice;
int quantity;
public:
Item(string name, float price, int qty) {
itemName = name;
unitPrice = price;
quantity = qty;
}
float calculateTotal() {
return unitPrice * quantity;
}
void display() {
cout << "Item Name: " << itemName << endl;
cout << "Unit Price: " << unitPrice << endl;
cout << "Quantity: " << quantity << endl;
cout << "Total Price: " << calculateTotal() << endl;
}
};
pf3
pf4
pf5

Partial preview of the text

Download C++ Classes and Objects: Practical Examples and more Schemes and Mind Maps Object Oriented Programming in PDF only on Docsity!

#include using namespace std; class Item { private: string itemName; float unitPrice; int quantity; public: Item(string name, float price, int qty) { itemName = name; unitPrice = price; quantity = qty; } float calculateTotal() { return unitPrice * quantity; } void display() { cout << "Item Name: " << itemName << endl; cout << "Unit Price: " << unitPrice << endl; cout << "Quantity: " << quantity << endl; cout << "Total Price: " << calculateTotal() << endl; } };

int main() { Item item1("Apple", 50, 5); item1.display(); return 0; }

#include using namespace std; class DriverRating { public: string driverName; float rating; int totalRides; DriverRating(string name, float r, int rides) { driverName = name; rating = r; totalRides = rides; } float averageRating() { return rating / totalRides; } void compareRating(DriverRating other) { if (averageRating() > other.averageRating()) { cout << driverName << " has a higher average rating.\n"; }

accountCount++; } static int getAccountCount() { return accountCount; } void display() { cout << "Name: " << name << ", Balance: " << balance << endl; } }; int BankAccount::accountCount = 0; int main() { BankAccount acc1("Abdullah", 1000.0); BankAccount acc2("Ali", 1500.0); cout << "Total accounts created: " << BankAccount::getAccountCount() << endl; return 0; }

#include using namespace std; class Doctor { public: string name; string specialization; int experienceYears; Doctor(string n, string s, int e) { name = n;

specialization = s; experienceYears = e; } }; class Patient { public: string name; string disease; Doctor doctor; static int patientCount; Patient(string n, string d, Doctor doc) : doctor(doc) { name = n; disease = d; patientCount++; } void display() { cout << "Patient Name: " << name << endl; cout << "Disease: " << disease << endl; cout << "Assigned Doctor: " << doctor.name << endl; cout << "Specialization: " << doctor.specialization << endl; cout << "Experience: " << doctor.experienceYears << " years\n"; } static int getPatientCount() { return patientCount; } };