Get premium membership and access questions with answers, video lessons as well as revision papers.

a)Create a class that contains a person's name and telephone number. Using new, dynamically allocate an object of this class and put your name and...

      

a)Create a class that contains a person's name and telephone number. Using new, dynamically allocate an object of this class and put your name and phone number into these fields within this object.
b).What are the two ways that new might indicate an allocatn failure?

  

Answers


Davis
a).#include
#include
using namespace std;
class phone {
char name[40];
char nur[14];
public:
void store(char *n, char *num);
void show();
};
void phone::store(char *n, char *num) {
strcpy(name, n);
strcpy(number, num);
id phone::show()
{
cout <cout << "\n";
}
int main()
{
phone *p;
p = new phone;
if(!p) {
cout << "Allocation error.";
n 1;
}
p->store("Isaac Newton", "111 555-2323");
p->show();
delete p;
return 0;
}


b).On failure, new will either return a null pointer or generate an exception.You must check you compiler's documentation to determine which approach is used. In standard C++, new generates an exception by default
Githiari answered the question on May 31, 2018 at 17:12


Next: What is wrong with following program? // This program has an error. #include using namespace std; void triple(double &num); int main() { double d = 7.0; tripe(&d); cout << d; return 0; } //...
Previous: a) Show how to convert the following code into its equivalent that uses new. char *p; p= (char *) malloc(100); //... strcpy(p, "This is a test"); b)Using new, show how...

View More Computer Studies Questions and Answers | Return to Questions Index


Learn High School English on YouTube

Related Questions