59 lines
1.2 KiB
C++
Raw Normal View History

#include <iostream>
#include <vector>
#include <string>
#include <memory>
#include <algorithm>
class Person {
public:
Person(int id, std::string name)
: id_(id), name_(std::move(name)) {}
int id() const { return id_; }
const std::string& name() const { return name_; }
virtual void greet() const {
std::cout << "Hello, I'm " << name_ << std::endl;
}
virtual ~Person() = default;
private:
int id_;
std::string name_;
};
class Student : public Person {
public:
Student(int id, std::string name, double gpa)
: Person(id, std::move(name)), gpa_(gpa) {}
void greet() const override {
std::cout << "Hi, I'm " << name() << " (GPA: " << gpa_ << ")" << std::endl;
}
private:
double gpa_;
};
template <typename T>
T max_value(T a, T b) {
return (a > b) ? a : b;
}
int main() {
auto p = std::make_unique<Student>(1, "Alice", 3.9);
p->greet();
std::vector<int> nums = {3, 1, 4, 1, 5, 9};
std::sort(nums.begin(), nums.end());
for (int n : nums) {
std::cout << n << " ";
}
std::cout << std::endl;
std::cout << "Max of 10 and 20: " << max_value(10, 20) << std::endl;
return 0;
}