Modify Program to include the assignment statement b = a, and then run the modif
ID: 3696091 • Letter: M
Question
Modify Program to include the assignment statement b = a, and then run the modified program to assess any error messages that occur. #include#include
using namespace std;
// class declaration section
class Book
{
private:
char *title; //a pointer to a book title
public:
Book(char * = ‘’); //constructor
void showtitle(void); //display the title
};
// class implementation section
Book::Book(char*strng)
{
title = new char[strlen(strng) + 1]; //allocate memory
strcpy(title,strng); //store the string
}
void Book::showtitle(void)
{ cout << title << endl;
return
}
int main()
{
Book book1(“Windows Primer”); // create 1st title
Book book2(“History of Western Civilization”); // 2nd title
book1.showtitle(); //display book1’s title
book2.showtitle(); //display book2’s title
return 0;
}
Explanation / Answer
Answer:
The implementation after the include of assignment statement b=a on modification of the program is as shown below :
#include<iostream>
#include<cstring>
using namespace std;
class Book
{
private:
char *title;
public:
Book(char * = '');
void showtitle(void);
};
Book::Book(char *strng)
{
title = new char[strlen(strng) + 1];
strcpy(title,strng); //store the string
}
void Book:: showtitle(void)
{
cout << title << endl;
return;
}
int main()
{
char *a="A Brief History of Western Civilization";
char *b=a; //assignment statement implementation by modification in program
Book book1("Windows Primer");
Book book2(b);
book1.showtitle();
book2.showtitle();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.