C++ex 8-13
C++ex 8-13
#include <iostream>
using namespace std;
// Function prototype
double calculateTotalExpenses(double tuition, double books, double
accommodation);
int main() {
double tuitionFee, booksCost, accommodationCost;
// User input
cout << "Enter tuition fee: ";
cin >> tuitionFee;
// Display result
cout << "Total expenses for the semester: Rs " << totalExpenses << endl;
return 0;
}
// Function definition
double calculateTotalExpenses(double tuition, double books, double
accommodation) {
return tuition + books + accommodation;
}
FUNCTION OVERLOADING
#include<iostream>
using namespace std;
int main() {
int x, y;
double c, d;
profit(c, d);
profit(x, y);
return 0;
}
OPERATOR OVERLOADING
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Product
{
private:
string name;
double price;
double taxRate;
public:
// Constructor to initialize product details
Product(const string& n, double p, double tr): name(n), price(p), taxRate(tr) {}
// Overload the operator to calculate total cost including tax
double operator*(int quantity) const {
double subtotal=price* quantity;
double taxAmount = subtotal*(taxRate/100);
double totalCost=subtotal + taxAmount;
return totalCost;
}
// Display product information
void display() const {
cout << "Product: "<<name << endl;
cout << "Price: Rs" << fixed << setprecision(2) << price << endl;
cout << "Tax Rate: " << taxRate <<"%" << endl;
}
};
int main()
{
Product laptop("Laptop", 1000.0, 10.0);
Product smartphone("Smartphone", 500.0, 8.0);
int laptopQuantity = 2;
int smartphoneQuantity = 3;
public:
// Constructor for TaxableProduct that calls the base class constructor
TaxableProduct(const string& n, double p, double tr) : Product(n, p),
taxRate(tr) {}
int main() {
Product product1("Book", 20.0); // Creating a Product object
TaxableProduct product2("Laptop", 1000.0, 10.0); // Creating a
TaxableProduct object
return 0;
}
MULTIPLE INHERITANCE
#include<iostream>
using namespace std;
class student {
protected:
int rno, m1, m2;
public:
void get() {
cout << "Enter the Roll no :";
cin>>rno;
cout << "Enter math mark:”;
cin >> m1;
cout << "Enter science mark:";
cin >> m2;
}
};
class sports {
protected:
int sm; // sm= Sports mark
public:
void getsm() {
cout << "\nEnter the sports mark:";
cin>>sm;
}
};
class statement: public student, public sports {
int tot, avg;
public:
void display() {
tot=(m1+m2 + sm);
avg = tot/3;
cout << "\n\n\tRoll No: " << rno << "\n\tTotal :" << tot;
cout << "\n\tAverage:"<<avg;
}
};
int main() {
statement obj;
obj.get();
obj.getsm();
obj.display();
return 0;
}