Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER


ENGINEERING
LAB TITLE: Destructors, Parameterized Constructors and Copy
Constructors

Student Name: RIZWAN AHMAD Reg.No: 190450.

Objective:
⮚ To learn types of constructors in classes their use and syntax
⮚ Understand difference between Shallow and Deep Copy

LAB ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)

Ability to Conduct
Experiment

Ability to assimilate the


results

Effective use of lab


equipment and follows
the lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion
Total Marks: Obtained Marks:

Date: Signature:

Task1:

I. Create a class POINT to represent a point in Cartesian coordinate system Choose


appropriate data members. Provide member functions:
● Default constructor
● Parameterized constructor
● Input
● Output
● Distance( returns the distance between two POINT objects,
the function takes one object as an input)
● isZero(determines if a point is the center)
● Midlepoint(returns the middle point , takes one object as an
input and returns the answer in a POINT object)
● isEqualTo (compare two POINTs)
● isGreaterThan (compares two POINT in terms of the distance from the center )
o Above two function takes one POINT object as an argument and returns true if
the condition is satisfied and false otherwise
Code:
Output:

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:

ClassName( TypeName1 parameterName1, TypeName2 parameterName2, ... )

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.

Why is copy constructor needed?


Copy Constructor is a type of constructor which is used to create a copy of an already existing object of a
class type. It is usually of the form X (X&), where X is the class name. The compiler provides a default
Copy Constructor to all the classes.

Why copy constructor receives a constant object of same class as an argument?

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

You might also like