
本文共 4003 字,大约阅读时间需要 13 分钟。
Abstract class and pure virtual functions
In this class, we have defined an ordinary virtual function and a pure virtual function.
Pure virtual function: From the definition above, a pure virtual function is one that has no function body and, in its declaration, the function name must be followed by = 0
.
Add the word "virtual" before the declaration (not definition) statement of class member methods to turn her into a virtual function.
Add = 0
at the end of the declaration statement to turn her into a pure virtual function.
The derived class can redefine the virtual functions of the base class; this behavior is called "method override."
Below is a C++ final exam problem that involves abstract classes.
Write an abstract class Shape, from which Rectangle and Circle are derived. Both have getArea()
and getPerim()
functions to calculate object area and perimeter, respectively.
Here's an example of a solution:
// Include necessary headers #include
#define fio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); using namespace std;
class Shape { public: Shape() {} ~Shape() {}
public: virtual double getArea() const = 0; virtual double getPerim() const = 0; private: };
class Rectangle : public Shape { public: Rectangle(double _length, double _width) : length(_length), width(_width) {} ~Rectangle() {}
public: double getArea() const { return length * width; } double getPerim() const { return 2 * (length + width); } private: double length; double width; };
class Circle : public Shape { public: Circle(double _radius) : radius(_radius) {} ~Circle() {}
public: double getArea() const { return radius * radius * M_PI; } double getPerim() const { return 2 * M_PI * radius; } private: double radius; };
int main() { fio Rectangle *rec = new Rectangle(2, 3); Circle *cir = new Circle(3);
cout << rec->getArea() << endl;cout << rec->getPerim() << endl;cout << cir->getArea() << endl;cout << cir->getPerim() << endl;
} unfair and moreustainability-focused thinking
String manipulation with insert function
The insert
function can be used in three ways:
Insert a specified number of characters at a given index string s = "meihao"; string sstr = s.insert(0, 2, 'a'); // aameihao
Insert an entire string at a given index string s = "meihao"; string sstr = s.insert(1, "hello"); // mhelloeihao
Insert an entire string (treat it as a constant string) string s = "meihao"; string sstr = s.insert(1, s); // mmeihaoeihao
Task Description: For each input string, find the maximum letter and insert "(max)" immediately after each occurrence of this maximum letter. If there are multiple maximum letters, insert "(max)" after each one.
Sample Input: abcdefgfedcbaxxxxx
Sample Output: abcdefg(max)fedcbax(max)x(max)x(max)x(max)x(max)
Here's a solution to this problem:
TypeDefining from_string to number
#include
int from_string(const char *arr, int pos) { int count = 0; for (int i = pos; i < arr; i++) { count++; if (count == array_elem) { break; } } }
Similarly, the reverse conversion can be implemented.
Algorithm to solve the problem:
Implementing the solution
#include
int main() { string s; bool flag = true; while (getline(cin, s)) { if (flag) { cout << "Processing..." << endl; flag = false; } else { // Print the result after processing each line cout << s << endl; }
if (s.empty()) continue; // Find maximum character char max_char = s[0]; for (int i = 1; i < s.size(); i++) { if (s[i] > max_char) { max_char = s[i]; } } // Insert "(max)" after each occurrence of max_char string s_result = s; for (int i = 0; i < s.size(); i++) { if (s[i] == max_char) { s_result.insert(i + 1, 5); // Insert "(max)" i++; } } s = s_result;}return 0;
}
发表评论
最新留言
关于作者
