Knowee
Questions
Features
Study Tools

What will be the output for the following code?1234567891011121314151617181920212223242526#include <iostream>class City {public: City() { name = new char[20]; } ~City() { delete[] name; } void SetName(const char* newName) { name = new char[strlen(newName) + 1]; strcpy(name, newName); } void DisplayName() { std::cout << "City: " << name << std::endl; }private: char* name;};int main() { City city; city.SetName("Metropolis"); city.DisplayName(); return 0;}

Question

What will be the output for the following code?1234567891011121314151617181920212223242526#include <iostream>class City {public: City() { name = new char[20]; } ~City() { delete[] name; } void SetName(const char* newName) { name = new char[strlen(newName) + 1]; strcpy(name, newName); } void DisplayName() { std::cout << "City: " << name << std::endl; }private: char* name;};int main() { City city; city.SetName("Metropolis"); city.DisplayName(); return 0;}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the code will be:

City: Metropolis

This is because the code creates an instance of the class City, sets the name of the city to "Metropolis" using the SetName method, and then displays the name using the DisplayName method.

Similar Questions

What will be the output of the following code?

What will be the output of the following program?1234567891011121314151617181920#include <stdio.h>void one();void two();void one() { for(int i = 0; i < 2; i++){ two(); }}void two() { printf("TWO ");}int main(){ one(); two(); return 0;}

What is the output for the following code?123456789#include <stdio.h>int main(){ char ch = '1'; for (ch = '5'; ch <= '8'; ch++) putchar(ch); return (0);}

What will be the output of the following code?1234567891011121314151617#include <stdio.h>int main() { int a = 100; int *p = &a; int count = 0; while (count < 1) { *p += 1; ++count; } int b = *p; printf("%d, %d", a, b); return 0; }

What will be the output of the following code snippet?123456789101112131415#include <stdio.h> int main() { char direction = 'N'; if (direction == 'N') printf("North"); else if (direction == 'S') printf("South"); else if (direction == 'E') printf("East"); else if (direction == 'W') printf("West"); else printf("Unknown"); return 0; }

1/4

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.