Assignment Write a C++ program in which you are required to define a classnamed
ID: 3614933 • Letter: A
Question
Assignment
Write a C++ program in which you are required to define a classnamed Account. The class must include thefollowing two data members.
// data member for account holder’sname
1: Account_Holder
//data member for amount in the account
2: Amount
Your Program should define three constructors for theclass Account
1: a constructor with no parameter
2: a constructor with two parameters (Account_Holder,Amount)
3: a copy constructor
All of these three constructors are meant toinitialize their respectiveobjects. Incase of copy constructor, you are required to assign aseparate space for the data members of the new object while copyingthe values of previously existed object.
Declare three objects (1 for each type of constructor)in main.
Write a function in class Account to display theinitialized data members for eachobject.
Also write destructor for the class Account. Display amessage that says “destructor called” in the destructorbody.
Note: you can do better by making your variable names moremeaningful. Adding proper comments and indenting your codeproperly.
OUTPUT
Your output should be similar to thefollowing
Ahsan
15000
_________________
Umar
70000
_________________
Qasim
19000
Explanation / Answer
//header files
#include<iostream.h>
#include<conio.h>
classAccount
{
private:
char*Account_holder;
char*Amount;
public:
Account()
{
Account_holder = "Ahsan" ;
Amount = "15000";
}
Account(char* Account_holder1,char* Amount1)
{
Account_holder = Account_holder1;
Amount = Amount1;
}
Account(const Account&other)
{
int length1, length2;
length1 =strlen(other. Account_holder);
length2 =strlen(other. Amount);
Account_holder = new char[length1 + 1];
Amount =new char[length2 + 1];
strcpy( Account_holder, other.Account_holder );
strcpy( Amount,other.Amount );
}
void print()
{
cout<<Account_holder<<endl<<Amount<<endl;
cout<<"_________________"<<endl;
}
~Account(){}
};
int main()
{
Accountacc1;
Account acc2("Umar", "70000");
Account acc3("Qasim","19000");
//print function calls
acc1.print();
acc2.print();
acc3.print();
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.