#includeusing namespace std; class Person { private: string name; public: void setName(string n) { name = n; } string getName() { return name; } }; int main() { Person p; p.setName("John"); cout << "Name: " << p.getName() << endl; return 0; }
#includeIn this example, we create a `Shape` base class with a protected `name` variable and a virtual `getName` function. We also create a `Circle` derived class that inherits from `Shape` and has a private `radius` variable. The `Circle` class overrides the `getName` function to append "(circle)" to the name. In the main function, we create a `Circle` object using the `Shape` pointer `s`. We then print the name of the `Shape` object, which calls the overridden `getName` function in the `Circle` class and returns "My circle (circle)". Package/Library: This code does not use any external package or library.using namespace std; class Shape { protected: string name; public: Shape(string n) : name(n) {} virtual string getName() { return name; } }; class Circle : public Shape { private: int radius; public: Circle(string n, int r) : Shape(n), radius(r) {} string getName() { return name + " (circle)"; } }; int main() { Shape* s = new Circle("My circle", 10); cout << "Name: " << s->getName() << endl; delete s; return 0; }