0% found this document useful (0 votes)
16 views

C++ex 8-13

Uploaded by

Patrick Alwin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

C++ex 8-13

Uploaded by

Patrick Alwin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

FUNCTION AND FUNCTION PROTOTYPING

#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;

cout << "Enter cost of books: ";


cin >> booksCost;

cout << "Enter accommodation cost: ";


cin >> accommodationCost;

// Call the function to calculate total expenses


double totalExpenses = calculateTotalExpenses(tuitionFee, booksCost,
accommodationCost);

// 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;

void profit(double sp, double cp) {


cout << "Profit = " << (sp - cp);
}

void profit(int sp, int cp) {


cout << endl << "Profit = " << (sp - cp);
}

int main() {
int x, y;
double c, d;

cout << "Function Overloading" << endl;


// Calling profit(double, double)
cout << "Executing profit(double, double)" << endl;
cout << "Enter Selling Price in decimal: ";
cin >> c;

cout << endl << "Enter Cost Price in decimal: ";


cin >> d;

profit(c, d);

// Calling profit(int, int)


cout << endl << "Executing profit(int, int)" << endl;
cout << "Enter Selling Price in integer: ";
cin >> x;

cout << endl << "Enter Cost Price in Integer: ";


cin >> y;

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;

// Calculate total cost using operator overloading


double totalCostLaptop=laptop*laptopQuantity;
double totalCostSmartphone =smartphone*smartphoneQuantity;
// Display product information and total cost
laptop.display();
cout << "Quantity: " << laptopQuantity << endl;
cout << "Total Cost including tax: Rs "<< fixed << setprecision(2) <<
totalCostLaptop <<endl;
smartphone.display();
cout << "Quantity: "<< smartphoneQuantity << endl;
cout << "Total Cost including tax: Rs" << fixed << setprecision(2) <<
totalCostSmartphone << endl;
return 0;
}
INHERITANCE
#include <iostream>
#include <string> // Include string library for modern string handling
using namespace std;
// Base class Product
class Product {
protected:
string name; // Use std::string instead of char array
double price;
public:
// Constructor for Product
Product(const string& n, double p) : name(n), price(p) {}

// Display method to show product details


virtual void display() const { // Made virtual to allow overriding in derived
classes
cout << "Product: " << name << endl;
cout << "Price: Rs " << price << endl;
}
};

// Derived class TaxableProduct inheriting from Product


class TaxableProduct : public Product {
private:
double taxRate;

public:
// Constructor for TaxableProduct that calls the base class constructor
TaxableProduct(const string& n, double p, double tr) : Product(n, p),
taxRate(tr) {}

// Overriding display method to include tax information


void display() const override {
Product::display(); // Call the base class display method
cout << "Tax Rate: " << taxRate << "%" << endl;
}
};

int main() {
Product product1("Book", 20.0); // Creating a Product object
TaxableProduct product2("Laptop", 1000.0, 10.0); // Creating a
TaxableProduct object

cout << "Displaying product 1:" << endl;


product1.display();
cout << endl;

cout << "Displaying product 2:" << endl;


product2.display();

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;
}

You might also like