Write a C++ program in which you are required to define a classnamed Account . T
ID: 3614890 • Letter: W
Question
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 to initializetheir respective objects. Incase of copy constructor, you arerequired to assign a separate space for the data members of the newobject while copying the values of previously existedobject.
Declare three objects (1 for each type of constructor)in main.
Write a function in class Account to display theinitialized data members for each object.
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>
//class definition
class Account
{
//hidden part
private:
char*Account_holder; //for Account'sAccount_holder
char*Amount; //forAmount
//interface
public:
//paramterless constructor
Account()
{
Account_holder = "Ahsan" ;
Amount = "15000";
}
//parameterized constructor
Account(char* Account_holder1, char*Amount1)
{
Account_holder = Account_holder1;
Amount = Amount1;
}
//copy constructor
Account(const Account &other)
{
//for stringlengths
int length1, length2;
length1 =strlen(other. Account_holder);
length2 =strlen(other. Amount);
//allocating newmemory space
Account_holder = new char[length1 + 1];
Amount = newchar[length2 + 1];
//copyingstrings
strcpy( Account_holder, other.Account_holder );
strcpy( Amount,other.Amount );
}
//for printing Account_holder and Amount
void print()
{
cout<<Account_holder<<endl<<Amount<<endl;
cout<<"_________________"<<endl;
}
//destructor
~Account(){}
};
//main function
int main()
{
//object declarations
Account acc1;
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.