main.cpp: In member function ‘void HackerClass::SetToken(const string&)’: main.cpp:21:7:…
- User
main.cpp: In member function ‘void HackerClass::SetToken(const string&)’: main.cpp:21:7: error: ‘std::string SecretClass::token’ is private within this context 21 | this->token = newToken; | ^~~~~ main.cpp:5:13: note: declared private here 5 | std::string token; | ^~~~~. FIX NEXT CODE ! #include
class SecretClass { private: std::string token; protected: void SetTokenTo(SecretClass& another) { another.token = token; } public: SecretClass(const std::string& token) : token(token) {}; std::string GetToken() const { return token; } };
class HackerClass : public SecretClass { public: HackerClass(const std::string& token) : SecretClass(token) {} void SetToken(const std::string& newToken) { this->token = newToken; } };
void externalFunction(SecretClass& secret) { HackerClass& hacker = static_cast<HackerClass&>(secret); hacker.SetToken("CODE"); }
- Assistant
#include
class SecretClass { private: std::string token; protected: void SetTokenTo(SecretClass& another) { another.token = token; } public: SecretClass(const std::string& token) : token(token) {}; std::string GetToken() const { return token; } };
class HackerClass : public SecretClass { public: HackerClass(const std::string& token) : SecretClass(token) {} void SetToken(const std::string& newToken) { SecretClass::token = newToken; } };
void externalFunction(SecretClass& secret) { HackerClass& hacker = static_cast<HackerClass&>(secret); hacker.SetToken("CODE"); }
- User
In this task, you will define the dynamic type of objects Namely, there is a class hierarchy class Shape { public: virtual double area() = 0; }; class Ellipse : public Shape { // Oval public: double area() override; }; class Circle : public Ellipse { // Circle public: double area() override; }; class Rectangle : public Shape { // Rectangle public: double area() override; }; class Square : public Rectangle { // Square public: double area() override; }; Your task is to write functions bool IsCircle(const Shape& shape); bool IsEllipse(const Shape&shape); bool IsSquare(const Shape&shape); bool IsRectangle(const Shape& shape);
- IsCircle returns true if the underlying shape object is a Circle. false -- otherwise
- IsEllipse returns true if the underlying shape object is an Ellipse. false -- otherwise
- IsSquare returns true if the underlying shape object is a Square. false -- otherwise
- IsRectangle returns true if the underlying shape object is a Rectangle. false -- otherwise Namely, the call to IsRectangle(Square()) must return false, and only the call IsRectangle(Rectangle()) will return true. Send only the code of functions and the necessary libraries to the system, everything else will be connected automatically Hint: think about how you can use dynamic_cast
- Assistant
#include
bool IsCircle(const Shape& shape) { return dynamic_cast<const Circle*>(&shape) != nullptr; }
bool IsEllipse(const Shape& shape) { return dynamic_cast<const Ellipse*>(&shape) != nullptr; }
bool IsSquare(const Shape& shape) { return dynamic_cast<const Square*>(&shape) != nullptr; }
bool IsRectangle(const Shape& shape) { return dynamic_cast<const Rectangle*>(&shape) != nullptr; }