Wednesday, May 20, 2020

CPP Questions and Answers 2

Learn C++ with Examples

A private telecommunication company generates monthly mobile bills for their customers. The bill includes Bill Number, Monthly Units Consumed, Charge per Unit, Monthly Rental Amount and Final Bill Amount. Assume that the monthly rental is fixed for all the bills. Write an Object-Oriented program to perform the following operations using necessary functions. Each operation requires a separate function in the program. (Hint: Final bill Amount=(Monthly Units Consumed * Charge per Unit)+ Monthly Rental Amount.

i) Input data for bill number, monthly units consumed, Monthly Rental Amount and Charge per unit of two bills.
ii) Calculate the final amount of each bill.
iii) Output the final bill amount of each bill.


#include <iostream >
using namespace std;
class Bill
{
protected:
 string bill_no;
 int unit_consume;
 double charge_per_unit;
public:
 static double monthly_rental;

 Bill(string b_no, int unit_con, double charge)
 {
  bill_no = b_no;
  unit_consume = unit_con;
  charge_per_unit = charge;
 }
 double calc_amount()
 {
  return (unit_consume * charge_per_unit) + monthly_rental;
 }
 void bill_print()
 {
  cout << "Bill No:" << bill_no<<"\t";
  cout << "Unit Consumed:" << unit_consume<<"\t";
  cout << "Charge Per Unit:" << charge_per_unit<<"\t";
  cout << "Monthly Rental:" << monthly_rental << "\t";
  cout << "Amount:" << calc_amount()<<"\n";

 }

};
double Bill::monthly_rental = 1500.00;
int main()
{
 Bill b1("A001", 10, 150.00);
 b1.bill_print();
 Bill b2("A002", 16, 200.00);
 b2.bill_print();
}

An Object-oriented application has two classes. They are class Product and class Electronic_Product. The class Product has attributes Price of the product and Quantity produced. The class Electronic_Product has an attribute Tax Rate.
Each class uses own function to assign data for given attributes. The class product has function Calculate() which calculates the total value of a product. Total value of the product =price of the product * quantity produced.
The class Electronic_Product overrides the function Calculate of class Product to calculate the Tax value of an Electronic_Product.
Tax value of the electronic product=price of the electronic product * tax rate of the electronic product.
Write an object oriented program which perform following operations.

i) Set price quantity produced and Tax rate of one electronic product.
ii) Calculate Tax Value of the electronic product.

#include <iostream >
using namespace std;
class Product
{
protected:
 double Price;
 int Quantity;
public:
 void setPrice(double p)
 {
  Price = p;
 }
 void setQuantity(int q)
 {
  Quantity = q;
 }
 virtual double Calculate()
 {
  return Price * Quantity;
 }
 void Display()
 {
  cout << "Price:" << Price << "\t";
  cout << "Quantity:" << Quantity << "\t";
  cout << "Calculate:" << Calculate() <<"\n";
 }
};

class Electronic_Product :public Product
{
protected:
 double Tax_Rate;
public:
 void setTaxRate(double tx)
 {
  Tax_Rate = tx;
 }
 double Calculate()
 {
  return Price * Tax_Rate/100;
 }
};
int main()
{
 Product p;
 p.setPrice(25);
 p.setQuantity(10);
 p.Display();
 Electronic_Product ep;
 ep.setPrice(100);
 ep.setQuantity(20);
 ep.setTaxRate(5);
 ep.Display();
}

Some more examples for inheritance, Polymorphism (Early binding)

#include <iostream >
class Animals
{
protected:
 int no_legs;
 int no_tails;
public:
 Animals()
 {
  no_legs = 4;
  no_tails = 1;
 }
 void animalSound()
 {
  cout << "animal makes sound..." << "\n";
 }
 void display()
 {
 cout << "No of legs :" << no_legs << " No of Tails" << no_tails <<"\n";
 }

};
class Cat :public Animals
{
public:
 void animalSound()
 
  cout << "Cat says mews mews..." << "\n";
 }

};

class Crow :public Animals
{
public:
 void animalSound()
 {
  cout << "crow says says ca..ca.ca..." << "\n";
 }
 Crow()
 {
  no_legs = 2;
 }

};
int main()
{
 Animals a1;
 a1.animalSound();
 a1.display();
 Cat c1;
 c1.animalSound();
 c1.display();
 Crow cc1();
 cc1.animalSound();
 cc1.display();
}

A class customer has customer number and name. The function writeData() writes number and name to file "customer". The function readData() reads and displays customer name according to the customer number.
Write an object oriented program to
i)Write customet number and name to the file "customer"
ii) Read and display the customer name when number is input.


#include <iostream>
#include <fstream>

using namespace std;
class customer
{ 
    fstream mfile;
public:  
    customer()
    {
        mfile.open("c:\\ganesh\\customer" );
    }
 
    void writeData(string custcode,string custname)
    {
        mfile << custcode + "," +custname<<endl; 
    }
   void readData(string xx)
    {
       string x,t;

       mfile.seekg(0);
       while (mfile.eof() != true)
       {
           mfile >> x;
           t = x.substr(0, 4);
          if (t == xx)
           {
               cout <<x;
               cout <<endl;
           } 
       }         
    }
    ~customer()
    {
        mfile.close();
    }
};
                    

int main() 
{   
    customer c1;
    c1.writeData("C001","Peter");
    c1.writeData("C002", "David");
    c1.writeData("C003", "White");
    c1.writeData("C004", "Black");
    c1.readData("C002");
}

No comments:

Post a Comment