Lab-Report NO 3
Lab-Report NO 3
Lab-Report NO 3
Objective:
⮚ To learn types of constructors in classes their use and syntax
⮚ Understand difference between Shallow and Deep Copy
LAB ASSESSMENT:
Ability to Conduct
Experiment
Data presentation
Experimental results
Conclusion
Total Marks: Obtained Marks:
Date: Signature:
Task1:
Task2
Create a class SavingAccountthat helps to maintain the accounts of different customers each
customer having its own savingsBalance and also stores an annualInterestRateProvide a
member function to calculate Monthlyinterestthat calculate the monthly interest by
(balance*annualInterestRate)/12. This interest should be added to savingBalance.ass
SavingAccountthat helps to maintain the accounts of different customers each customer having
its own savingsBalance and also stores an annualInterestRateProvide a member function to
calculate Monthlyinterestthat calculate the monthly interest by
(balance*annualInterestRate)/12. This interest should be added to savingBalance.
Code:
Output:
Task3:
Create a class called Invoice that a hardware store might use to represent an invoice for
an item sold at the store. An Invoice should include four data members—a part number
(type string), a part description (type string), a quantity of the item being purchased
(type int) and a price per item (type int). Your class should have a constructor that
initializes the four data members. A constructor that receives multiple arguments is
defined with the form:
Provide a set and a get function for each data member. In addition, provide a member
function named getInvoiceAmount that calculates the invoice amount (i.e., multiplies
the quantity by the price per item), then returns the amount as an int value. If the
quantity is not positive, it should be set to 0. If the price per item is not positive, it
should be set to 0. Write a test program that demonstrates class Invoice’s capabilities.
Code:
OUTPUT
Task 4.
Create a class name copy_concatenate that take char* as data member and
concatenate the given string with existing one. Provide following functions:
● copy_concatenate(const char *str)
● copy_concatenate(const copy_concatenate &)
● void concatenate(const char *)
● ~copy_concatenate()
● Void display()
Code:
OUTPUT:
Conclusion:
In this Lab we have learned about Destructors, Parameterized Constructors and Copy
Constructors. We also have performed different tasks in order to practice these. now we
are familiar to Destructors, Parameterized Constructors and Copy Constructors.
THINK:-
Why do we need to overwrite destructors in our class?
ANS:- The CPP Reference says that override makes sure that the function is virtual and that it indeed
overrides a virtual function. So the override keyword would make sure that the destructor is virtual. If
you specify override but not = default , then you will get a linker error.
It is necessary to pass object as reference and not by value because if you pass it by value its copy is
constructed using the copy constructor. This means the copy constructor would call itself to make copy.
This process will go on until the compiler runs out of memory.
THE END